【问题标题】:apollo-client server to server mutation causes error (query ok)apollo-client 服务器到服务器突变导致错误(查询正常)
【发布时间】:2017-08-14 23:24:34
【问题描述】:

我在我的 web 服务器上使用 apollo-client 与我的 graphql 服务器(也是 apollo)进行通信。我有一个成功的查询工作并正确检索数据,但是当我尝试突变时,我收到new ApolloError 消息。相同的突变复制/粘贴到 graphiql 中效果很好。

我已经在我的 graphql 服务器上检查了 CORS,它已启用并正常运行。我已经将代码中的突变复制并粘贴到我的 graphiql 编辑器中,它按预期工作。

有人可以帮我解释一下吗,或者指导我如何在我的 graphql-server 上查看更多关于为什么我收到 400 错误的信息?

这是阿波罗错误

{
  "graphQLErrors": [],
  "networkError": {
    "response": {
      "url": "http://localhost:9000/api/private",
      "status": 400,
      "statusText": "Bad Request",
      "headers": {
        "_headers": {
          "x-powered-by": [
            "Express"
          ],
          "access-control-allow-origin": [
            "*"
          ],
          "content-type": [
            "application/json"
          ],
          "date": [
            "Wed, 22 Mar 2017 05:36:21 GMT"
          ],
          "connection": [
            "close"
          ],
          "transfer-encoding": [
            "chunked"
          ]
        }
      },
      "ok": false,
      "body": {
        "_readableState": {
          "objectMode": false,
          "highWaterMark": 16384,
          "buffer": {
            "head": {
              "data": {
                "type": "Buffer",
                "data": [
                  123,
                  34,
                  101,
                  114,
                  114,
                  111,
                  114,
                  115,
                  34,
                  58,
                  91,
                  123,
                  34,
                  109,
                  101,
                  115,
                  115,
                  97,
                  103,
                  101,
                  34,
                  58,
                  34,
                  67,
                  97,
                  110,
                  110,
                  111,
                  116,
                  32,
                  113,
                  117,
                  101,
                  114,
                  121,
                  32,
                  102,
                  105,
                  101,
                  108,
                  100,
                  32,
                  92,
                  34,
                  116,
                  111,
                  107,
                  101,
                  110,
                  92,
                  34,
                  32,
                  111,
                  110,
                  32,
                  116,
                  121,
                  112,
                  101,
                  32,
                  92,
                  34,
                  85,
                  115,
                  101,
                  114,
                  83,
                  101,
                  114,
                  118,
                  105,
                  99,
                  101,
                  115,
                  92,
                  34,
                  46,
                  34,
                  44,
                  34,
                  108,
                  111,
                  99,
                  97,
                  116,
                  105,
                  111,
                  110,
                  115,
                  34,
                  58,
                  91,
                  123,
                  34,
                  108,
                  105,
                  110,
                  101,
                  34,
                  58,
                  57,
                  44,
                  34,
                  99,
                  111,
                  108,
                  117,
                  109,
                  110,
                  34,
                  58,
                  55,
                  125,
                  93,
                  125,
                  93,
                  125
                ]
              },
              "next": null
            },
            "tail": {
              "data": {
                "type": "Buffer",
                "data": [
                  123,
                  34,
                  101,
                  114,
                  114,
                  111,
                  114,
                  115,
                  34,
                  58,
                  91,
                  123,
                  34,
                  109,
                  101,
                  115,
                  115,
                  97,
                  103,
                  101,
                  34,
                  58,
                  34,
                  67,
                  97,
                  110,
                  110,
                  111,
                  116,
                  32,
                  113,
                  117,
                  101,
                  114,
                  121,
                  32,
                  102,
                  105,
                  101,
                  108,
                  100,
                  32,
                  92,
                  34,
                  116,
                  111,
                  107,
                  101,
                  110,
                  92,
                  34,
                  32,
                  111,
                  110,
                  32,
                  116,
                  121,
                  112,
                  101,
                  32,
                  92,
                  34,
                  85,
                  115,
                  101,
                  114,
                  83,
                  101,
                  114,
                  118,
                  105,
                  99,
                  101,
                  115,
                  92,
                  34,
                  46,
                  34,
                  44,
                  34,
                  108,
                  111,
                  99,
                  97,
                  116,
                  105,
                  111,
                  110,
                  115,
                  34,
                  58,
                  91,
                  123,
                  34,
                  108,
                  105,
                  110,
                  101,
                  34,
                  58,
                  57,
                  44,
                  34,
                  99,
                  111,
                  108,
                  117,
                  109,
                  110,
                  34,
                  58,
                  55,
                  125,
                  93,
                  125,
                  93,
                  125
                ]
              },
              "next": null
            },
            "length": 1
          },
          "length": 117,
          "pipes": null,
          "pipesCount": 0,
          "flowing": null,
          "ended": false,
          "endEmitted": false,
          "reading": true,
          "sync": false,
          "needReadable": true,
          "emittedReadable": true,
          "readableListening": false,
          "resumeScheduled": false,
          "defaultEncoding": "utf8",
          "ranOut": false,
          "awaitDrain": 0,
          "readingMore": false,
          "decoder": null,
          "encoding": null
        },
        "readable": true,
        "domain": null,
        "_events": {},
        "_eventsCount": 7,
        "_writableState": {
          "objectMode": false,
          "highWaterMark": 16384,
          "needDrain": false,
          "ending": false,
          "ended": false,
          "finished": false,
          "decodeStrings": true,
          "defaultEncoding": "utf8",
          "length": 0,
          "writing": false,
          "corked": 0,
          "sync": false,
          "bufferProcessing": false,
          "writecb": null,
          "writelen": 0,
          "bufferedRequest": null,
          "lastBufferedRequest": null,
          "pendingcb": 0,
          "prefinished": false,
          "errorEmitted": false,
          "bufferedRequestCount": 0,
          "corkedRequestsFree": {
            "next": null,
            "entry": null
          }
        },
        "writable": true,
        "allowHalfOpen": true,
        "_transformState": {
          "needTransform": true,
          "transforming": false,
          "writecb": null,
          "writechunk": null,
          "writeencoding": "buffer"
        }
      },
      "bodyUsed": false,
      "size": 0,
      "timeout": 0,
      "_raw": [],
      "_abort": false
    }
  },
  "message": "Network error: Network request failed with status 400 - \"Bad Request\""
}
Error
    at new ApolloError (/Users/rkstar/dev/projects/crate/microservices/dashboard/node_modules/apollo-client/apollo.umd.js:1490:23)
    at /Users/rkstar/dev/projects/crate/microservices/dashboard/node_modules/apollo-client/apollo.umd.js:2149:24
    at process._tickCallback (internal/process/next_tick.js:103:7)

