admin管理员组文章数量:1405298
I've been using GraphQL in a Node server using graphql-js, and GraphQL has shown to be an extremely valuable abstraction, but I'm running into an issue.
I often find myself needing to pass large structured objects as arguments to GraphQL mutations, using GraphQLInputObjectType
. This would be fine, but GraphQL doesn't support the use of JSON notation :(. So I end up just sending a string containing the JSON, for the server to deal with.
const objectStr = JSON.stringify(object).replace(new RegExp("\"", "g"), "'")
graphQLClient(`{
user: updateUser(someDataObject: "${objectStr}") {...}
}`)
But now I'm not benefiting at all from GraphQL!
I have a feeling I'm doing something wrong here. What is the GraphQL way of sending, say, signup form data, to a mutation?
I've been using GraphQL in a Node server using graphql-js, and GraphQL has shown to be an extremely valuable abstraction, but I'm running into an issue.
I often find myself needing to pass large structured objects as arguments to GraphQL mutations, using GraphQLInputObjectType
. This would be fine, but GraphQL doesn't support the use of JSON notation :(. So I end up just sending a string containing the JSON, for the server to deal with.
const objectStr = JSON.stringify(object).replace(new RegExp("\"", "g"), "'")
graphQLClient(`{
user: updateUser(someDataObject: "${objectStr}") {...}
}`)
But now I'm not benefiting at all from GraphQL!
I have a feeling I'm doing something wrong here. What is the GraphQL way of sending, say, signup form data, to a mutation?
Share Improve this question asked Apr 25, 2017 at 6:27 functorialfunctorial 6875 silver badges13 bronze badges1 Answer
Reset to default 5The best way of doing this is to use input objects.
Essentially your request would look like:
/* Query */
mutation Update($input: UpdateUserInput!) {
updateUser(input: $input) {
changedUser {
id
username
}
}
}
/* Variables (as JSON) */
{
"input": {
"username": "[email protected]",
"password": "SuperSecretPassword"
}
}
You would pass that into the content body of the payload in your POST request as this:
{
"query": <GraphQL query from above as a string>,
"variables": <JSON object from above>
}
If you want a deeper explanation, you can check out Scaphold's Docs for updating data to help you structure your API.
Hope this helps!
本文标签: javascriptPassing complex arguments to GraphQL mutationsStack Overflow
版权声明:本文标题:javascript - Passing complex arguments to GraphQL mutations - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744909849a2631846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论