Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions samples/conditional-pipeline/function1-rds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { util, runtime } from '@aws-appsync/utils';
import { select, createPgStatement } from '@aws-appsync/utils/rds';

/**
* Sends a request to a Lambda function. Passes all information about the request from the `info` object.
* @param {import('@aws-appsync/utils').Context} ctx the context
*/
export function request(ctx) {
if (ctx.stash.useFirstDataSource === false) {
// if the condition is to not use first resolver, skip it
runtime.earlyReturn({});
}

return createPgStatement(select({ table: 'persons' }));
}

/**
* Process a Lambda function response
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the Lambda function response
*/
export function response(ctx) {
const { result, error } = ctx;
if (error) {
util.error(error.message, error.type, result);
}

ctx.stash.ds1resp = result;
return result;
}
40 changes: 40 additions & 0 deletions samples/conditional-pipeline/function2-lambda_async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { util, runtime } from '@aws-appsync/utils';

/**
* Sends a request to a Lambda function. Passes all information about the request from the `info` object.
* @param {import('@aws-appsync/utils').Context} ctx the context
*/
export function request(ctx) {
if (ctx.stash.useFirstDataSource === true) {
// If we already invoked the first resolver, skip this one
// Make sure to return result of previous function since we skip the current function
runtime.earlyReturn(ctx.prev.result);
}
return {
operation: 'Invoke',
invocationType: 'Event', // Invoke lambda async
payload: {
fieldName: ctx.info.fieldName,
parentTypeName: ctx.info.parentTypeName,
variables: ctx.info.variables,
selectionSetList: ctx.info.selectionSetList,
selectionSetGraphQL: ctx.info.selectionSetGraphQL,
},
};
}

/**
* Process a Lambda function response
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the Lambda function response
*/
export function response(ctx) {
const { result, error } = ctx;
if (error) {
util.error(error.message, error.type, result);
}

ctx.stash.ds2resp = result;

return result;
}
24 changes: 24 additions & 0 deletions samples/conditional-pipeline/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Starts the resolver execution
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the return value sent to the first AppSync function
*/
export function request(ctx) {
// check the number of arguments and set a valude in the stash that we can conditionally use later
if (Object.keys(ctx.args).length > 5) {
ctx.stash.useFirstDataSource = true;
} else {
ctx.stash.useFirstDataSource = false;
}

return {};
}

/**
* Returns the resolver result
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the return value of the last AppSync function response handler
*/
export function response(ctx) {
return ctx.prev.result;
}