0

I`m trying to work with GraphQL/Apollo, and my "Documentation Explorer" loading infinitely and doesnt show anything, and i can't make any queries.

enter image description here

After few minutes I getting an typeError "Failed to fetch".

Here's my graphql/index.js file:

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 and DevTools both clear. Can someone explain, what`s wrong? Thank you !

4

1 回答 1

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 通常会抱怨并告诉您,只要您实际上首先到达它。

于 2018-05-06T12:35:46.563 回答