typeorm composition with type-graphql : need mixins?

Viewed 586

I'm trying to use TypeOrm composition together with type-graphql, but I can not find a way to declare the object types. The goal is to have a final entity NewBudgetEntity composed with 3 entities : MyBaseEntity, BudgetHeaderEntity and BudgetHeaderEntity I have run the code below and it works fine, but the graphql schema is not reflecting the table. In the table, as expected, there are all the columns of the 3 entities. But in type-graphql, because I've add the @ObjectType in the main entity, the fields header and corps are in the Graphql schema, whereas they are not in the table.

I think I need to use mixins to make sure that the final @ObjectType extends the 3 other @ObjectTypes related to the 3 other entities, but I'm quite confused how to do it. I read the doc of type-graphql about mixins, but can not get it work on my project.

Here is the code that is running well, but I don't want 'header' and 'corps' to be part of the graphql query Ths for your help

import { Field, Float, ID, InputType, ObjectType } from "type-graphql";
import { BaseEntity, Column, Generated, Index, PrimaryColumn } from "typeorm";

export interface IIdentifier {
    id: string;
}

export interface IBudgetHeader {
    codeBudget: string;
    natureBudget: string;
}
export interface IBudgetCorps {
    montant: number;
    nom: string;
    isAffectable: boolean;
}

@ObjectType()
@InputType("myBaseEntityInputType")
export class MyBaseEntity extends BaseEntity implements IIdentifier {
    @Field(() => ID, { nullable: false })
    @PrimaryColumn({ type: "uuid" })
    @Index()
    @Generated("uuid")
    id: string;
}

@ObjectType()
@InputType("BudgetHeaderEntityInputType")
export class BudgetHeaderEntity implements IBudgetHeader {
    @Field({ nullable: false })
    @Column({
        type: "character varying",
        length: 20,
        nullable: false,
        unique: true,
        default: "",
    })
    codeBudget: string;

    @Field({ nullable: true })
    @Column({
        type: "character varying",
        length: 20,
        nullable: true,
        default: "",
    })
    natureBudget: string;
}

@ObjectType()
@InputType("BudgetCorpsEntityInputType")
export class BudgetCorpsEntity implements Partial<IBudgetCorps> {
    @Field({ nullable: true })
    @Column({
        type: "character varying",
        length: 250,
        nullable: true,
        default: "",
    })
    nom: string;

    @Field(() => Boolean, { nullable: true })
    @Column({
        type: "bool",
        default: false,
        nullable: true,
    })
    isAffectable: boolean;

    @Field(() => Float, { nullable: true })
    @Column({
        type: "float",
        default: 0.0,
        nullable: true,
    })
    montant: number;
}

@ObjectType()
@InputType("NewBudgetEntityInputType")
@Entity({
    name: "newbudget",
})
export class NewBudgetEntity extends MyBaseEntity {
    @Field(() => BudgetHeaderEntity)
    header: BudgetHeaderEntity;
    @Field(() => BudgetCorpsEntity)
    corps: BudgetCorpsEntity;
}

@Resolver(NewBudgetEntity)
export default class NewBudgetEntityResolvers extends apiNewBudgetEntity {
    @Query()
    dummy(): string {
        console.log("test");
        return "test";
    }
}

Example of query but I want to avoid 'header{} and corps{} and just having data {id codeBudget nom} enter image description here

1 Answers

If you want to hide some property from being exposed in GraphQL schema, just don't put @Field decorator above the property. If the property should be mapped to db column, just use @Column decorator only.

Related