簡體   English   中英

GraphQL:錯誤:必須提供文檔

[英]GraphQL: Error: Must provide Document

更新:阿波羅已更新代碼和文檔,所以問題現在不相關

預期結果

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

我正在將JQuery Ajax用於對graphql服務器(apollo-server-express)的HTTP發布請求,並在查詢突變時收到此錯誤。 原因與spencer.sm描述的相同 為了幫助其他Google員工,我在這里粘貼了完整的方法-

(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
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM