[英]type-graphql make exception from Field Resolver
例如,我有一个以下实体和解析器,
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;
}
}
结果很好。 只要不满足条件,整个应用程序的answer
字段将始终为“答案不公开”。 但是,如果有时我想破例,
例如,如果我有以下
@ObjectType()
class ExceptedResponse {
@Field(() => String, {nullable: true})
errors?: string;
@Field(() => SampleA, {nullable: true})
result?: SampleA;
}
当我尝试返回SampleA
ExceptedResponse
将始终被answer @FieldResolver()
过滤,因此我无法得到真正的answer
。
所以我想知道我是否可以在不手动重新创建一个全新字段的情况下将其排除在外
@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
或者理想情况下,如果我可以使SpecialSampleA
从SampleA
扩展而不受此限制。
经过一些随机测试,似乎实现我的目标非常简单。 只需简单地创建一个新的 class,然后将SampleA
结果分配给它。 它由type-graphql
自动处理并转义解析字段检查,因为Class
已更改;
@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 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.