【问题标题】:Export graphql schema using es5使用 es5 导出 graphql 架构
【发布时间】:2018-09-13 07:21:17
【问题描述】:

我正在编写一个使用 graphql 运行 Express 服务器的脚本。我正在使用 ES5。

这是我的 server.js 代码(用于运行 Express 服务器):

const express = require('express');
const cors = require('cors');

const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');

const schemaTest = require('./schemas/schema');

const app = express();
app.listen(4000, () => { console.log("Listening on 4000")});

app.use('/graphql', bodyParser.json(), graphqlExpress({schemaTest})); 
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));

这是我的 schema.js 的代码

const {makeExecutableSchema, addMockFunctionsToSchema} = require('graphql-tools');

const typeDefs = `type Query {
  greeting: String
}
`;

const schema = makeExecutableSchema({typeDefs});
addMockFunctionsToSchema({ schema });

module.exports = schema;

但是我得到了这个问题:

错误:预期未定义为 GraphQL 架构。

我无法找到我的错误在哪里。

请注意,如果我将我的 schema.js 代码复制粘贴到 server.js 文件中,它可以正常工作,就像我没有正确导入(或导出)架构文件一样。

我的错误在哪里

【问题讨论】:

    标签: ecmascript-6 graphql ecmascript-5 apollo


    【解决方案1】:

    graphqlExpress 期望将配置对象传递给它,该对象的属性之一是schema。所以你的代码应该是这样的:

    app.use('/graphql', bodyParser.json(), graphqlExpress({
      schema: schemaTest,
    }));
    

    您目前正在做的是传入一个具有schemaTest 属性但没有schema 属性的对象。

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 2017-10-18
      • 2017-05-11
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2020-11-26
      • 2018-12-17
      • 2017-06-12
      相关资源
      最近更新 更多