GraphQL:错误:必须提供文档

mar*_*768 1 javascript apollo graphql graphql-js apollo-server

更新:apollo已更新代码和文档,因此问题现在无关紧要

预期结果

使用https://github.com/apollographql/apollo-tutorial-kit中的以下代码段启动apollo-server-express .

import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import bodyParser from 'body-parser';
import schema from './data/schema';

const GRAPHQL_PORT = 3000;

const graphQLServer = express();

graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
graphQLServer.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));

graphQLServer.listen(GRAPHQL_PORT, () => console.log(`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`));
Run Code Online (Sandbox Code Playgroud)

实际结果:

在apollo-server-express的任何实现中,使用docs,使用https://github.com/apollographql/apollo-tutorial-kit等.我一遍又一遍地收到以下堆栈跟踪:

Error: Must provide document
    at invariant (~/node_modules/graphql/jsutils/invariant.js:18:11)
    at Object.validate (~/node_modules/graphql/validation/validate.js:58:34)
    at doRunQuery (~/node_modules/apollo-server-core/dist/runQuery.js:80:38)
    at ~/node_modules/apollo-server-core/dist/runQuery.js:20:54
    at <anonymous>
Run Code Online (Sandbox Code Playgroud)

如何重现问题:

git clone https://github.com/apollographql/apollo-tutorial-kit.git
cd apollo-tutorial-kit
yarn/npm i
npm start
Run Code Online (Sandbox Code Playgroud)

spe*_*.sm 8

在我的情况下,我收到此错误,因为我没有在我的graphql服务器请求中使用正确的密钥.

我发送了一个mutation请求,我认为对象中的密钥需要,"mutation"但事实证明查询和变异请求都使用密钥"query".

xhr.open('POST', '/graphql');
xhr.setRequestHeader("Content-Type", "application/json");

xhr.send(JSON.stringify({
  query: mutationString,
}));
Run Code Online (Sandbox Code Playgroud)