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 }