1

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)

**** 更新 **** apolloMiddlewarereq.headers と req.body を console.log する前に、ミドルウェアを追加しました

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({ ... })自分の Web サーバーから呼び出していますが、req.bodyここでは次のように設定しています。

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

なぜそうでは"query": "mutation ..."ないの"mutation": "mutation ..."ですか?

4

1 に答える 1

1

TL;DR

i solved the problem and it was because my update-token.graphql file had a return property improperly nested === error.


OKAY... so i solved this problem. it was a silly error in my graphql mutation. i was trying to get a field that didn't exist within another field.

i had copied and pasted my query into graphiql where it worked fine! what i had forgotten was that i un-nested one of my return vars which made all the difference. i thought i had pasted my changes back to the .graphql file... but apparently not.

what's INTERESTING about this is how i finally figured out this error:

i tried adding middleware to print out my request -------

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

that showed me lots of things, but nothing jumped out at me when comparing the request from graphiql vs. apollo-client (probably because the actual query was a string and not formatted like nice json)

then i finally came upon formatError which i was able to have print out my error:

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

and THAT showed me this:

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

after seeing this message, i promptly updated my .graphql file and did an epic facepalm as i watched the mutation work as expected via apollo-client.

于 2017-03-23T04:57:32.277 に答える