简体   繁体   中英

type-graphql make exception from Field Resolver

I have a following Entity and Resolver for example,

    export class SampleA extends BaseEntity {
      @Field()
      @PrimaryGeneratedColumn()
      id!: number;
    
      @Field()
      @Column()
      title!: string
    
      @Field()
      @Column()
      answer!: string;
     }


    @Resolver(SampleA)
    export class SampleAResolver {

        @FieldResolver(() => String)
        async answer(@Root() sampleA: SampleA, @Ctx() {req}: MyContext) {
            const msg = "answer is not public";
            if(!req.session.userId) return msg;
            return sampleA.answer;
        }
    }

It worked out well. As long as the condition does not meet, the answer field will be always "answer is not public" across the entire app. However, if sometimes I want to make a exception out of it,

For example, if I have the following

@ObjectType()
class ExceptedResponse {
    @Field(() => String, {nullable: true})
    errors?: string;
    @Field(() => SampleA, {nullable: true})
    result?: SampleA;
}

When I try to return the ExceptedResponse , the SampleA field will always be filtered by the answer @FieldResolver() so I cannot get the real answer .

So I wonder if I can make it to an exception out of it without manually re-create a whole new field like

@ObjectType()
class ExceptedResponse {
    @Field(() => String, {nullable: true})
    errors?: string;
    @Field(() => {id: Number, title: String, answer: String }, {nullable: true})
    result?: {id: number, title: string, answer: string };
}

//then I just manually assign each value inside my graphql method

Or ideally if I can make a SpecialSampleA extends from SampleA without that limitation.

After some random tests, it seems like it's extremely simple to achieve my goal. Just simply create a new class and then assign the SampleA result to it. It's auto handled by type-graphql and escape the resolve field check because Class is changed;

@ObjectType()
class ExceptedSampleA {
  @Field()
  inspiration: string;
  @Field()
  answer: string;
}
//inside response
    @Field(() => ExceptedSampleA, {nullable: true})
    result?: ExceptedSampleA;

//inside graphql method like Mutation() then return response
   const sampleA = SampleA.findOne();
   return { result: sampleA, error: someError }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM