Skip to main content

Types

@nestjs-query/query-graphql provides a number of types. Most will be automatically generated for you when you create a resolver. However, you can use many of the types in your own code for custom endpoints.

The following examples are based on the following TodoItemDTO

import { FilterableField, IDField } from '@nestjs-query/query-graphql';import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';
@ObjectType('TodoItem')export class TodoItemDTO {  @IDField(() => ID)  id!: string;
  @FilterableField()  title!: string;
  @FilterableField()  completed!: boolean;
  @FilterableField(() => GraphQLISODateTime)  created!: Date;
  @FilterableField(() => GraphQLISODateTime)  updated!: Date;}

ArgsTypes#

QueryArgsType#

Args type used in read endpoints to allow filtering, paging, and sorting.

The QueryArgsType will generate an ArgsType that will require the user to provide three arguments.

  • filter? - A filter that should be used to find records to update. See FilterType
  • paging? - Options to page of result.
  • sorting? - Optional array to allow sorting of results. See SortType.

Usage#

import { QueryArgsType } from '@nestjs-query/query-graphql';import { ArgsType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@ArgsType()export class TodoItemQuery extends QueryArgsType(TodoItemDTO) {}

ObjectTypes#

ConnectionType#

Implementation of connections. Useful for large result sets where the end user should be able to page through the results.

Usage#

import { QueryArgsType } from '@nestjs-query/query-graphql';import { TodoItemDTO } from './dto/todo-item.dto';
export const TodoItemQueryArgs = QueryArgsType(TodoItemDTO)export const TodoItemConnection = TodoItemQueryArgs.ConnectionType;

UpdateManyResponseType#

Response from updateMany mutations.

Usage#

import { UpdateManyResponseType } from '@nestjs-query/query-graphql';
export const UpdateManyResponse = UpdateManyResponseType()

DeleteManyResponseType#

Response from deleteMany mutations.

Usage#

import { DeleteManyResponseType } from '@nestjs-query/query-graphql';
export const UpdateManyResponse = DeleteManyResponseType()

InputTypes#

FilterType#

GraphQL implementation of the @nestjs-query/core Filter type.

The FilterType is dynamically created based on the fields in the DTO annotated with @FilterableField.

Along with the dynamically create fields filter also has the following static fields:

  • and - Allows for joining multiple Filters with using an AND condition.

    For example, find all todo items that start with 'Foo' AND end in 'Bar'.

    todoItems(filter: {  and: [    {title: {like: "Foo%"}},    {title: {like: "%Bar"}},  ]}) {  #...select your fields}
  • or - Allows for joining multiple Filters using an OR condition.

    For example, find all todo items that (are completed AND start with 'Foo') OR (are not completed and end in 'Bar').

    todoItems(filter: {  or: [    {title: {eq: "Foo"}, completed: {is: true}},    {title: {eq: "Bar"}, completed: {is: false}},  ]}) {  #...select your fields}
import { FilterType } from '@nestjs-query/query-graphql';import { TodoItemDTO } from './dto/todo-item.dto';
export const TodoItemFilter = FilterType(TodoItemDTO)

SortType#

The SortType allows you to sort results.

NOTE This type is automatically created when using QueryArgsType.

For more comprehensive examples take a look at the Sorting Docs

Fields#

  • field - The field to sort on. The is an ENUM of @FilterableFields defined in the DTO.
  • direction - The direction to sort the field ASC or DESC.
  • nulls? - On supported storage engines you can specify the null sort order NULLS_FIRST, NULLS_LAST
import { QueryArgsType } from '@nestjs-query/query-graphql';import { TodoItemDTO } from './dto/todo-item.dto';
export const TodoItemQueryArgs = QueryArgsType(TodoItemDTO)export const TodoItemSort = TodoItemQueryArgs.SortType;

CreateOneInputType#

Input type for createOne mutations.

NOTE Dont forget to annotate your DTO with @InputType() from @nestjs/graphql.

NOTE Your DTO should be one that you want to use for input. For example you may not want to let the end user to specify the created or updated fields so you would create a new DTO just for input.

Usage#

import { CreateOneInputType } from '@nestjs-query/query-graphql';import { InputType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@InputType('TodoItemInput')export class TodoItemInputDTO {  @IsString()  @Field({nullable: true})  title?: string;
  @IsBoolean()  @Field({nullable: true})  completed?: string;}
@InputType()export class CreateOneTodoItemInput extends CreateOneInputType('todoItem', TodoItemInputDTO) {}

CreateManyInputType#

Input type for createMany mutations.

NOTE Dont forget to annotate your DTO with @InputType() from @nestjs/graphql.

NOTE Your DTO should be one that you want to use for input. For example you may not want to let the end user to specify the created or updated fields so you would create a new DTO just for input.

Usage#

import { CreateManyInputType } from '@nestjs-query/query-graphql';import { InputType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@InputType('TodoItemInput')export class TodoItemInputDTO {  @IsString()  @Field({nullable: true})  title?: string;
  @IsBoolean()  @Field({nullable: true})  completed?: string;}
@InputType()export class CreateManyTodoItemsInput extends CreateManyInputType('todoItems', TodoItemInputDTO) {}

UpdateOneInputType#

InputType type for updateOne mutations.

The UpdateOneInputType will generate an InputType that will require the user to provide two fields.

  • id - The id of the record to update
  • update - A record with fields to update.
note

Dont forget to annotate your DTO with @InputType() from @nestjs/graphql.

note

Your DTO should be one that you want to use for updates. For example you may not want to let the end user to update the created or updated fields so you would create a new DTO just for updates.

Usage#

import { UpdateOneInputType } from '@nestjs-query/query-graphql';import { InputType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@InputType('TodoItemUpdateInput')export class TodoItemUpdateDTO {  @IsOptional()  @IsString()  @Field({ nullable: true })  title?: string;
  @IsOptional()  @IsBoolean()  @Field({ nullable: true })  completed?: boolean;}
@InputType()export class UpdateOneTodoItemInput extends UpdateOneInputType(TodoItemDTO, TodoItemUpdateDTO) {}

UpdateManyInputType#

Input type for updateMany mutations.

The UpdateOneInputType will generate an InputType that will require the user to provide two arguments.

  • filter - The filter that should be used to find records to update. See FilterType
  • update - The update to apply to each record found.

NOTE Dont forget to annotate your DTO with @InputType() from @nestjs/graphql.

NOTE Your DTO should be one that you want to use for input. For example you may not want to let the end user to specify the created or updated fields so you would create a new DTO just for input.

Usage#

In this example we use the read DTO for the FilterType, and a different update DTO.

import { CreateOneInputType, FilterType } from '@nestjs-query/query-graphql';import { InputType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@InputType('TodoItemUpdate')export class TodoItemUpdateDTO {  @IsOptional()  @IsString()  @Field({ nullable: true })  title?: string;
  @IsOptional()  @IsBoolean()  @Field({ nullable: true })  completed?: boolean;}
@InputType()export class UpdateManyTodoItemsInput extends UpdateManyInputType(  TodoItemDTO, // filter  TodoItemUpdateDTO // update DTO) {}

DeleteOneInputType#

InputType type for deleteOne mutations.

The DeleteOneInputType will generate an InputType that will require the user to provide the id of the record to delete.

Usage#

import { DeleteOneInputType } from '@nestjs-query/query-graphql';import { InputType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@InputType()export class DeleteOneTodoItemInput extends DeleteOneInputType(TodoItemDTO) {}

DeleteManyInputType#

Input type type for deleteMany mutations.

The DeleteManyInputType will generate an InputType that will require the user to provide:

  • filter - A filter used to find records to delete. See FilterType

Usage#

import { CreateOneInputType, FilterType } from '@nestjs-query/query-graphql';import { InputType } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@InputType()export class DeleteManyTodoItemsInput extends DeleteManyInputType(TodoItemDTO) {}