更新:阿波罗已经更新了代码和文档,所以问题现在无关紧要

预期结果

使用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`));


实际结果:

在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>


如何重现该问题:

git clone https://github.com/apollographql/apollo-tutorial-kit.git
cd apollo-tutorial-kit
yarn/npm i
npm start

最佳答案

就我而言,出现此错误是因为我在向graphql服务器的请求中未使用正确的密钥。

我正在发送mutation请求,但我认为对象中的键必须为"mutation",但事实证明,查询请求和变异请求都使用键"query"

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

xhr.send(JSON.stringify({
  query: mutationString,
}));

关于javascript - GraphQL:错误:必须提供文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46849235/

10-11 15:02