0

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
  • The question is absolutely unclear, the two code snippets do two totally different things. There's one thing missing in your NestJS code for sure, I'll add an an answer for that though. Commented Sep 12, 2022 at 8:22

1 Answer 1

1

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' });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.