Defining the Resolver
Auto Generated Resolver
The easiest way to get started is to use the@NestjsQueryGraphQLModule. The NestjsQueryGraphQLModule will automatically create a CRUDResolver for you.
- TypeOrm
- Sequelize
- Mongoose
todo-item.module.ts
CRUDResolver
If you want to override auto generated queries or mutations you can use theCRUDResolver to manually define your resolver.
Resolvers work the same as they do in @nestjs/graphql by annotating your class with @Resolver.
In this example the DTO and entity are the same shape, if you have a case where they are different or have computed fields check
out Assemblers to understand how to convert to and from the DTO/Entity.
todo-item.resolver.ts
NestjsQueryGraphQLModule.
Notice how the dtos property is specified instead of the resolvers, this allows you to specify your DTOClass, CreateDTOClass, and UpdateDTOClass without creating an auto-generated resolver.
- TypeOrm
- Sequelize
- Mongoose
todo-item.module.ts
Generated Endpoints
When using the auto-generated resolver or extendingCRUDResolver the methods that will be exposed for the TodoItemDTO are:
todoItem- Find a singleTodoItemby id.todoItems- Filter, sort, and pageTodoItemsexportTodoItems- Export filteredTodoItemsas CSV, only whenexport.enabledistrue. The query name followsexport<PluralName>.createOneTodoItem- Create a singleTodoItemcreateManyTodoItems- Create multipleTodoItems.updateOneTodoItem- Update a singleTodoItemby id.updateManyTodoItems- Update multipleTodoItemsusing a filter.deleteOneTodoItem- Delete a singleTodoItemby id.deleteManyTodoItems- Delete multipleTodoItemsusing a filter.
Options
When usingNestjsQueryGraphQLModule or CRUDResolver you can define a number of options to control your endpoints.
-
CreateDTOClass- The input DTO to use for create mutations. See Create and Update DTOs -
UpdateDTOClass- The input DTO to use for update mutations. See Create and Update DTOs -
enableSubscriptions?- Set totrueto enable graphql subscriptions. See Subscriptions. -
enableAggregate?- When set to true an aggregate query will be enabled on the type and all relations (unless the explicitly disable it). See Aggregation -
create- In addition toResolverOptionsyou can also specify the followingCreateDTOClass- The input DTO to use for create mutations.CreateOneInput- TheInputTypeto use for create one mutations.CreateManyInput- TheInputTypeto use for create many mutations.
-
read- In addition toResolverOptionsyou can also specify the followingQueryArgs- Specify to override the auto-generatedArgsTypeto use to filter records inqueryManyendpoint.
-
update- In addition toResolverOptionsyou can also specify the followingUpdateDTOClass- The input DTO to use for update mutations.UpdateOneInput- TheInputTypeto use for update one mutations.UpdateManyInput- TheInputTypeto use for update many mutations.
-
delete- In addition toResolverOptionsyou can also specify the followingDeleteOneInput- TheInputTypeto use for delete one mutations.DeleteManyInput- TheInputTypeto use for delete many mutations.
-
aggregate- In addition toResolverOptionsyou can also specify the followingenabled- Set to true to enable aggregations. If this is used in place of the rootenableAggregateoption relations will not have aggregate queries exposed.
-
export- Configure CSV export. See CSV Export.enabled- Export is disabled by default. This must be explicitly set totrueto add the export query to the GraphQL schema.limit- Set the maximum number of exported records. Defaults to1000. Clients can request fewer records with the query’spaging.limitargument and select a page withpaging.offset. The requested limit is capped at this maximum; additional records are omitted without an error or truncation indicator.ExportDTOClass- Use a separate@ObjectType()DTO whose GraphQL fields define the allowed export columns, including inherited fields. Supportsclass-transformertransformations without requiring@Expose().- Select CSV columns and headers using the query’s required
fieldsargument.
ResolverOptions
The create, read, update, delete, and export options above all accept the following options.
-
dtoName- Set to override the default name (the name passed to@ObjectTypeor the name of the class). -
disabled=false- Set to true to disable all endpoints. -
guards=[]- An array of guards to add to all endpoints. -
interceptors=[]- An array of interceptors to add to all endpoints. -
pipes=[]- An array of pipes to add to all endpoints. -
filters=[]- An array of filters to add to all endpoints. -
decorators=[]- An array of customPropertyDecoratororMethodDecoratorsto add to the endpoint. -
enableSubscriptions?- Set totrueto enable graphql subscriptions. See Subscriptions. -
one,many- Both theoneandmanyaccept the following options:name?- Override the endpoint name.disabled=false- Set to true to disable the endpoint.enableSubscriptions?- Set totrueto enable graphql subscriptions. See Subscriptions.guards=[]- An array of guards to add to the endpoint.interceptors=[]- An array of interceptors to add to the endpoint.pipes=[]- An array of pipes to add to the endpoint.filters=[]- An array of filters to add to the endpoint.decorators=[]- An array of customPropertyDecoratororMethodDecoratorsto add to the endpoint.
Examples
Create and Update DTOs.
There may be times when you want to specify certain validation or only allow certain fields when updating or creating records. To allow for this you can specify theCreateDTOClass and UpdateDTOClass options.
Example
In this example we’ll create a newTodoItemInputDTO that adds validation and limits the fields you can modify.
Assume we have the following class todo-item.input.ts which omits the id, created, and updated fields from the original DTO.
todo-item.input.ts
TodoItemInputDTO
- NestjsQueryGraphQLModule
- CRUDResolver
todo-item.module.ts
Disabling Endpoints.
There may be scenarios where you wish to disable certain methods. Using the options describe above we can disable different actions. In this example we disable allcreate endpoints
- NestjsQueryGraphQLModule
- CRUDResolver
todo-item.module.ts
many mutations. This will prevent createManyTodoItems, updateManyTodoItems, deleteManyTodoItems from being exposed in the graphql schema.
NOTE The same pattern applies for disabling the one endpoints.
- NestjsQueryGraphQLModule
- CRUDResolver
todo-item.module.ts
Guards, Pipes, Filters, and Interceptors
In this section we’ll just demonstrate using aguard 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
- NestjsQueryGraphQLModule
- CRUDResolver
todo-item.module.ts
create, update or delete method will require the guard.
You can also apply to individual methods using the one and many fields. For example lets put a guard on all many mutations.
- NestjsQueryGraphQLModule
- CRUDResolver
todo-item.module.ts
Override Endpoint Name
If you find yourself in a situation where you want to override an endpoint name you can use theone.name or many.name options to override
These options are available for the
create, read, update, and delete endpoints.todoItem query to findTodoItem and the todoItems endpoint to queryForTodoItems.
- NestjsQueryGraphQLModule
- CRUDResolver
todo-item.module.ts
Individual Resolvers
The@ptc-org/nestjs-query-graphql package exposes each part of CRUD into individual mixins and resolvers allowing you to pick and choose what functionality you want to expose.
This is advanced usage of the resolvers API and is subject to change!
- All examples below can be achieved with resolver options.
- The following resolvers do not expose relations options, to add relations options see Relateable
CreateResolver
The CreateResolver will only expose the createOne and createMany endpoints. The options described for create can be passed to the CreateResolver
For example the following resolver will expose the createOneTodoItem and createManyTodoItems mutations.
todo-item.resolver.ts
ReadResolver
The ReadResolver will only expose the query and findById endpoints. The options described for read can be passed to the ReadResolver
For example the following resolver will expose the todoItems and todoItem queries.
todo-item.resolver.ts
UpdateResolver
The UpdateResolver will only expose the updateOne and updateMany endpoints. The options described for update can be passed to the UpdateResolver
For example the following resolver will expose the updateOneTodoItem and updateManyTodoItems mutations.
todo-item.resolver.ts
DeleteResolver
The DeleteResolver will only expose the deleteOne and deleteMany endpoints. The options described for delete can be passed to the DeleteResolver
For example the following resolver will expose the deleteOneTodoItem and deleteManyTodoItems mutations.
todo-item.resolver.ts
Custom Endpoints
You can also create custom methods.Unless you are overriding an endpoint you DO NOT need to extend the crud resolver directly, instead you can create a new
resolver for your type and add the new endpoint.
@nestjs/graphql will handle merging the two resolver into one.TodoItems.
First create a file named types.ts. And add the following.
types.ts
TodoItemConnection and TodoItemQuery. Because of the way @nestjs/graphql and nest work we need to extend the QueryArgsType so that it will know the type to serialize into.
In your resolver you can now create a new completedTodoItems method with the following:
todo-item.resolver.ts
TodoItemQuery we created for the arguments type and annotate with @Args when you look at the generated schema in it will look like.
query arg but instead you see the fields of TodoItemQuery, that is because we used @Args without a name and added the @ArgsType decorator to the TodoItemQuery.
The next piece is
filter and add completed: { is: true }. This will override any completed arguments that an end user may have provided to ensure we always query for completed todos.
Finally we create our connection response by using the createFromPromise method on the connection.
NestjsQueryGraphQLModule we will get both the auto generated resolver along with the custom endpoints from the custom resolver.
todo-items.module.ts