Type-graphql: Can't figure out how to create a resolver in case a class extending another class

Created on 22 Jul 2019  路  2Comments  路  Source: MichalLytek/type-graphql

Hi guys,

I have a FinalClass that extends a BasicClass, with 2 additional fields of 2 other Classes.
Basic Class and the 2 other Classes are linked to typeOrm Entites and a foreignkey in each class enabled to join the data.

I want the final class to gather all data so that I can query all of them.
But I don't know how to implement the resolver in a proper way, for example, a getAllFinal() which will go through all BasicClass reports and collect the 2 other data.

I tried to create a resolver getAllBasic() in BasicClass, FieldResolvers for each additional fields in the FinalClass ? But I'm wondering , how I call all that stuff in the getAllFinal() ?

@ObjectType()
export class FinalClass extends BasicClass{
  @Field(() => [AnotherClass2], { nullable: true })
  userAliases: AnotherClass2[];

  @Field(() => [AnotherClass3], { nullable: true })
  userIdentity: AnotherClass3[];
}

@ObjectType()
export class BasicClass {
  @Field(()=>ID!)
  bcPrimaryKey: string;
}

@ObjectType()
export class AnotherClass2 {
  @Field()
  ac2PrimaryKey: string;
  @Field()
  bcPrimaryKey : string;
  @Field()
  value : number
}


@ObjectType()
export class AnotherClass3 {
  @Field()
  ac3PrimaryKey: string;
  @Field()
  bcPrimaryKey : string;
  @Field()
  thevalue : number
  @Field()
  thevalue2 : boolean
}

the below resolver is working fine, but this is not a clean way to do it I guess, because I'm calling the DB for each field:

@Resolver(FinalClass )
export class FinalClassResolver {
  @Query(() => [FinalClass ], { nullable: true })
  async getAllFinal(
    @Ctx() context: GlobalContext
  ): Promise<Array<Promise<FinalClass >>> {
    const features: BasicClass[] = await context.dbConnection
      .getRepository<BasicClass>("BasicClass")
      .createQueryBuilder("features")
      .skip(0)
      .take(1000)
      .getMany();

    const resu = features.map(async (user: BasicClass) => {
      const aliases: AnotherClass2[] = await context.dbConnection
        .getRepository<AnotherClass2 >("AnotherClass2 ")
        .createQueryBuilder("alias")
        .where("alias.bcPrimaryKey=:email", { email: user.bcPrimaryKey })
        .getMany();

      const identity: AnotherClass3[] = await context.dbConnection
        .getRepository<AnotherClass3>("AnotherClass3")
        .createQueryBuilder("identity")
        .where("identity.bcPrimarykey=:email", { email: user.bcPrimaryKey})
        .getMany();

      const item: BasicClass= {
        ...user,
        userAliases: aliases,
        userIdentity: identity
      };
      return item;
    });
    return resu;
  }
Community Question Wontfix

All 2 comments

Sorry but GitHub issues are not a helpdesk. If your issue is not a bug report or feature request, please ask your question on Gitter or StackOverflow.

That's unfortunate ... I haven't seen a solution to this in either stackoverflow or gitter so I continue to be stuck ...

Was this page helpful?
0 / 5 - 0 ratings