【发布时间】:2020-05-12 06:16:41
【问题描述】:
我正在使用request-promise 库向graphql 服务器发出http 请求。为了实现查询,我这样做:
const query = `
{
user(id:"123173361311") {
_id
name
email
}
}
`
const options = {
uri: "http://localhost:5000/graphql",
qs: { query },
json: true
}
return await request(options)
上面的代码工作正常。但是我对如何进行突变感到困惑,因为我需要像这样指定实际的突变和 inputData:
// Input
{
name: "lomse"
email: "lomse@lomse.com"
}
const mutation = `
mutation addUser($input: AddUserInput!){
addUser(input: $input) {
_id
name
email
}
}
`
const option = {
uri: "http://localhost:5000/graphql",
formData: {mutation},
json: true,
// how to pass the actual data input
}
request.post(option)
或者request-promise 库不是为这个用例设计的?
【问题讨论】:
-
查询也可以包含变量...在某些 graphiql 游乐场中检查浏览器网络请求以获取详细信息
-
有趣的是,网络选项卡没有记录任何突变请求
-
可以使用socket通信
-
搜索
post mutation, f.e. stackoverflow.com/a/51636430/6124657 -
这项研究展示了如何从前端执行突变,这不是我想要完成的。就我而言,我有两台服务器需要相互通信