TriPSs / nestjs-query

Easy CRUD for GraphQL.
https://tripss.github.io/nestjs-query/
MIT License
164 stars 46 forks source link

@FilterableRelation does not work - v7.1.0 #313

Closed mborgues closed 3 weeks ago

mborgues commented 3 weeks ago

The @FilterableRelation like documentation not working: image introspectionSchema (1).json

My Classes

//return-MarcaVeiculoTerceiro.dto.ts
import { AutoMap } from "@automapper/classes";
import { Directive, ID, ObjectType } from "@nestjs/graphql";
import { ReturnModeloVeiculoTerceiroDTO } from "@/models";
import { FilterableField, IDField, OffsetConnection } from '@ptc-org/nestjs-query-graphql';
import { IsInt, IsString, Length } from "class-validator";

@ObjectType('marca')
@OffsetConnection('modelos', () => ReturnModeloVeiculoTerceiroDTO, { nullable: true })
@Directive('@key(fields: "codigo")')
export class ReturnMarcaVeiculoTerceiroDTO {
    @AutoMap()
    @IDField(()=> ID)
    @Length(3)
    @IsInt()
    codigo:number;

    @AutoMap()
    @FilterableField()
    @IsString()
    @Length(30)
    descricao:string;
}
//return-ModeloVeiculoTerceiro.dto.ts
import { AutoMap } from "@automapper/classes";
import { Directive, Int, ObjectType } from "@nestjs/graphql";
import { FilterableField, FilterableRelation, IDField} from '@ptc-org/nestjs-query-graphql';
import { ReturnMarcaVeiculoTerceiroDTO } from "./return-MarcaVeiculoTerceiro.dto";
import { IdModeloVeiculoTerceiroInput } from "../input/id-modeloVeiculoTerceiro.input";
import { IsInt, IsString, Length } from "class-validator";

@ObjectType('ModeloVeiculoTerceiro')
@FilterableRelation('Marca', () => ReturnMarcaVeiculoTerceiroDTO, { nullable: true })
@Directive('@key(fields: "modelo codigoMarca")')
export class ReturnModeloVeiculoTerceiroDTO {
    @AutoMap()
    @FilterableField(() => Int)
    @Length(3)
    @IsInt()
    modelo: number;

    @AutoMap()
    @FilterableField(() => Int)
    @Length(3)
    @IsInt()
    codigoMarca?: number

    @AutoMap()
    @FilterableField()
    @IsString()
    @Length(30)
    descricaoModelo:string;

    @AutoMap()
    @FilterableField({nullable: true})
    @Length(10)
    @IsString()
    codigoSeguradora?:string;

    @IDField(()=> IdModeloVeiculoTerceiroInput)
    get IdModeloVeiculoTerceiro(): IdModeloVeiculoTerceiroInput {
      return {
        modelo: this.modelo, 
        codigoMarca: this.codigoMarca};
    }
}
//MarcaVeiculoTerceiro.Module.ts
import { Module } from "@nestjs/common";
import { NestjsQueryGraphQLModule, PagingStrategies } from "@ptc-org/nestjs-query-graphql";
import { NestjsQueryTypeOrmModule } from "@ptc-org/nestjs-query-typeorm";
import { MarcaVeiculoTerceiroEntity } from "@/entities";
import { ReturnMarcaVeiculoTerceiroDTO, UpdateMarcaVeiculoTerceiroInput } from "@/models";
import { JwtAuthGuard } from "@praxio/globusweb-autentication";

@Module({
  imports: [
    NestjsQueryGraphQLModule.forFeature({
      imports:[NestjsQueryTypeOrmModule.forFeature([MarcaVeiculoTerceiroEntity])],
      resolvers:[{
        EntityClass:MarcaVeiculoTerceiroEntity,
        DTOClass:ReturnMarcaVeiculoTerceiroDTO,
        UpdateDTOClass:UpdateMarcaVeiculoTerceiroInput,
        enableTotalCount: true,
        pagingStrategy: PagingStrategies.OFFSET,
        guards:[JwtAuthGuard],
        referenceBy: {key: 'codigo'}
      }]
    }),
  ],
  providers: [],
})
export class MarcaVeiculoTerceiroModule {}

