IDE
One of the most popular GraphQL in-browser IDE is called GraphiQL.
To use a GraphiQL with your application, you need to set up a middleware.
This particular middleware comes with apollo-server-express package that we had to install already.
Its name is a graphiqlExpress().
In order to set up a middleware, we need to open an app.module.ts file once again:
JavaScript
TypeScript
TypeScript
import {
Module,
MiddlewaresConsumer,
NestModule,
RequestMethod,
} from '@nestjs/common';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { GraphQLModule, GraphQLFactory } from '@nestjs/graphql';
@Module({
imports: [GraphQLModule],
})
export class ApplicationModule implements NestModule {
constructor(private readonly graphQLFactory: GraphQLFactory) {}
configure(consumer: MiddlewaresConsumer) {
const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
const schema = this.graphQLFactory.createSchema({ typeDefs });
consumer
.apply(graphiqlExpress({ endpointURL: '/graphql' }))
.forRoutes({ path: '/graphiql', method: RequestMethod.GET })
.apply(graphqlExpress(req => ({ schema, rootValue: req })))
.forRoutes({ path: '/graphql', method: RequestMethod.ALL });
}
}
TypeScript
import { Module, RequestMethod } from '@nestjs/common';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { GraphQLModule, GraphQLFactory } from '@nestjs/graphql';
@Dependencies(GraphQLFactory)
@Module({
imports: [GraphQLModule],
})
export class ApplicationModule {
constructor(graphQLFactory) {
this.graphQLFactory = graphQLFactory;
}
configure(consumer) {
const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
const schema = this.graphQLFactory.createSchema({ typeDefs });
consumer
.apply(graphiqlExpress({ endpointURL: '/graphql' }))
.forRoutes({ path: '/graphiql', method: RequestMethod.GET })
.apply(graphqlExpress(req => ({ schema, rootValue: req })))
.forRoutes({ path: '/graphql', method: RequestMethod.ALL });
}
}
Hint The graphiqlExpress() offers few other options, read more about them here.
Now, when you navigate to the http://localhost:PORT/graphiql you should see a graphical interactive GraphiQL IDE.