Aller au contenu

Endpoints

The CRUD resolver automatically exposes two query endpoints. The endpoint names will be derived from the name provided to @ObjectType or the class name.

The following examples are based on the following ProductDTO:

import { EnhancedField, IDField } from '@lib/type-graphql-query';
import { ObjectType, ID, GraphQLISODateTime, Field } from '@lib/type-graphql';
@ObjectType('Product')
export class ProductDTO {
@IDField(() => ID)
id!: string;
@EnhancedField()
name!: string;
@EnhancedField()
price!: number;
@Field(() => GraphQLISODateTime)
created!: Date;
@Field(() => GraphQLISODateTime)
updated!: Date;
}

In the following examples you will see two endpoints referenced:

  • product - GraphQL endpoint to find a record by id.
  • products - GraphQL endpoint to filter, page, and sort records.

The following example finds a Product by id.

GraphQL:

{
product(id: "1") {
id
name
price
created
updated
}
}

Response:

{
"data": {
"product": {
"id": "1",
"name": "Premium Eggs",
"price": 15.99,
"created": "2020-01-14T07:00:31.763Z",
"updated": "2020-01-14T07:00:31.763Z"
}
}
}

As described above, the CRUD resolver will expose a query method called products. The result will be either a connection or array depending on the paging strategy you choose.

By default, if you do not provide arguments, it will use default values for the page size and for the sorting.

GraphQL:

{
products {
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
edges {
node {
id
name
price
created
updated
}
cursor
}
}
}

Response:

{
"data": {
"products": {
"pageInfo": {
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
"endCursor": "YXJyYXljb25uZWN0aW9uOjI="
},
"edges": [
{
"node": {
"id": "1",
"name": "Premium Eggs",
"price": 15.99,
"created": "2020-01-01T00:43:16.000Z",
"updated": "2020-01-01T00:43:16.000Z"
},
"cursor": "YXJyYXljb25uZWN0aW9uOjA="
},
{
"node": {
"id": "2",
"name": "Organic Eggs",
"price": 18.99,
"created": "2020-01-01T00:49:01.000Z",
"updated": "2020-01-01T00:49:01.000Z"
},
"cursor": "YXJyYXljb25uZWN0aW9uOjE="
},
{
"node": {
"id": "3",
"name": "Free Range Eggs",
"price": 20.99,
"created": "2020-01-01T00:49:01.000Z",
"updated": "2020-01-01T00:49:01.000Z"
},
"cursor": "YXJyYXljb25uZWN0aW9uOjI="
}
]
}
}
}

When a DTO has fields marked with localizable: true, the resolver automatically exposes a translations field that returns all translations for that record.

GraphQL:

{
product(id: "1") {
id
name
description
translations {
locale
name
description
}
}
}

Response:

{
"data": {
"product": {
"id": "1",
"name": "Premium Eggs",
"description": "High quality eggs",
"translations": [
{
"locale": "fr",
"name": "Œufs Premium",
"description": "Œufs de haute qualité"
},
{
"locale": "en",
"name": "Premium Eggs",
"description": "High quality eggs"
},
{
"locale": "mg",
"name": "Atody Premium",
"description": "Atody kalitao avo lenta"
}
]
}
}
}

The translations field is only available if the DTO has at least one field marked with localizable: true. See the DTOs documentation for more information.