【问题标题】:GraphQL / Apollo: "No Schemas available". NodeJSGraphQL / Apollo:“没有可用的模式”。节点JS
【发布时间】:2018-10-16 08:15:01
【问题描述】:

我正在尝试使用 GraphQL/Apollo,而我的“文档资源管理器”无限加载并且不显示任何内容,而且我无法进行任何查询。

几分钟后,我收到了一个 typeError "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 都清楚。有人可以解释一下,怎么了?谢谢!

【问题讨论】:

    标签: node.js graphql apollo graphiql


    【解决方案1】:

    有点不清楚你想要完成什么,但你已经在 /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 通常会抱怨并告诉您,只要您实际上首先到达它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 2019-04-22
      • 2021-11-28
      • 2020-08-28
      • 2020-06-28
      • 2019-05-10
      • 2018-03-15
      • 2018-04-23
      相关资源
      最近更新 更多