更新: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`));
实际结果:
在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
答案 0 :(得分:8)
在我的情况下,我收到此错误,因为我没有在我的graphql服务器请求中使用正确的密钥。
我发送了一个mutation
请求,我认为对象中的密钥必须是"mutation"
,但事实证明查询和突变请求都使用密钥"query"
。
xhr.open('POST', '/graphql');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({
query: mutationString,
}));
答案 1 :(得分:-1)
我正在使用JQuery Ajax for HTTP Post Request to graphql server(apollo-server-express)并在查询突变时收到此错误。原因与spencer.sm描述的相同。为了帮助其他googlers,我在这里粘贴完整的方法 -
(function() {
globals.makeGraphQLRequest('graphql', {
query: `query($email: String!) {
profile(email: $email) {
id mobile name email dob gender
}
}`,
variables: {
email: 'varunon9@gmail.com'
}
}, function successCallback(response) {
console.log(response);
});
globals.makeGraphQLRequest('graphql', {
query: `mutation($email: String!) {
updateUser(email: $email) {
email
}
}`,
variables: {
email: 'varunon9@gmail.com'
}
}, function successCallback(response) {
console.log(response);
});
}());
makeGraphQLRequest -
globals.makeGraphQLRequest = function(url, params, successCallback) {
$.ajax({
url: url,
method: 'post',
data: params,
dataType: 'json',
success: function(response) {
successCallback(response);
},
error: globals.ajaxErrorHandler
});
}