【问题标题】:Apollo server GraphQL network issueApollo 服务器 GraphQL 网络问题
【发布时间】:2021-10-04 11:29:08
【问题描述】:

我遇到了问题。我搜索并尝试了一切。这是关于我认为的网络问题或 GraphQL(Apollo-Server-Express)中的错误。我正在发送一个似乎完全正确且很好的查询,测试了这些值并且它们都很好。我发送请求,它显示 200 状态代码,它显示内容正确。但是,当我在开发工具中打开网络窗口时,就会出现问题。它给了我一个错误,即预期的值是一个字符串,但它收到了未定义的。即使在请求有效负载中也清晰可见。我确实检查了架构,它似乎是正确的。现在我不知道为什么会这样。实际上,我在解析器中接收数据,但所有这些数据都在一个变量(密码)中作为对象,而另一个变量(电子邮件)未定义。我没有可能损害传入请求的中间件。

客户端查询:

signIn: (email: string, password: string) => requests.post({query: `
    query ($email: String!, $password: String!) {
        signIn(email: $email, password: $password) {
            _id
            name
            email
            avatar
            admin
            rents{
                _id
            }
            createdAt
            updatedAt
          }
        }
    `, variables: {
        email: email,
        password: password,
    }}),

Network panel error

Request body

Schema query

Resolver and return outcome when executing the request

【问题讨论】:

  • 只是糟糕的解析器 [args 定义],阅读文档!在编码 FE 之前在操场上测试它(API)

标签: javascript express graphql apollo


【解决方案1】:

您的signIn 功能已损坏。它不接收 emailpassword 作为前两个参数,因为您的方法已声明它们。与每个解析器函数一样,第一个参数是父对象(Query 字段的根对象),第二个参数是参数对象

你的服务器代码中的日志语句的输出应该已经很明显了。

要修复您的代码,请使用

const Query = {
    async signIn(_root, args, _context) {
//                      ^^^^
        const { email, password } = args;
        …
    }
};

const Query = {
    async signIn(_root, {email, password}, _context) {
//                      ^^^^^^^^^^^^^^^^^
        …
    }
};

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2020-02-14
    • 1970-01-01
    • 2021-06-17
    • 2011-02-18
    • 2013-03-12
    相关资源
    最近更新 更多