Need Example of calling mutation from NodeJS/Lambda
See original GitHub issueHi,
I need to call a mutation from an AWS Lambda and I don’t manage to make it work. Unfortunately, I don’t find any example of directly using AppSync client for calling a mutation…
Currently, my code looks like that:
const AddNotificationMutation = gql(mutation addNotification($notificationId: ID!, $recipientId: String!, $dateTime: String!, $content: String) { addNotification(notificationId: $notificationId, recipientId: $recipientId, dateTime: $dateTime, content: $content) { notificationId recipientId content dateTime } }
);
const AppSyncConfig = { url: process.env.GRAPHQL_API_URL, region: process.env.REGION, auth: { type: AUTH_TYPE.AWS_IAM, }, disableOffline: true, };
// Set up Apollo client
`const client = new AWSAppSyncClient(AppSyncConfig);
export const createNotification = (notification, callback) => {
logger.debug('Call for notification: %s', JSON.stringify(notification));
client.hydrated().then(client => {
client.mutate({ mutation: AddNotificationMutation, variables: notification, fetchPolicy: 'network-only'})
.then(newNotification => {
logger.debug("newNotification created: %s", JSON.stringify(newNotification));
return callback(null, newNotification);
})
.catch(error => {
logger.error("newNotification failed: %s", JSON.stringify(error));
return callback(error, notification);
});
});
}`
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:18 (2 by maintainers)
Top GitHub Comments
@OlivierPT I was having the same trouble as you. Solved it by importing AWS and then setting credentials from the config from that:
Hi @OlivierPT
This will be helpful for you?