简体   繁体   English

type-graphql 使字段解析器异常

[英]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.只要不满足条件,整个应用程序的answer字段将始终为“答案不公开”。 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 .当我尝试返回SampleA ExceptedResponse将始终被answer @FieldResolver()过滤,因此我无法得到真正的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.或者理想情况下,如果我可以使SpecialSampleASampleA扩展而不受此限制。

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.只需简单地创建一个新的 class,然后将SampleA结果分配给它。 It's auto handled by type-graphql and escape the resolve field check because Class is changed;它由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.

相关问题 如何在 type-graphql 中为联合类型做一个字段解析器 - How to do a Field Resolver for a Union Type in type-graphql 使用 type-graphql 和 RESTDataSource 为第二个 api 调用添加字段解析器 - Adding a field resolver for a second api call with type-graphql and the RESTDataSource Type-GraphQL 将分页 object 添加到解析器 - Type-GraphQL adding pagination object to resolver 是否可以将type-graphql buildSchema方法与来自nestjs / graphql的带有@Resolver注释的类一起使用? - Is it possible to use type-graphql buildSchema method with classes annotated with @Resolver from nestjs/graphql? type-graphql:如何知道 Resolver 返回了哪些字段 - type-graphql: How to know which fields are returned by Resolver 我的解析器导致未处理的 Promise 拒绝:Typegoose,Type-Graphql - Unhandled Promise Rejection caused by my resolver: Typegoose, Type-Graphql 如何使用 type-graphql 解析器 function 获取后端 graphql 中的选定字段? - How to fetch selected fields in graphql backend using type-graphql resolver function? 不能对 Type-GraphQL 字段类型使用类型“对象” - can't use type 'object' for Type-GraphQL field types type-graphql字段类型“ Int”和“ Number”之间的区别 - Difference between type-graphql field type “Int” and “Number” type-graphql 找不到模块“类验证器”异常 - type-graphql Cannot find module 'class-validator' exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM