GraphQL/阿波罗:"No Schemas available"。节点JS

GraphQL / Apollo: "No Schemas available". NodeJS

我正在尝试使用 GraphQL/Apollo,但我的 "Documentation Explorer" 无限加载并且没有显示任何内容,而且我无法进行任何查询。

几分钟后,我收到类型错误 "Failed to fetch"

这是我的 graphql/index.js 文件:

const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const User = require('../models/user.model');

const typeDefs = `

  type Query {
    users: [User]
  }

  type User {
    id: ID!
    name: String
    email: String
    password: String
  }

`;

const resolvers = {
  Query: {
    users() {
      return User.find({});
    }
  }
}

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

module.exports = (app) => {
  app.use('/graphql', () => { }, graphqlExpress({ schema }));

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

Console 和 DevTools 都清除了。谁能解释一下,怎么了?谢谢!

有点不清楚您想要完成什么,但是您已经在 /graphql 路由中添加了一个什么都不做的中间件:

app.use('/graphql', () => { }, graphqlExpress({ schema }))

您插入的函数会在任何时候调用 /graphql 路由,并且由于您的函数不调用 next 或结束响应,下一个中间件 (graphqlExpress) 从未被调用,请求只是挂起。

另一个问题是 graphqlExpress 在调用之前 运行 需要 bodyParser 中间件。这意味着您可以执行以下任一操作:

const bodyParser = require('body-parser')

// Option A -- body parser will run prior to all routes after this point
app.use(bodyParser.json())

// Option B -- body parser will only run for the graphql route
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }))

如果您未能包含 bodyParser,graphqlExpress 通常会抱怨并告诉您尽可能多的时间,只要您首先实际达到它。