Skip to content

Instantly share code, notes, and snippets.

@ottokruse
Last active July 23, 2019 06:17
Show Gist options
  • Save ottokruse/d0c15adb85c0214267a7e5027cb15fd0 to your computer and use it in GitHub Desktop.
Save ottokruse/d0c15adb85c0214267a7e5027cb15fd0 to your computer and use it in GitHub Desktop.
Example AWS Lambda function that executes AWS AppSync GraphQL Mutation using AWS Amplify
const Amplify = require('aws-amplify');
Amplify.default.configure({
aws_appsync_graphqlEndpoint: 'https://something.appsync-api.eu-west-1.amazonaws.com/graphql',
aws_appsync_region: 'eu-west-1',
aws_appsync_authenticationType: 'API_KEY',
aws_appsync_apiKey: 'yourkey'
});
const mutation = `mutation CreatePerson($firstName: String!, $lastName: String!, bankAccountBalance: Float!) {
createPerson(firstName: $firstName, lastName: $lastName, bankAccountBalance: $bankAccountBalance) {
firstName
}
}`;
exports.handler = async (event) => {
// Let's assume that the incoming event will at least have the following fields (and maybe more):
// { firstName: 'Edsger', lastName: 'Dijkstra', bankAccountBalance: 0.0 }
await Amplify.API.graphql(Amplify.graphqlOperation(mutation, event));
};
@kwhitejr
Copy link

kwhitejr commented Jul 23, 2019

Great write up! Any thoughts on how the necessary Appsync configuration (lines 4~7) can be pulled dynamically from Amplify config?

edit: Answering my own question, maybe >_<) seems like if you configure the lambda correctly, Amplify provide you commented references to the relevant environment variables. Example here: https://geromekevin.com/how-to-use-aws-appsync-in-lambda-functions/

@ottokruse
Copy link
Author

Not sure how that works with the environment variables. But if you get aws-exports.js in your Lambda bundle from an Amplify build you can just read that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment