aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

Skip resolver pipeline function or exit resolver pipeline early #261

Open Angus-Walsh opened 1 year ago

Angus-Walsh commented 1 year ago

Greetings!

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

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).

nikolajwestergaard commented 1 year ago

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 ❤️

onlybakam commented 1 year ago

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

Hideman85 commented 1 year ago

:+1: This is really needed

nits1991 commented 1 year ago

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

onlybakam commented 1 year ago

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

lqtruong commented 1 year ago

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

micchickenburger commented 10 months ago

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?

pdramirez-dev commented 9 months ago

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

onlybakam commented 9 months ago

Yes. in VTL, you can use #return

danielmarzan commented 3 months ago

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!