如何将GraphQL突变从一个服务器发送到另一个服务器?[英] How to send GraphQL mutation from one server to another?

本文是小编为大家收集整理的关于如何将GraphQL突变从一个服务器发送到另一个服务器?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想将一些Slack消息保存到GraphQL后端.我可以使用Slack API及其所谓的" Slack App命令",因此每次将消息发送到我的Slack Channel时,Slack都会自动将HTTP POST请求发送到我的服务器,并以新消息为数据.

我正在考虑使用AWS lambda函数将此帖子请求转发到我的GraphQl Server端点(我正在使用GraphCool).我对GraphQL非常陌生,我使用Apollo从浏览器中创建突变.现在,我需要从节点服务器(AWS lambda函数)而不是浏览器发送突变.我该如何实现?

谢谢.

推荐答案

GraphQl突变只是http post请求到GraphQl端点.您可以使用任何HTTP库轻松发送一个,例如request或axios.

例如,这个突变,

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

和查询变量

$id = 1

是HTTP POST请求,带有JSON有效载荷

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

请注意,query是您的GraphQl查询作为字符串.

以axios为例,您可以使用类似的内容将其发送到服务器,

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

其他推荐答案

设置AWS lambda是读者的练习.

要查看Apollo客户端代码发送到服务器的GraphQl查询(或在这种情况下为突变),以切割+粘贴(可能是参数化)到您的lambda代码中,此工具存在: apollo graphql graphql dev工具现在可以观看它您的突变被执行.

本文地址:https://www.itbaoku.cn/post/1938049.html

问题描述

I would like to save some Slack messages to a GraphQL backend. I can use the Slack API and what they call "Slack App Commands" so everytime a message is send to my Slack channel, Slack will automatically send a HTTP POST request to my server with the new message as data.

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

Thanks.

推荐答案

GraphQL mutations are simply HTTP POST requests to a GraphQL endpoint. You can easily send one 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 of

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

Take 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,
});

其他推荐答案

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

To get to see what GraphQL queries (or in this case, mutations) your Apollo client code is sending to the server, for cutting+pasting (and presumably parameterising) into your lambda code, this tool exists: Apollo GraphQL Dev Tools which now allows you to watch your mutations being executed.