我该如何解决graphql错误:必须提供文件?

时间:2019-06-05 21:45:56

标签: graphql react-apollo graphql-js apollo-server

我正在使用graphql-yoga。我的查询可在GraphiQL和curl中工作,但在React中发送时不起作用。

这是我收到的错误:

Error: Must provide document
    at invariant (.../node_modules/graphql/jsutils/invariant.js:21:11)
    at Object.validate (.../node_modules/graphql/validation/validate.js:53:41)
    at doRunQuery (.../node_modules/apollo-server-core/dist/runQuery.js:111:42)
    at .../node_modules/apollo-server-core/dist/runQuery.js:21:56
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async Promise.all (index 0)

当我发送CURL时,它会起作用:

curl 'http://localhost:4000/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: http://localhost:4000' -H 'Authorization: XXX' --data-binary '{"query":"query { get_me { user { email } }}"}' --compressed

但是当我将其添加到React应用程序时,它不会:

export const myQuery = () => ({
  url: 'http://localhost:4000/graphql',

  options: {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'ABC',
    },
  },

  body: {
    data: '{"query":"get_user { user { email } }"}',
  },

1 个答案:

答案 0 :(得分:0)

我发现了问题:我应该使用

body: {
    query: 'query { get_user { user { email } } }',
  },

代替

body: {
    data: '{"query":"get_user { user { email } }"}',
  },