使用 es5 导出 graphql 模式

Export graphql schema using es5

我正在编写一个 运行 带有 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;

但是我得到了这个问题:

Error: Expected undefined to be a GraphQL schema.

我找不到我的错误在哪里。

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

我的错误在哪里

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

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

您目前正在做的是传入一个带有 schemaTest 属性 但没有 schema 属性.

的对象