首页 文章

GraphQL / Apollo:“没有架构可用” . 的NodeJS

提问于
浏览
0

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

enter image description here

几分钟后,我得到一个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都很清楚 . 有人能解释一下,出了什么问题?谢谢 !

1 回答

  • 1

    它's a little unclear what you were trying to accomplish, but you'已经为您的 /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通常会抱怨并告诉你,只要你真正到达它的时间很长 .

相关问题