Skip to main content

v0.11.2

路 2 min read

@InjectAssemblerQueryService#

In v0.11.1 a new @InjectAssemblerQueryService decorator has been introduced to reduce boiler plate code related to creating an AssemblerQueryService.

Example#

Define your assembler

todo-item.assembler.ts
import { Assembler, ClassTransformerAssembler } from '@nestjs-query/core';import { TodoItemDTO } from './todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Assembler(TodoItemDTO, TodoItemEntity)export class TodoItemAssembler extends ClassTransformerAssembler<TodoItemDTO, TodoItemEntity> {  convertToDTO(entity: TodoItemEntity): TodoItemDTO {    const dto = super.convertToDTO(entity);    dto.age = Date.now() - entity.created.getMilliseconds();    return dto;  }}

Now, instead of defining an AssemblerQueryService you can use the decorator.

todo-item.resolver.ts
import { QueryService, InjectAssemblerQueryService } from '@nestjs-query/core';import { CRUDResolver } from '@nestjs-query/query-graphql';import { Resolver } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';
@Resolver(() => TodoItemDTO)export class TodoItemResolver extends CRUDResolver(TodoItemDTO) {  constructor(@InjectAssemblerQueryService(TodoItemAssembler) readonly service: QueryService<TodoItemDTO>) {    super(service);  }}

Using Assemblers in auto-generated Resolver#

There is a new AssemblerClass option when using the NestjsQueryGraphQLModule to generate your resolver to using a custom Assembler.

note

When using the AssemblerClass option you don't need to specify the entity.

todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemDTO } from './dto/todo-item.dto';import { TodoItemEntity } from './todo-item.entity';import { TodoItemAssembler } from './todo-item.assembler';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      // Register the assembler      assemblers: [TodoItemAssembler],      resolvers: [        {          DTOClass: TodoItemDTO,          // specify the assembler type to use.          AssemblerClass: TodoItemAssembler,        },      ],    }),  ],})export class TodoItemModule {}

Using Services in auto-generated Resolver#

There is a new ServiceClass option when using the NestjsQueryGraphQLModule to generate your resolver to a custom QueryService.

note

When using the ServiceClass option you don't need to specify the entity.

todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemDTO } from './dto/todo-item.dto';import { TodoItemEntity } from './todo-item.entity';import { TodoItemService } from './todo-item.service';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      // Register the the custom QueryService      services: [TodoItemService],      resolvers: [        {          DTOClass: TodoItemDTO,          // specify the QueryService type to use.          ServiceClass: TodoItemService,        },      ],    }),  ],})export class TodoItemModule {}

v0.11.0

路 5 min read

@InjectQueryService#

In v0.11.x an new decorator was added @InjectQueryService, this decorator replaces the ORM specific decorators:

  • @InjectTypeOrmQueryService
  • @InjectSequelizeQueryService

To migrate replace all @InjectTypeOrmQueryService and @InjectSequelizeQueryService with @InjectQueryService.

note

You still need to import the NestjsQueryTypeOrmModule or NestjsQuerySequelizeModule to use @InjectQueryService.

import { QueryService, InjectQueryService } from '@nestjs-query/core';import { CRUDResolver } from '@nestjs-query/query-graphql';import { Resolver } from '@nestjs/graphql';import { TodoItemDTO } from './dto/todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Resolver(() => TodoItemDTO)export class TodoItemResolver extends CRUDResolver(TodoItemDTO) {  constructor(@InjectQueryService(TodoItemEntity) readonly service: QueryService<TodoItemEntity>) {    super(service);  }}

Relation Decorators#

In v0.11.x two new decorators were introduced from the @nestjs-query/query-graphql package. The @Relation and @Connection allow specifying relations on the DTO rather than passing them in as options to the CRUDResolver.

@Relation#

The @Relation decorator allow specifying a single relation (e.g. one-to-one, many-to-one).

In this example we add a relation for a todoItem.

