Reputation: 576
UPDATE: apollo has updated code and docs so issue is irrelevant now
Intended outcome
start apollo-server-express using the following snippet from https://github.com/apollographql/apollo-tutorial-kit.
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`));
Actual Outcome:
In any implementation of apollo-server-express, using docs, using https://github.com/apollographql/apollo-tutorial-kit, etc. I receive the follow stack trace over and over again:
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>
How to reproduce the issue:
git clone https://github.com/apollographql/apollo-tutorial-kit.git
cd apollo-tutorial-kit
yarn/npm i
npm start
Upvotes: 2
Views: 6003
Reputation: 2751
I was using JQuery Ajax for HTTP Post Request to graphql server (apollo-server-express) and getting this error while querying for mutation. The reason was same as described by spencer.sm. To help other googlers, I am pasting here complete method-
(function() {
globals.makeGraphQLRequest('graphql', {
query: `query($email: String!) {
profile(email: $email) {
id mobile name email dob gender
}
}`,
variables: {
email: '[email protected]'
}
}, function successCallback(response) {
console.log(response);
});
globals.makeGraphQLRequest('graphql', {
query: `mutation($email: String!) {
updateUser(email: $email) {
email
}
}`,
variables: {
email: '[email protected]'
}
}, 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
});
}
Upvotes: -1
Reputation: 20518
In my case I was getting this error because I wasn't using the correct key in my request to the graphql server.
I was sending a mutation
request and I thought the key in the object needed to be "mutation"
but it turns out both query and mutation requests use the key "query"
.
xhr.open('POST', '/graphql');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({
query: mutationString,
}));
Upvotes: 9