@BeforeFindOne- invoked before anyfindOnequery.@BeforeQueryMany- invoked before anyqueryManyquery.@BeforeCreateOne- invoked before anycreateOnemutation.@BeforeCreateMany- invoked before anycreateManymutation.@BeforeUpdateOne- invoked before anyupdateOnemutation.@BeforeUpdateMany- invoked before anyupdateManymutation.@BeforeDeleteOne- invoked before anydeleteOnemutation.@BeforeDeleteMany- invoked before anydeleteManymutation.
- A hook function
- A class that extends
Hook, when using a class you can use DI to access other services just likeguards,interceptorsorpipes.
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 customHook 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.
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 customHook 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.
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 customHook 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.
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 customHook 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.
BeforeUpdateMany decorator.
Using Hooks In Custom Endpoints
You can also use hooks in custom endpoints by using theHookInterceptor along with
HookArgs- Used to apply hooks to any query endpoint.MutationHookArgs- Used to apply hooks to anymutationthat usesMutationArgsType
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
UpdateManyTodoItemsInputwhich extends theUpdateManyInputTypethis exposes anupdateandfilterfield just like theupdateManyendpoints that are auto generated. - The
UpdateManyTodoItemsArgswhich extendsMutationArgsType, this provides a uniform interface for all mutations ensuring that the argument provided to the mutation is namedinput.
context to be used by the param decorator.
There are a few things to take note of:
- The
HookTypes.BEFORE_UPDATE_MANYlets the interceptor know we are wanting the BeforeUpdateMany hook to be used for this mutation. - We use the
TodoItemUpdateDTO, that is because the@BeforeUpdateManydecorator was put on theTodoItemUpdateDTOnot theTodoItemDTO.
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.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