@Relation- A relation that is a single value (one-to-one, many-to-one)@FilterableRelation- A@Relationthat enables filtering the parent by fields of the relationDTO.@UnPagedRelation- An array of relations (e.g, many-to-many, one-to-many) that returns all of the related records.@FilterableUnPagedRelation- An@UnPagedRelationthat enables filtering the parent by fields of the relationDTO.@OffsetConnection- A connection that represents a collection (e.g, many-to-many, one-to-many) that usesoffsetbased pagination.@FilterableOffsetConnection- An@OffsetConnectionthat enables filtering the parent by fields of the connectionDTO.@CursorConnection- A connection that represents a collection (e.g, many-to-many, one-to-many) that usescursorbased pagination.@FilterableCursorConnection- A@CursorConnectionthat enables filtering the parent by fields of the connectionDTO.
When loading relations a dataloader that is scoped to the request will be used. This
prevents the n+1 problem.
- TodoItemEntity
- SubTaskEntity
- Example
- Example
todo-item/todo-item.entity.ts
@Relation
A relation that is a single value (one-to-one, many-to-one)Example
Based on the entities defined above we can add atodoItem relation to the SubTask by creating the following SubTaskDTO with a @Relation decorator.
sub-task/sub-task.dto.ts
@Relation decorator lets nestjs-query know to expose the following endpoints:
subTask.todoItem- Aqueryto retrieve theSubTasksTodoItemsetTodoItemOnSubTask- Amutationto set theTodoItemon aSubTask.removeTodoItemFromSubTask- Amutationto remove aTodoItem/SubTaskrelation.- NOTE This does not typically remove either record just removes the relation.
NestjsQueryGraphQLModule.
sub-task/sub-task.module.ts
@ptc-org/nestjs-query-graphql will then automatically create the following graphql definition:
If
remove.enabled was set to true a removeTodoItemFromSubTask mutation would also be exposed with the same arguments as
setTodoItemOnSubTask.@FilterableRelation
The@FilterableRelation extends the @Relation decorator exposing the ability to filter the DTO that defines the relation by relation properties.
Example
In this example we’ll use the same Entities defined above to create a graphql endpoint that allows filteringSubTasks by TodoItems.
sub-task/sub-task.dto.ts
@FilterableRelation instead of @Relation, by using the @FilterableRelation version nestjs-query will allow filtering on the todoItem relation.
The module definition remains the same.
sub-task/sub-task.module.ts
SubTasks you can now also filter on todoItem properties.
In this example we’ll find all subTasks that are related to a todoItem with a title that starts with Created.
@UnPagedRelation
You can also use the@UnPagedRelation decorator to define a relation that does not use paging and returns an array of all the related records.
Example
Based on the entity definition above we can define aTodoItemDTO with a subTasks relation.
todo-item/todo-item.dto.ts
-
todoItem.subTasks- Aqueryendpoint to retrieve aTodoItemsSubTasks.- The
subTasksproperty will accept a query to allow you to filter, and sort results. - The
subTaskswill be returned as an array of results.
- The
-
addSubTasksToTodoItem- Amutationto addSubTasksto aTodoItem.
NestjsQueryGraphQLModule.
todo-item/todo-item.module.ts
TodoItem type like the following.
If
remove.enabled was set to true a removeSubTasksFromTodoItem mutation would also be exposed with the same arguments as
addSubTasksToTodoItem.@FilterableUnPagedRelation
The@FilterableUnPagedRelation extends the @UnPagedRelation decorator exposing the ability to filter the DTO that defines the relation by relation properties.
Example
In this example we’ll use the same Entities defined above to create a graphql endpoint that allows filteringTodoItems by SubTasks.
sub-task/sub-task.dto.ts
@FilterableUnPagedRelation instead of @UnPagedRelation, by using the @FilterableUnPagedRelation version nestjs-query will allow filtering on the subTasks relation.
The module definition remains the same.
sub-task/sub-task.module.ts
TodoItems you can now also filter on subTasks properties.
In this example we’ll find all todoItems that are related to a subTasks that are completed.
@OffsetConnection
Example
Based on the entity definitions above we can create aTodoItemDTO with a connection to the subTasks.
todo-item/todo-item.dto.ts
@OffsetConnection relation a couple of endpoints will automatically be generated. In this example the following are generated.
-
todoItem.subTasks- Aqueryto retrieve aTodoItemsSubTasks.- The
subTasksproperty will accept a query to allow you to filter, page and sort results. - The
subTasksproperty will return a offset based connection to page through results.
- The
-
addSubTasksToTodoItem- Amutationto addSubTasksto aTodoItem.
NestjsQueryGraphQLModule.
todo-item/todo-item.module.ts
TodoItem type like the following.
If
remove.enabled was set to true a removeSubTasksFromTodoItem mutation would also be exposed with the same arguments as
addSubTasksToTodoItem.Total Count Example
The
totalCount field is not eagerly fetched. It will only be executed if the field is queried from the client.@OffsetConnection decorator you can enable the totalCount field. The totalCount field will return the total number of records included in the connection.
todo-item/todo-item.dto.ts
TodoItemSubTasksConnection with a totalCount field.
@FilterableOffsetConnection
The@FilterableOffsetConnection extends the @OffsetConnection decorator exposing the ability to filter the DTO that defines the relation by relation properties.
Example
In this example we’ll use the same Entities defined above to create a graphql endpoint that allows filteringTodoItems by subTasks.
todo-item/todo-item.dto.ts
@FilterableOffsetConnection instead of @OffsetConnection, by using the @FilterableOffsetConnection version nestjs-query will allow filtering on the subTasks relation.
The module definition remains the same.
todo-item/todo-item.module.ts
TodoItems you can now also filter on subTasks properties.
In this example we’ll find all todoItems that have subTasks that are completed.
@CursorConnection
Example
Based on the entity definitions above we can create aTodoItemDTO with a connection to the subTasks.
todo-item/todo-item.dto.ts
@CursorConnection relation a couple of endpoints will automatically be generated. In this example the following are generated.
-
todoItem.subTasks- Aqueryto retrieve aTodoItemsSubTasks.- The
subTasksproperty will accept a query to allow you to filter, page and sort results. - The
subTasksproperty will return a cursor based connection to page through results.
- The
-
addSubTasksToTodoItem- Amutationto addSubTasksto aTodoItem.
NestjsQueryGraphQLModule.
todo-item/todo-item.module.ts
TodoItem type like the following.
If
remove.enabled was set to true a removeSubTasksFromTodoItem mutation would also be exposed with the same arguments as
addSubTasksToTodoItem.Total Count Example
The
totalCount field is not eagerly fetched. It will only be executed if the field is queried from the client.@CursorConnection decorator you can enable the totalCount field. The totalCount field will return the total number of records included in the connection.
todo-item/todo-item.dto.ts
TodoItemSubTasksConnection with a totalCount field.
@FilterableCursorConnection
The@FilterableCursorConnection extends the @CursorConnection decorator exposing the ability to filter the DTO that defines the relation by relation properties.
Example
In this example we’ll use the same Entities defined above to create a graphql endpoint that allows filteringTodoItems by subTasks.
todo-item/todo-item.dto.ts
@FilterableCursorConnection instead of @CursorConnection, by using the @FilterableCursorConnection version nestjs-query will allow filtering on the subTasks relation.
The module definition remains the same.
todo-item/todo-item.module.ts
TodoItems you can now also filter on subTasks properties.
In this example we’ll find all todoItems that have subTasks that are completed.
Virtual Relations
You may run into a case where you have avirtual relation that does not exist in the database. nestjs-query supports this through the RelationQueryService.
Options
The following options can be passed to all relation/connection decorators, to customize functionality.-
relationName- The name of the relation to use when looking up the relation from theQueryService -
nullable- Set totrueif the relation is nullable. -
complexity- Set to specify relation complexity. For more info see complexity docs -
disableRead- Set totrueto disable read operations. -
updateenabled- Set totrueto enable update operations.description- The description of the update operation.complexity- Set to specify relation complexity. For more info see complexity docsdecorators=[]- An array of customPropertyDecoratororMethodDecoratorsto add to the endpoint.
-
removeenabled- Set totrueto enable remove operations.description- The description of the remove operation.complexity- Set to specify relation complexity. For more info see complexity docsdecorators=[]- An array of customPropertyDecoratororMethodDecoratorsto add to the endpoint.
-
allowFiltering- Set totrueto allow filtering on the relation. -
guards=[]- An array of guards to add toupdateandremoveendpoints. -
interceptors=[]- An array of interceptors to add toupdateandremoveendpoints. -
pipes=[]- An array of pipes to add toupdateandremoveendpoints. -
filters=[]- An array of filters to add toupdateandremoveendpoints.
guards, pipes, interceptors and filters will not work by default with relation endpoints. See
https://docs.nestjs.com/graphql/tooling#execute-enhancers-at-the-field-resolver-levelCustom Relation Name
Sometimes you may want to expose a relation that has a different name when persisted from the graphql property. To do this use therelationName property.
- Relation
- CursorConnection
- OffsetConnection
- UnPagedRelation
GraphQL lookahead
When you want to optimze your database queries you can setenableLookAhead, this will join and select the relation when it’s being fetched in the query.
- Relation
Disable Reads
To disable theread queries you can set the disableRead option to true.
- Relation
- CursorConnection
- OffsetConnection
- UnPagedRelation
Disable filter or sorting in relations
To disable the filter or sorting of relations you can set the disableFilter or/and disableSort option to true.
- Relation
- CursorConnection
- OffsetConnection
- UnPagedRelation
This is not available in
relation as it will only fetch one record.Enable Updates
To enable theupdate mutations you can set the update.enabled option to true.
- Relation
- CursorConnection
- OffsetConnection
- UnPagedRelation
Enable Removes
To enable theremove mutations you can set the remove.enabled option to true.
- Relation
- CursorConnection
- OffsetConnection
- UnPagedRelation
Guards, Pipes and Filters
NOTEguards, pipes, interceptors and filters will not work by default with read endpoints. See https://github.com/nestjs/graphql/issues/295
In this example we’ll just demonstrate using a guard but the same pattern applies for pipes, filters and interceptors
To set up a guard for endpoint you can use the guards option.
Assume we have the following auth guard that checks for a certain header and value.
auth.guard.ts
- Relation
- CursorConnection
- OffsetConnection
- UnPagedRelation
update or remove endpoints will require the guard.
Relation Mixin
If you are using the resolvers individually you can use the following mixins to add relations functionality.Relatable
When using TheRelatable mixin adds all relations functionality to a resolver.
In this example we expose on read endpoints for todo items with the relations defined on the TodoItemDTO.
todo-item/todo-item.resolver.ts