How to send a GraphQL mutation from one server to another?

I would like to save some Slack messages on a GraphQL server. I can use the Slack API and what they call "Slack App Commands", so every time a message is sent to my Slack channel, Slack automatically sends an HTTP POST request to my server with a new message as data.

I was thinking of using the AWS lambda function to redirect this request to send to the GraphQL server endpoint (I use GraphCool). I am new to GraphQL, I used Apollo to create mutations from the browser. Now I need to send the mutation from my Node server (AWS Lambda function) instead of the browser. How can i achieve this?

Thanks.

+7
lambda aws-lambda graphql apollo graphcool
source share
2 answers

GraphQL mutations are simply POST HTTP requests for the GraphQL endpoint. You can easily send it using any HTTP library such as request or axios .

For example, this mutation,

 mutation ($id: Int!) { upvotePost(postId: $id) { id } } 

and query variable,

 $id = 1 

is an HTTP POST request with a JSON payload

 { "query": "mutation ($id: Int!) { upvotePost(postId: $id) { id } } ", "variables": { "id": 1 } } 

Note that query is your GraphQL query as a string.

Using axios as an example, you can send this to your server using something like this,

 axios({ method: 'post', url: '/graphql', // payload is the payload above data: payload, }); 
+9
source share

Setting up AWS Lambda is left as an exercise for the reader.

To find out what GraphQL queries (or, in this case, mutations), your Apollo client code sends to the server, to cut + paste (and presumably parameterize) your lambda code, this tool exists: Apollo GraphQL Dev Tools , which now allows you to watch how your mutations are performed.

+3
source share

All Articles