Raigato
Raigato

Reputation: 171

Apollo Server Express - Playground cannot be reached

I am trying to follow this tutorial https://www.youtube.com/watch?v=I6ypD7qv3Z8&t=48972s but I am stuck on trying to make the playground work.

I get to the playground on "http://localhost:4000/graphql" but somehow I get the "Server cannot be reached" error. In the network inspector I see "Cannot POST /" 404s.

Here's my code (app.ts):

import { ApolloServer } from "apollo-server-express";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
import { buildSchema } from "type-graphql";
import { PORT } from "./constants";
import { HelloResolver } from "./graphql/resolvers";

export const main = async () => {
  const app = express();

  const apolloServer = new ApolloServer({
    schema: await buildSchema({ resolvers: [HelloResolver], validate: false }),
    plugins: [ApolloServerPluginLandingPageGraphQLPlayground],
  });
  await apolloServer.start();

  apolloServer.applyMiddleware({ app });

  app.listen(PORT, () => {
    console.log(
      `Server started on http://localhost:${PORT}${apolloServer.graphqlPath}`
    );
  });
};

Things I did extra from the tut:

Things I tried:

Any idea on where could be the issue? Seems like express didn't create the POST endpoint for the /graphql route.

EDIT: It works if I change this: apolloServer.applyMiddleware({ app }); to this: apolloServer.applyMiddleware({ app, path: "/" });

Upvotes: 2

Views: 7821

Answers (4)

nassqra
nassqra

Reputation: 403

Try setting your NODE_ENV to anything other than test or production before running your server

Upvotes: 0

Shah Aayush
Shah Aayush

Reputation: 1

I recently had this issue too. This was the case where the

Server cannot be reached/no visible schema but still able to execute a query

To fix this you should have this as in your ApolloServer initializer:

const server = new ApolloServer({
  csrfPrevention: true,
  plugins: [
     ApolloServerPluginLandingPageGraphQLPlayground(),
  ],
  instrospection: true,
})

Read more about instrospection here and this github issue.

Upvotes: 0

BaharaJr
BaharaJr

Reputation: 73

Try using 127.0.0.1 instead of localhost. It worked for me. Also If you have cached queries and mutations that you still want to use, switching back to localhost from 127.0.0.1 will have the localhost working again.

Upvotes: 0

MOHAMMED AWAL
MOHAMMED AWAL

Reputation: 49

I found the answer here! helpful. Do check it out!

Upvotes: 2

Related Questions