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

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

问题描述

我正在尝试使用 Apollo 客户端在 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.

以下是 Apollo 的文档内容:

Here is what the documentation for Apollo said:

mutate: (options?: MutationOptions) => Promise从您的 UI 触发突变的函数.您可以选择将变量、optimisticResponse、refetchQueries 和 update 作为选项传递,这将覆盖传递给 Mutation 组件的任何道具.该函数返回一个满足您的变异结果的承诺.

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"未定义,位置:[object Object],[object Object],路径:undefined

[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 Client:未定义变量.收到状态码 400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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