I've been following along with the code first approach to create a GraphQL mutation for basic auth as documented on: https://docs.nestjs.com/graphql/mutations
This is what I have so far...
// login-input.dto.ts
@InputType('LoginInput')
export class LoginInputDTO {
@Field()
@IsEmail()
email!: string
@Field(() => String)
@IsString()
password!: string
}
// login-response.dto.ts
@ObjectType('LoginResponse')
export class LoginResponseDTO {
@Field()
accessToken!: string
}
// auth.resolver.ts
@Resolver()
export class AuthResolver {
@Mutation(() => LoginResponseDTO)
async login(@Args('input') input: LoginInputDTO): Promise<LoginResponseDTO> {
console.log(input)
}
}
However, input is always undefined. I stumbled across this post: https://stackoverflow.com/questions/69165638/graphql-mutation-and-query-cannot-read-property-of-undefined
So I updated my mutation signature to be like this:
// auth.resolver.ts
type LoginInputType = {
input: LoginInputDTO
}
@Resolver()
export class AuthResolver {
@Mutation(() => LoginResponseDTO)
async login(_: any, @Args('input', { type: () => LoginInputDTO }) { input }: LoginInputType): Promise<LoginResponseDTO> {
console.log(input)
}
}
This now works, but I'm confused as to why the example from the documentation didn't work for me. I'm not happy with the extra LoginInputType type that I need to create, so any help to understand how I can get the documented code to work would be much appreciated!