DEV Community

Cover image for A nice NestJs GraphQL init module
Andy Allison
Andy Allison

Posted on

A nice NestJs GraphQL init module

This is as much of a placeholder for myself as anything but this snippet gives you a nice graphql module for nestjs that has authentication provided via the context. It also handles error formatting and hides all the excess data if the platform is production

 GraphQLModule.forRoot({
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
      sortSchema: true,
      installSubscriptionHandlers: true,
      debug: false,
      context: ({ req }) => {
        return {
          request: req,
        };
      },
      formatError: (error) => {
        if (error.extensions.exception.status === 404)
          throw new NotFoundException();
        if (process.env.NODE_ENV === 'production')
          return { message: error.message };
        return error;
      },
    }),
Enter fullscreen mode Exit fullscreen mode

Top comments (0)