import { FilterableField, Relation } from '@nestjs-query/query-graphql';import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';import { TodoItemDTO } from '../todo-item/todo-item.dto.ts';
@ObjectType('SubTask')@Relation('todoItem', () => TodoItemDTO, { disableRemove: true })export class SubTaskDTO {  @FilterableField(() => ID)  id!: string;
  @FilterableField()  title!: string;
  @FilterableField()  completed!: boolean;
  @FilterableField(() => GraphQLISODateTime)  created!: Date;
  @FilterableField(() => GraphQLISODateTime)  updated!: Date;
  @FilterableField()  todoItemId!: string;}

@Connection#

The @Connection decorator allow specifying a connection relation (e.g. many-to-many, one-to-many).

In this example we add a connection for subTasks.

import { FilterableField, Connection } from '@nestjs-query/query-graphql';import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';import { SubTaskDTO } from '../sub-task/sub-task.dto'
@ObjectType('TodoItem')@Connection('subTasks', () => SubTaskDTO, { disableRemove: true })export class TodoItemDTO {  @FilterableField(() => ID)  id!: string;
  @FilterableField()  title!: string;
  @FilterableField()  completed!: boolean;
  @FilterableField(() => GraphQLISODateTime)  created!: Date;
  @FilterableField(() => GraphQLISODateTime)  updated!: Date;}

Auto Generated Resolvers#

In v0.11.x the @nestjs-query/query-graph package is now able to automatically create your resolvers.

To start using this feature update your module to import the NestjsQueryGraphQLModule

note

When used with the new relation decorators, you can remove a lot of boiler plate.

todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemDTO } from './todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      resolvers: [{ DTOClass: TodoItemDTO, EntityClass: TodoItemEntity }],    }),  ],})export class TodoItemModule {}

Once you have updated your module you can remove the old resolver.

note

Be sure to add the NestjsQueryTypeOrmModule or NestjsQuerySequelizeModule to the imports in NestjsQueryGraphQLModule.

To read more about the auto-generated resolvers check out the resolvers docs

Example#

Previously#

todo-item.resolver.ts
import { QueryService, InjectQueryService } from '@nestjs-query/core';import { CRUDResolver } from '@nestjs-query/query-graphql';import { Resolver } from '@nestjs/graphql';import { TodoItemInputDTO } from './todo-item.input';import { TodoItemDTO } from './todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Resolver()export class TodoItemResolver extends CRUDResolver(TodoItemDTO) {  constructor(      @InjectQueryService(TodoItemEntity) readonly service: QueryService<TodoItemEntity>  ) {    super(service);  }}
todo-item.module.ts
import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemEntity } from './todo-item.entity';import { TodoItemResolver } from './todo-item.resolver';
@Module({  providers: [TodoItemResolver],  imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],})export class TodoItemModule {}

New#

todo-item.module.ts
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';import { NestjsQueryTypeOrmModule } from '@nestjs-query/query-typeorm';import { Module } from '@nestjs/common';import { TodoItemDTO } from './todo-item.dto';import { TodoItemEntity } from './todo-item.entity';
@Module({  imports: [    NestjsQueryGraphQLModule.forFeature({      imports: [NestjsQueryTypeOrmModule.forFeature([TodoItemEntity])],      resolvers: [{ DTOClass: TodoItemDTO, EntityClass: TodoItemEntity }],    }),  ],})export class TodoItemModule {}

v0.8.6

路 One min read
  • chore(renovate): Renovate to include examples
  • chore(renovate): Renovate set ignorePaths to empty
  • fix(deps): pin dependencies
  • chore(deps): Update package-lock.json
  • chore(deps): Update postgres backing app to 11.7
  • docs(): Update Federation Docs
  • chore(lerna): add hoist to lerna.json
  • chore(deps): update dependency @nestjs/graphql to v7.3.4
  • chore(deps): update dependency @types/node to v13.13.2
  • chore(renovate): Update to automerge devDeps
  • chore(deps): update dependency coveralls to v3.0.13
  • chore(deps): update dependency eslint-config-prettier to v6.11.0

v0.8.1

路 One min read
  • [FIXED] Mysql error "LIMIT in subquery" #77
    • Changed nestjs-query/query-typeorm to not use subqueries to fetch relations.