**** 更新 **** 我在 apolloMiddleware 之前添加了中间件到 console.log 的 req.headers 和 req.body

req.headers -------
{
  "accept": "*/*",
  "content-type": "application/json",
  "authorization": "JWT ",
  "accept-encoding": "gzip,deflate",
  "user-agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)",
  "connection": "close",
  "content-length": "585",
  "host": "localhost:9000"
}

req.body -------
{
  "query": "mutation updateTwitterAccessToken($data: TokenInput!) {\n  updateTwitterToken(data: $data) {\n    _id\n    services {\n      twitter {\n        id\n        __typename\n      }\n      token {\n        token\n        expires\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n",
  "variables": {
    "data": {
      "accessToken": "000700000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "accessTokenSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "_id": "yyyyyyyyyyyyyyyyy",
      "service": "twitter",
      "serviceId": "000700000"
    }
  },
  "operationName": "updateTwitterAccessToken"
}

我从我的网络服务器调用client.mutate({ ... }),但在req.body 中,我设置了这个:

req.body -------
{
  "query": "mutation ..."
....
}

为什么是"query": "mutation ..." 而不是"mutation": "mutation ..."

【问题讨论】:

    标签: javascript express graphql apollo apollo-server


    【解决方案1】:

    TL;DR

    我解决了这个问题,这是因为我的update-token.graphql 文件的返回属性嵌套不正确 === 错误。


    好的...所以我解决了这个问题。这是我的 graphql 突变中的一个愚蠢的错误。我试图获取另一个字段中不存在的字段。

    我已将我的查询复制并粘贴到 graphiql 中,它运行良好!我忘记的是,我没有嵌套我的一个返回变量,这让一切变得不同。我以为我已将更改粘贴回.graphql 文件……但显然不是。

    有趣的是,我最终发现了这个错误:

    我尝试添加中间件来打印我的请求 -------

    router.all('/private',
      bodyParser.json(),
      (req, res, next)=>{
        console.log(JSON.stringify(req.route, null, 2))
        next()
      },
      apolloMiddleware
    )
    

    这向我展示了很多东西,但是在比较来自 graphiql 和 apollo-client 的请求时,我没有发现任何问题(可能是因为实际查询是一个字符串,而不是像漂亮的 json 那样格式化)

    然后我终于找到了formatError,我能够打印出我的错误:

    const apolloMiddleware: any = graphqlExpress(request => ({
      debug: debugMode,
      schema: executableSchema,
      context: request,
      formatError: e =>{
        console.log(JSON.stringify(e, null, 2))
      }
    }))
    

    这向我展示了这一点:

    {
      "message": "Cannot query field \"token\" on type \"UserServices\".",
      "locations": [
        {
          "line": 9,
          "column": 7
        }
      ]
    }
    

    看到这条消息后,我立即更新了我的.graphql 文件并做了一个史诗般的面部表情,因为我通过 apollo-client 看到了突变按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 2019-10-17
      • 2020-09-11
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多