Apollo客户端:未定义变量.收到状态码400 [英] Apollo Client: Variable is not defined. Received status code 400

查看:505
本文介绍了Apollo客户端:未定义变量.收到状态码400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apollo Client在GraphQL查询中使用动态变量.我遵循了文档,但是Apollo不断给我错误,说我的变量未定义,最终以状态码400响应.

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)=>承诺 从用户界面触发突变的功能.您可以选择将变量,optimisticResponse,refetchQueries和update作为选项传递,这将覆盖传递给Mutation组件的所有道具.该函数返回一个满足您突变结果的promise.

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错误]:消息:未定义变量"$ id".,位置:[对象对象],[对象对象],路径:未定义

[GraphQL error]: Message: Variable "$id" is not defined., Location: [object Object],[object Object], Path: undefined

每条错误消息之后,我都会收到一条状态码为400的最终消息:

After each of these error messages, I then get a final message with status code 400:

[网络错误]:ServerError:响应失败:收到状态码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
  }
}

这使GraphQL可以根据提供的类型验证变量,还可以验证变量是否在代替正确的输入.

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.

这篇关于Apollo客户端:未定义变量.收到状态码400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