DEV Community

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

Posted on

2 1

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)