问题描述
我正在尝试使用Apollo客户端在GraphQl查询中使用动态变量.我已经遵循了文档,但是阿波罗一直给我错误,说我的变量没有定义,最终响应状态代码400.
这是阿波罗的文档所说的:
突变:(选项?:mutationOptions)=>承诺 触发UI突变的功能.您可以选择传递变量,OpportisticRespons,Refetcheries和AS AS选项,这将覆盖传递给突变组件的任何道具.该功能返回了通过突变结果实现的承诺.
这是我尝试编写的代码:
const fetch = require('node-fetch'); const ApolloClient = require('apollo-boost').default; const gql = require('graphql-tag'); const client = new ApolloClient({ uri: "http://api.domain.com/graphql", fetch }); run(); async function run() { try { const resp = await client.mutate({ mutation: gql`mutation { trackPr(id: $id, pr: $pr, title: $title, body: $body, state: $state, merged: $merged) { id } }`, variables: { id: 1, pr: 1, title: "test title", body: "test body", state: "test state", merged: false }, }); console.log(resp.data); } catch(ex) { console.log(ex); } }
然后,我将为每个变量收到一个错误消息,说明它尚未定义:
[GraphQl错误]:消息:变量" $ id"未定义.,位置:[对象对象],[对象对象],路径:Undfeined
这些错误消息中的每一个,然后我获得了带有状态代码400的最终消息:
[网络错误]:ServerRorr:响应不成功:接收状态代码400
突变本身在没有变量的情况下运行良好,并且直接在突变中设置的所有值,但是我不知道为什么它认为变量未定义.
推荐答案
必须将操作内部使用的任何变量声明为操作定义的一部分,例如:
mutation SomeOptionalMutationName ($id: ID!) { trackPr(id: $id) { id } }
这允许GraphQl对提供的类型验证您的变量,并验证变量正在代替右输入.
问题描述
I'm trying to use dynamic variable in a GraphQL query using Apollo Client. I have followed the documentation, but Apollo keeps giving me errors, saying that my variables are not defined, and ultimately responding with status code 400.
Here is what the documentation for Apollo said:
mutate: (options?: MutationOptions) => Promise A function to trigger a mutation from your UI. You can optionally pass variables, optimisticResponse, refetchQueries, and update in as options, which will override any props passed to the Mutation component. The function returns a promise that fulfills with your mutation result.
And here is the code I tried to write:
const fetch = require('node-fetch'); const ApolloClient = require('apollo-boost').default; const gql = require('graphql-tag'); const client = new ApolloClient({ uri: "http://api.domain.com/graphql", fetch }); run(); async function run() { try { const resp = await client.mutate({ mutation: gql`mutation { trackPr(id: $id, pr: $pr, title: $title, body: $body, state: $state, merged: $merged) { id } }`, variables: { id: 1, pr: 1, title: "test title", body: "test body", state: "test state", merged: false }, }); console.log(resp.data); } catch(ex) { console.log(ex); } }
I'll then get a error message for each variable saying it has not been defined:
[GraphQL error]: Message: Variable "$id" is not defined., Location: [object Object],[object Object], Path: undefined
After each of these error messages, I then get a final message with status code 400:
[Network error]: ServerError: Response not successful: Received status code 400
The mutation itself runs fine without the variables and all the values set directly in the mutation, but I don't know why it thinks the variables are not defined.
推荐答案
Any variables used inside an operation must be declared as part of the operation definition, like this:
mutation SomeOptionalMutationName ($id: ID!) { trackPr(id: $id) { id } }
This allows GraphQL to validate your variables against the provided type, and also validate that the variables are being used in place of the right inputs.