//ModeloVeiculoTerceiro.Module.ts
import { Module } from "@nestjs/common";
import { ModeloVeiculoTerceiroService } from "@/service";
import { ModeloVeiculoTerceiroEntity } from "@/entities";
import { ModeloVeiculoTerceiroRepository } from '@/repository';
import { IModeloVeiculoTerceiroRepositoryToken, IModeloVeiculoTerceiroServiceToken } from "@/token/token";
import { ModelosVeiculoTerceiroProfile } from "@/automapper/ModeloVeiculoTerceiro.profile";
import { NestjsQueryGraphQLModule, PagingStrategies } from "@ptc-org/nestjs-query-graphql";
import { ReturnModeloVeiculoTerceiroDTO, UpdateModeloVeiculoTerceiroInput } from "@/models";
import { NestjsQueryTypeOrmModule } from "@ptc-org/nestjs-query-typeorm";
import { JwtAuthGuard } from "@praxio/globusweb-autentication";
@Module({
    imports: [
        NestjsQueryGraphQLModule.forFeature({
            imports:[NestjsQueryTypeOrmModule.forFeature([ModeloVeiculoTerceiroEntity])],
            resolvers:[{
                EntityClass:ModeloVeiculoTerceiroEntity,
                DTOClass:ReturnModeloVeiculoTerceiroDTO,
                UpdateDTOClass:UpdateModeloVeiculoTerceiroInput,
                enableTotalCount: true,
                pagingStrategy: PagingStrategies.OFFSET,
                guards:[JwtAuthGuard],
                referenceBy: {key: 'modelo codigoMarca'}
            }]
        }),
    ],
    providers: [        
        ModelosVeiculoTerceiroProfile,
        {
            provide: IModeloVeiculoTerceiroServiceToken,
            useClass:ModeloVeiculoTerceiroService
        },
        {
            provide: IModeloVeiculoTerceiroRepositoryToken,
            useClass:ModeloVeiculoTerceiroRepository
        },
    ],
})
export class ModeloVeiculoTerceiroModule {}
//ModeloVeiculoTerceiro.Entity.ts
import { AutoMap } from "@automapper/classes";
import { Column, PrimaryColumn, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { MarcaVeiculoTerceiroEntity } from "./MarcaVeiculoTerceiro.Entity";

@Entity({name:'ACD_MODELOVEICTERCEIRO'})
export class ModeloVeiculoTerceiroEntity {
    @AutoMap()
    @PrimaryColumn({type: "int",name: "CODIGOMODELOVEICTER"})
    modelo: number;

    @AutoMap()
    @PrimaryColumn({ name: 'CODIGOMARCAVEICTER', type: 'number', precision: 3 })
    codigoMarca: number;

    @AutoMap()
    @Column({name: "DESCRICAOMODELOVEICTER"})
    descricaoModelo:string;

    @AutoMap()
    @Column({name: "CODSEGURADORAVEICTER"})
    codigoSeguradora?:string;

    @ManyToOne(() => MarcaVeiculoTerceiroEntity, marca => marca.modelos)
    @JoinColumn({name: "CODIGOMARCAVEICTER"})
    marca?:MarcaVeiculoTerceiroEntity;

}
// MarcaVeiculoTerceiro.Entity.ts
import { AutoMap } from "@automapper/classes";
import { Column, PrimaryColumn, Entity, OneToMany, JoinColumn } from 'typeorm';
import { ModeloVeiculoTerceiroEntity } from "./ModeloVeiculoTerceiro.Entity";
import { join } from "path";

@Entity({ name: 'ACD_MARCAVEICTERCEIRO' })
export class MarcaVeiculoTerceiroEntity {
  @AutoMap()
  @PrimaryColumn({ type: "int", name: "CODIGOMARCAVEICTER" })
  codigo: number;

  @AutoMap()
  @Column({ name: "DESCRICAOMARCAVEICTER" })
  descricao: string;

  @OneToMany(() => ModeloVeiculoTerceiroEntity, modelo => modelo.marca)
  modelos?: ModeloVeiculoTerceiroEntity[];
}

The error:

{
  "errors": [
    {
      "message": "unknown operator \"codigo\"",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "modeloVeiculoTerceiros"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "Error: unknown operator \"codigo\"",
          "    at SQLComparisonBuilder.build (D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\packages\\query-typeorm\\src\\query\\sql-comparison.builder.ts:93:11)",
          "    at D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\packages\\query-typeorm\\src\\query\\where.builder.ts:143:37",
          "    at Array.map (<anonymous>)",
          "    at Brackets.whereFactory (D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\packages\\query-typeorm\\src\\query\\where.builder.ts:142:37)",
          "    at SelectQueryBuilder.getWhereCondition (D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\src\\query-builder\\QueryBuilder.ts:1626:19)",
          "    at SelectQueryBuilder.andWhere (D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\src\\query-builder\\SelectQueryBuilder.ts:1183:29)",
          "    at WhereBuilder.withFilterComparison (D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\packages\\query-typeorm\\src\\query\\where.builder.ts:139:18)",
          "    at D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\packages\\query-typeorm\\src\\query\\where.builder.ts:109:21",
          "    at Array.reduce (<anonymous>)",
          "    at WhereBuilder.filterFields (D:\\Praxio\\GlobusWeb\\GlobusWeb.Acidentes\\back-end\\packages\\query-typeorm\\src\\query\\where.builder.ts:107:32)"
        ]
      }
    }
  ],
  "data": null
}

Observation: The query from Marca filtering Modelos returns the data

{
  marcas(
    filter:{
      modelos:{descricaoModelo:{like:"%FIESTA%"}}
    }
  ) {
    nodes {
      codigo
      descricao
    }
  }
}

image