aws-appsync-community
aws-appsync-community copied to clipboard
Skip resolver pipeline function or exit resolver pipeline early
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).
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 ❤️
posted a proposal here https://github.com/aws/aws-appsync-community/issues/147 please comment.
:+1: This is really needed
This is really needed. like nikolajwestergaard just returning some cheap operation.
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
Hi @onlybakam
Does that e.g. runtime.earlyReturn
also work with VTL pipeline? Many thanks.
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?
Hello @onlybakam Is there any functionality similar to runtime.earlyReturn but for resolvers in VTL? ?
Yes. in VTL, you can use #return
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!