Types
@ptc-org/nestjs-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 '@ptc-org/nestjs-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 FilterTypepaging?
- Options to page of result.sorting?
- Optional array to allow sorting of results. See SortType.
Usage
import { QueryArgsType } from '@ptc-org/nestjs-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
- Code
- Schema
import { QueryArgsType } from '@ptc-org/nestjs-query-graphql';
import { TodoItemDTO } from './dto/todo-item.dto';
export const TodoItemQueryArgs = QueryArgsType(TodoItemDTO)
export const TodoItemConnection = TodoItemQueryArgs.ConnectionType;
scalar ConnectionCursor
type TodoItemConnection {
pageInfo: PageInfo!
edges: [TodoItemEdge!]!
}
type TodoItemEdge {
node: TodoItem!
cursor: ConnectionCursor!
}
type PageInfo {
hasNextPage: Boolean
hasPreviousPage: Boolean
startCursor: ConnectionCursor
endCursor: ConnectionCursor
}
UpdateManyResponseType
Response from updateMany
mutations.
Usage
- Code
- Schema
import { UpdateManyResponseType } from '@ptc-org/nestjs-query-graphql';
export const UpdateManyResponse = UpdateManyResponseType()
type UpdateManyResponse {
updatedCount: Int!
}
DeleteManyResponseType
Response from deleteMany
mutations.
Usage
- Code
- Schema
import { DeleteManyResponseType } from '@ptc-org/nestjs-query-graphql';
export const UpdateManyResponse = DeleteManyResponseType()
type DeleteManyResponse {
deletedCount: Int!
}
InputTypes
FilterType
GraphQL implementation of the @ptc-org/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 multipleFilters
with using anAND
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 multipleFilters
using anOR
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
}
- Code
- Schema
import { FilterType } from '@ptc-org/nestjs-query-graphql';
import { TodoItemDTO } from './dto/todo-item.dto';
export const TodoItemFilter = FilterType(TodoItemDTO)
input TodoItemFilter {
and: [TodoItemFilter!]
or: [TodoItemFilter!]
id: IDFilterComparison
title: StringFieldComparison
completed: BooleanFieldComparison
created: DateFieldComparison
updated: DateFieldComparison
}
input IDFilterComparison {
is: Boolean
isNot: Boolean
eq: ID
neq: ID
gt: ID
gte: ID
lt: ID
lte: ID
like: ID
notLike: ID
iLike: ID
notILike: ID
in: [ID!]
notIn: [ID!]
}
input StringFieldComparison {
is: Boolean
isNot: Boolean
eq: String
neq: String
gt: String
gte: String
lt: String
lte: String
like: String
notLike: String
iLike: String
notILike: String
in: [String!]
notIn: [String!]
}
input BooleanFieldComparison {
is: Boolean
isNot: Boolean
}
input DateFieldComparison {
is: Boolean
isNot: Boolean
eq: DateTime
neq: DateTime
gt: DateTime
gte: DateTime
lt: DateTime
lte: DateTime
in: [DateTime!]
notIn: [DateTime!]
}
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 fieldASC
orDESC
.nulls?
- On supported storage engines you can specify the null sort orderNULLS_FIRST
,NULLS_LAST
- Code
- Schema
import { QueryArgsType } from '@ptc-org/nestjs-query-graphql';
import { TodoItemDTO } from './dto/todo-item.dto';
export const TodoItemQueryArgs = QueryArgsType(TodoItemDTO)
export const TodoItemSort = TodoItemQueryArgs.SortType;
input TodoItemSort {
field: TodoItemSortFields!
direction: SortDirection!
nulls: SortNulls
}
enum TodoItemSortFields {
id
title
completed
created
updated
}
enum SortDirection {
ASC
DESC
}
enum SortNulls {
NULLS_FIRST
NULLS_LAST
}
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 '@ptc-org/nestjs-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 '@ptc-org/nestjs-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 updateupdate
- A record with fields to update.
Dont forget to annotate your DTO with @InputType()
from @nestjs/graphql
.
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 '@ptc-org/nestjs-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 FilterTypeupdate
- 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 '@ptc-org/nestjs-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 '@ptc-org/nestjs-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 '@ptc-org/nestjs-query-graphql';
import { InputType } from '@nestjs/graphql';
import { TodoItemDTO } from './dto/todo-item.dto';
@InputType()
export class DeleteManyTodoItemsInput extends DeleteManyInputType(TodoItemDTO) {}