wesleyyoung / perch-query-builder

Dynamically build TypeORM queries based on GraphQL queries for NestJS and TypeORM
GNU General Public License v3.0
46 stars 9 forks source link

Bug: getter fields are included in query #11

Open Migushthe2nd opened 3 years ago

Migushthe2nd commented 3 years ago

Describe the bug I have these entities:

@Entity()
export class PrimaryEntity extends BaseEntity {
    @PrimaryColumn()
    id: string;

    @OneToOne(() => SubEntity, subEntity => subEntity.entity)
    sub: SubEntity;
}

@Entity()
export class SubEntity extends BaseEntity {
    @OneToOne(() => PrimaryEntity, entity => entity.sub)
    @JoinColumn({name: "primaryEntityId"})
    entity: PrimaryEntity;

    @PrimaryColumn()
    primaryEntityId: string;

    get value() {
        return "test";
    }
}

And graphql models:

@ObjectType()
export class PrimaryModel {
    @Field()
    id: string;

    @Field(() => SubModel)
    sub: SubModel;
}

@ObjectType()
export class SubModel {
    @Field()
    value: string;
}

If I execute the following query:

query {
  item {
    id
    sub {
      value
    }
  }
}

PerchQueryBuilder tries to select the SubModel.value field in the database (incorrectly even. EDIT: This is a typeorm bug), while it is not decorated with a @Column decorator:

SELECT "PrimaryEntity"."id" AS "PrimaryEntity_id",
         PrimaryEntity_sub.value
FROM "typeorm"."primary_entity" "PrimaryEntity"
         LEFT JOIN "typeorm"."sub_entity" "PrimaryEntity_sub"
                   ON "PrimaryEntity_sub"."primaryEntityId" = "PrimaryEntity"."id"

(this query is a trimmed down query from something else)

To Reproduce See above

Expected behavior The SubEntity.value field isn't included in the query.

Migushthe2nd commented 3 years ago

Update: see the updated example above