aws-appsync-community icon indicating copy to clipboard operation
aws-appsync-community copied to clipboard

Skip resolver pipeline function or exit resolver pipeline early

Open Angus-Walsh opened this issue 2 years ago • 10 comments

Greetings!

I Have been experimenting with the Javascript resolver functions and pipelines, and I was wondering if there was a way to:

  • Conditionally passthrough a function in a pipeline, i.e. check if I need to make a request, if not, then pass the previous response to the next function.
  • Conditionally exit the pipeline early with a response

As an example, the first resolver function in the pipeline queries a DynamoDB. I then have the following resolver function that creates a new entry if nothing was found by the previous function.

function request(ctx) {
  if (ctx.prev.result) {
    return; // Exit the function or pipeline here
  }

  const id = util.autoId();

  return {
    version: '2018-05-29',
    operation: 'PutItem',
    key: util.dynamodb.toMapValues({ id }),
    attributeValues: util.dynamodb.toMapValues(ctx.args)
  };
}

function response(ctx) {
  const { error, result } = ctx;
  if (error) {
    return util.appendError(error.message, error.type, result);
  }
  
  // Check if there was a result, otherwise pass on the previous result (not sure if this would be needed)
  if (result) {
    return result;
  } else {
    return ctx.prev.result;
  }
} 

Is it possible in this sort of a situation to "pass through" this resolver, or even return early from the pipeline?

I have seen that in VTL there is the concept of #return which looks like what I need but I can't find an equivalent for JS (I could also be misunderstanding #return as I am not that familiar with VTL).

Angus-Walsh avatar Feb 08 '23 00:02 Angus-Walsh

We have been using #return in VLC, and I have been looking for this functionality. Documentation mentions nothing about it!

As we are porting our VLC to APPSYNC_JS I really hope that it is a possibility, in certain circumstances I have to use "cheap calls" to the DB just to get by a unneeded pipeline function 👎

Hope this issue will be prioritised, JavaScript resolvers is really a great leap forward ❤️

nikolajwestergaard avatar Feb 22 '23 09:02 nikolajwestergaard

posted a proposal here https://github.com/aws/aws-appsync-community/issues/147 please comment.

onlybakam avatar Mar 01 '23 18:03 onlybakam

:+1: This is really needed

Hideman85 avatar Mar 11 '23 23:03 Hideman85

This is really needed. like nikolajwestergaard just returning some cheap operation.

nits1991 avatar Apr 05 '23 02:04 nits1991

We now support early return to skip data source request in a function or resolver. See details: https://docs.aws.amazon.com/appsync/latest/devguide/runtime-utils-js.html

onlybakam avatar Apr 07 '23 01:04 onlybakam

Hi @onlybakam Does that e.g. runtime.earlyReturn also work with VTL pipeline? Many thanks.

lqtruong avatar Nov 09 '23 03:11 lqtruong

We now support early return to skip data source request in a function or resolver. See details: https://docs.aws.amazon.com/appsync/latest/devguide/runtime-utils-js.html

This seems to only skip the current function execution. It will NOT stop a pipeline from executing the next function. Are there any plans for introducing a method for returning early from the entire pipeline?

micchickenburger avatar Jan 17 '24 18:01 micchickenburger

Hello @onlybakam Is there any functionality similar to runtime.earlyReturn but for resolvers in VTL? ?

pdramirez-dev avatar Feb 12 '24 06:02 pdramirez-dev

Yes. in VTL, you can use #return

onlybakam avatar Feb 20 '24 21:02 onlybakam

We now support early return to skip data source request in a function or resolver. See details: https://docs.aws.amazon.com/appsync/latest/devguide/runtime-utils-js.html

This seems to only skip the current function execution. It will NOT stop a pipeline from executing the next function. Are there any plans for introducing a method for returning early from the entire pipeline?

I just tested this and it works, thank you!

danielmarzan avatar Aug 13 '24 23:08 danielmarzan