Search code examples
node.jsgraphqlapollographiql

GraphQL / Apollo: "No Schemas available". NodeJS


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 !


Solution

  • It's a little unclear what you were trying to accomplish, but you've added a middleware to your /graphql route that does nothing:

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

    The function you've inserted gets called anytime something hits the /graphql route, and because your function doesn't call next or end the response, the next middleware (graphqlExpress) is never called and the request just hangs.

    The other problem is that graphqlExpress requires bodyParser middleware to run before it's called. That means you can do either:

    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 }))
    

    If you fail to include bodyParser, graphqlExpress will typically complain and tell you as much as long you actually reach it in the first place.