How to make GraphQL enum data in resolver with nestjs/graphql?

Viewed 18

In this way, it can pass enum data in resolver:

    enum AuthType {
     GOOGLE =  'google-auth',
     GITHUB =  'github-auth',
     OUTLOOK = 'outlook-auth',
    }

    interface UsersArgs {
      first: number,
      from?: string,
      status?: String,
      authType?: AuthType,
    }

    export const resolvers = {
      AuthType,
      Query: {
        users: (_record: never, args: UsersArgs, _context: never) {
          // args.authType will always be 'google-auth' or  'github-auth' or 'outlook-auth'
          // ...
        }
      }
    }

There is also good example for pure GraphQL syntax as:

https://www.graphql-tools.com/docs/scalars#internal-values

In NestJS, the code like

    import { Args, Query, Resolver } from '@nestjs/graphql';

    import { AuthType } from '@enum/authEnum';

    @Resolver()
    export class AuthResolver {
      constructor(private readonly authRepo: AbstractAuthSettingRepository) {}
      
      @Query(() => AuthSetting)
      findAuth(
        @Args('input')
        id: string,
      ): Promise<AuthSetting | undefined> {
        return this.authRepo.findOne({ id });
      }
    }

How can I use AuthType in the AuthResolver class?

1 Answers

In order to be able to use enums in NestJS GraphQL, you need to register them once:

import { registerEnumType } from '@nestjs/graphql';
import { AuthType } from '@enum/authEnum';

registerEnumType(AuthType, { name: 'AuthType' });
Related