NestJs GraphQL playground access

Viewed 5337

I can't seem to access the GraphQL Playground using NestJS. I'm exploring the documentation and have followed this https://docs.nestjs.com/graphql/quick-start up to the Resolvers section to generate the schema.gql, but attempting to reach localhost:3000/graphql is not able to connect.

At first I thought my code was setup incorrectly, but I spent some time digging into Nest's examples and found that those also do not work when trying to access the /graphql endpoint. It does work if I setup a get endpoint to return a JSON body using the REST method.

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { RecipesModule } from './recipes/recipes.module';

@Module({
  imports: [
    RecipesModule,
    GraphQLModule.forRoot({
      installSubscriptionHandlers: true,
      autoSchemaFile: 'schema.gql',
    }),
  ],
})
export class AppModule {}

This is directly from the NestJS example. My understanding is that the GraphQLModule should be setting up the connection to the /graphql endpoint. Following the docs, graphql, apollo-server-express, and graphql-tools were all installed.

Any idea why the graphql route is not connecting?

[Edit]: Things I've tried so far:

  • setting playground: true explicitly with GraphQLModule.forRoot
  • verified NODE_ENV is not 'production'
  • confirmed server works when creating resolvers using REST
  • curl'd localhost:3000/graphql and receive a graphql validation error, so confirm that connects correctly
3 Answers

sometimes helmet causes this same issue. if you have helmet loaded as a middleware, it might probably also cause this.

I encountered a similar issue this is what I did. I hope you will find it helpful. I appreciated everyone's support.

Step1: // app.module.ts

  imports: [
    UsersModule,
    GraphQLModule.forRoot({
      // autoSchemaFile: true,    did not work!
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
      // schema.gql will automatically be created
      debug: true,
      playground: true,
    }),
  ],
  providers: [AppResolver],   // all resolvers & service should be in providers

step 2:

Make sure you have one at least query bcz Mutation is not enough if you don't GraphQL playground will not start. Read more here

Related