【问题标题】:GraphQL: Error: Must provide DocumentGraphQL:错误:必须提供文档
【发布时间】:2018-04-01 15:11:46
【问题描述】:

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

预期结果

使用来自https://github.com/apollographql/apollo-tutorial-kit 的以下 sn-p 启动 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 的任何实现中,使用文档、使用 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

【问题讨论】:

  • 4 个月前,我认为阿波罗已经很老了,一直在变化
  • 入门套件工作正常。这听起来像是您在响应您提出的请求时遇到的错误。通常,当请求正文格式错误或缺少 Content-Type: application/json 标头时会引发该错误。您应该更新您的问题,以包括您如何尝试向服务器发出请求。
  • 清除我的 npm 缓存 && node_modules 后,我让它工作了!谢谢大家

标签: javascript graphql apollo graphql-js apollo-server


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    我正在使用 JQuery Ajax 对 graphql 服务器 (apollo-server-express) 进行 HTTP 发布请求,并在查询突变时收到此错误。原因与spencer.sm 所描述的相同。为了帮助其他谷歌人,我在这里粘贴完整的方法-

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

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 2019-01-11
      • 2017-01-09
      • 2019-10-21
      • 2020-04-23
      • 2019-10-30
      • 1970-01-01
      • 2018-01-13
      • 2019-06-09
      相关资源
      最近更新 更多