Skip to content

Function: Context()

ts
function Context(property?): ParameterDecorator;

Defined in: packages/apollo/src/decorators/ctx.decorator.ts:39

Decorator for the GraphQL context. Provides access to the GraphQL request context.

Parameters

ParameterTypeDescription
property?stringOptional property name to extract from context.

Returns

ParameterDecorator

Example

typescript
@Query()
async me(@Context() ctx: MyContext) {
  return ctx.currentUser;
}

@Query()
async users(@Context() ctx: GraphQLContext) {
  return this.userService.findAll(ctx.tenantId);
}

@Mutation()
async createPost(
  @Args('input') input: CreatePostInput,
  @Context() ctx: MyContext
) {
  return this.postService.create(input, ctx.userId);
}

Released under the MIT License.