Skip to main content

Set up a new nest app​

Install Dependencies​

Install your packages.
Be sure to install the correct ORM package!
Install extra dependencies for the example.

Generate the Module​

From the root of your project run:

Create the Entity​

From the root of your project run:
Now lets fill out the entity. Add the following to src/todo-item/todo-item.entity.ts.

Create the DTO​

The DTO (Data Transfer Object) is used by the resolver to represent incoming requests and outgoing responses. The DTO is where you can:
  • Define fields that should be rendered by graphql.
  • Define fields that should be filterable using the @FilterableField decorator.
  • Define validation that will be used by mutations.
In this example the DTO and entity are two different classes to clearly demonstrate what is required for graphql vs the persistence layer. However, you can combine the two into a single class. From the root of your project run:
Now lets fill out the DTO. Add the following to src/todo-item/todo-item.dto.ts.
todo-item/todo-item.dto.ts
Notice the use of @FilterableField this will let @ptc-org/nestjs-query-graphql know to allow filtering on the corresponding field. If you just use @Field then you will not be able to filter on the corresponding field.

Create the create DTO class.​

From the previously created DTO, @ptc-org/nestjs-query-graphql will automatically create a CreateTodoItem graphql type:
But in our case, the fields id, created and updated are actually not required when creating a TodoItem: they will be autogenerated. We only need to provide title and completed. To create a DTO that does not require these fields, we can create a custom create DTO:
todo-item/todo-item.create.dto.ts

Wire everything up.​

Update the todo-item.module to set up the NestjsQueryGraphQLModule and the entities to provide a QueryService. The NestjsQueryGraphQLModule will automatically create a Resolver that will expose the following queries and mutations: Queries
  • todoItems - find multiple TodoItems.
  • todoItem - find one TodoItem.
Mutations
  • createManyTodoItems - create multiple TodoItems.
  • createOneTodoItems - create one TodoItem.
  • updateManyTodoItems - update multiple TodoItems.
  • updateOneTodoItems - update one TodoItem.
  • deleteManyTodoItems - delete multiple TodoItemss.
  • deleteOneTodoItems - delete one TodoItem.
Next update app.module to set up your db connection and the graphql nest modules.
Create a compose.yml file in the root of the project

Running the Example​

Start the backing services
Start the app
Visit http://localhost:3000/graphql where you should see the playground Example playground

Exploring The GraphQL Endpoint​

Create a TodoItem​

Create Multiple TodoItems​

Query For Multiple TodoItems​

Query for all todo items​

Query for completed todo items​

Query For One TodoItem​

Query by id​

Update a TodoItem​

Lets update the completed TodoItem we created earlier to not be completed.

Update Multiple TodoItems​

Lets update the completed TodoItem we created earlier to not be completed.
You can check this by running the completed query from above.

Delete One TodoItem​

Lets update delete the first TodoItem.

Delete Many TodoItems​

Lets update delete the create many todo items TodoItem using a filter.