question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need Example of calling mutation from NodeJS/Lambda

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
damonmariacommented, Aug 4, 2018

@OlivierPT I was having the same trouble as you. Solved it by importing AWS and then setting credentials from the config from that:

import AWS from 'aws-sdk/global'
import { AUTH_TYPE } from 'aws-appsync/lib/link/auth-link'

new AWSAppSyncClient(
    {
      url: env.graphQlApiEndpoint,
      region: AWS.config.region,
      auth: {
        type: AUTH_TYPE.AWS_IAM,
        credentials: AWS.config.credentials,
      },
      disableOffline: true,
    },
    {
      defaultOptions: {
        query: {
          fetchPolicy: 'network-only',
          errorPolicy: 'all',
        },
      },
    },
  )
6reactions
harleygurucommented, Aug 4, 2018

Hi @OlivierPT

const mutation = gql(`
    mutation($infoID: String!){
            deleteInfo(infoID: $infoID) {
              infoID
              noOfDays
              noOfItems
              infoName
             }
    }`);

  const client = new AWSAppSyncClient({
    url: url,
    region: region,
    auth: {
        type: type,
        credentials: credentials,
    }
  });

  client.hydrated().then(function (client) {
      //Now run a query
      client.mutate({ mutation: mutation, variables:{infoID: infoID} })
          .then(function logData(data) {
              console.log('results of mutate: ', data);
          })
          .catch(console.error);
  });

This will be helpful for you?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling GraphQL API from a Lambda function - Amplify Docs
In this example you will create a mutation showing how to pass in variables as arguments to create a Todo record.
Read more >
Tutorial: Lambda resolvers - AWS AppSync
The following example shows a Lambda function written in Node.js that performs different operations on blog posts as part of a blog post...
Read more >
Perform a GraphQL Mutation with an AWS Lambda Function
In GraphQL you make changes to your database using what are called mutations. We can define a mutation in our schema and then...
Read more >
node.js - How can I call a graphql lambda from another ...
I can send the request as an event to trigger the graphql lambda. Is there a way to do that? For example, if...
Read more >
Backend GraphQL: How to trigger an AWS AppSync mutation ...
A developer can define an AWS Lambda function that is triggered after the user has authenticated with Amazon Cognito user pools, and then ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found