gqlContextType
gqlContextType
annotates a method to indicate that it needs the RequestContext and the generated GraphQL code should pass it down to the method.
It's required to be the first argument to the method. It can be used in custom queries or mutations.
For example:
src/graphql/mutations/auth.ts
export class AuthResolver {
@gqlMutation({
class: "AuthResolver",
name: "userAuth",
type: UserAuthPayload,
args: [
gqlContextType(),
{
name: 'input',
type: 'UserAuthInput',
},
],
})
async userAuth(
context: RequestContext,
input: UserAuthInput,
): Promise<UserAuthPayload> {
return new UserAuthPayload("1");
}
}
and the generated code looks like:
src/graphql/mutations/generated/user_auth_type.ts
export const UserAuthType: GraphQLFieldConfig<
undefined,
RequestContext,
{ [input: string]: UserAuthInput }
> = {
// ...
resolve: async (
_source,
{ input },
context: RequestContext,
_info: GraphQLResolveInfo,
): Promise<UserAuthPayload> => {
const r = new AuthResolver();
return r.userAuth(context, {
emailAddress: input.emailAddress,
password: input.password,
});
},
};