Skip to main content
  • @BeforeFindOne - invoked before any findOne query.
  • @BeforeQueryMany - invoked before any queryMany query.
  • @BeforeCreateOne - invoked before any createOne mutation.
  • @BeforeCreateMany - invoked before any createMany mutation.
  • @BeforeUpdateOne - invoked before any updateOne mutation.
  • @BeforeUpdateMany - invoked before any updateMany mutation.
  • @BeforeDeleteOne - invoked before any deleteOne mutation.
  • @BeforeDeleteMany - invoked before any deleteMany mutation.
In order to use a hook you only need to decorate your DTO with the corresponding decorator. Each hook decorator can be provided one of the following:
  • A hook function
  • A class that extends Hook, when using a class you can use DI to access other services just like guards, interceptors or pipes.
The graphql context by default only contains the incoming request!
If you provide a custom create or update DTO you can also decorate those classes with corresponding decorators.
All of the examples reference a GqlContext. This was defined for the sake of the example. It is recommended that you define a custom type that represents the information in the context based on the guards and interceptors used in your application.We have defined our GqlContext as

@BeforeCreateOne

The @BeforeCreateOne decorator can be used to modify incoming createOne mutations with information from the graphql context.

Hook Function

In this example we set the createdBy field based on the context.

Hook Class

You can also provide a custom Hook class that can leverage nestjs dependency injection. In this example we create a simple Hook that works with any type that has a createdBy property.
Now we just provide the hook to the BeforeCreateOne decorator.

@BeforeCreateMany

The @BeforeCreateMany decorator can be used to modify incoming createMany mutations with information from the graphql context.

Hook Function

In this example we set the createdBy field on each record based on the context.

Hook Class

You can also provide a custom Hook class that can leverage nestjs dependency injection. In this example we create a simple Hook that works with any type that has a createdBy property.
Now we just provide the hook to the BeforeCreateMany decorator.

@BeforeUpdateOne

The @BeforeUpdateOne decorator can be used to modify incoming updateOne mutations with information from the graphql context.

Hook Fnction

In this example we set the updatedBy field in the update.

Hook Class

You can also provide a custom Hook class that can leverage nestjs dependency injection. In this example we create a simple Hook that works with any type that has a updatedBy property.
Now we just provide the hook to the BeforeUpdateOne decorator.

@BeforeUpdateMany

The @BeforeUpdateMany decorator can be used to modify incoming updateMany mutations with information from the graphql context.

Hook Function

In this example we set the updatedBy field in the update.

Hook Class

You can also provide a custom Hook class that can leverage nestjs dependency injection. In this example we create a simple Hook that works with any type that has a updatedBy property.
Now we just provide the hook to the BeforeUpdateMany decorator.

Using Hooks In Custom Endpoints

You can also use hooks in custom endpoints by using the HookInterceptor along with
  • HookArgs - Used to apply hooks to any query endpoint.
  • MutationHookArgs - Used to apply hooks to any mutation that uses MutationArgsType

Example

In this example we’ll create an endpoint that marks all todo items that are currently not completed as completed. To start we’ll create our input types. There are two types that are created
  • The UpdateManyTodoItemsInput which extends the UpdateManyInputType this exposes an update and filter field just like the updateMany endpoints that are auto generated.
  • The UpdateManyTodoItemsArgs which extends MutationArgsType, this provides a uniform interface for all mutations ensuring that the argument provided to the mutation is named input.
todo-item/types.ts
Now we can use our new types in the resolver. todo-item/todo-item.resolver.ts
The first thing to notice is the
This interceptor adds the correct hook to the context to be used by the param decorator. There are a few things to take note of:
  • The HookTypes.BEFORE_UPDATE_MANY lets the interceptor know we are wanting the BeforeUpdateMany hook to be used for this mutation.
  • We use the TodoItemUpdateDTO, that is because the @BeforeUpdateMany decorator was put on the TodoItemUpdateDTO not the TodoItemDTO.
When using the HookInterceptor you must use the DTO that you added the hook decorator to.
In this example we bind the BEFORE_UPDATE_MANY hook, you can use any of the hooks available to bind to the correct one when creating, updating, or deleting records.
The next piece is the
By using the MutationHookArgs decorator we ensure that the hook is applied to the arguments adding any additional fields to the update. Finally we invoke the service updateMany with a filter that ensures we only update TodoItems that are completed, and add an setting completed to true to the update
Edit this page