NestJS/GraphQL/Local Auth - How to define variables passed in

Viewed 33

What is the standard approach to defining variables to be used only in guards/interceptors?

Resolver

  • Without the @Args() it throws an error when I send a mutation with variables
  • With the @Args(), it has an eslint error createUserInput' is defined but never used
@UseGuards(GqlLocalAuthGuard)
@Mutation(() => User)
async login(
  // @Args("createUserInput") createUserInput: CreateUserInput, 
  // ^ it works if I put this
): Promise<User> {
  return { id: "1", email: "fake@gmail.com" };
}

GqlLocalAuthGuard

  • This extends local auth guard and the strategy verifies that the user put in the correct password
export class GqlLocalAuthGuard extends AuthGuard("local") {
  getRequest(context: ExecutionContext): any {
    const ctx = GqlExecutionContext.create(context);
    const req = ctx.getContext().req;
    const { createUserInput } = ctx.getArgs<any>();
    req.body = createUserInput;
    return req;
  }
}

Error without Args()

"GraphQLError: Unknown argument \"createUserInput\" on field \"Mutation.login\"."

Mutation

mutation ($createUserInput: CreateUserInput!) {
  login (createUserInput: $createUserInput) {
    id,
    email,
  } 
}
0 Answers
Related