如何在graphql中将附加信息传递给子解析器?

ncecgwcz  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(227)

我使用的是graphql java实现,我的简化模式如下所示:

topSection: TopSection {
  anotherSection: AnotherSection {
    name: String
    ## someMetadata: String (not part of schema)
    someAttribute: Attribute
  }
}

我必须实现 topSection 以及 someAttribute 现场。对于其他字段,我将依赖于graphql的隐式属性获取。
现在,对于 someAttribute 为了工作,我需要设置和通过 someMetadata 去吧。
一种选择是将其作为schema和 someAttribute 解析程序可以从 parent 但我不想让它成为模式的一部分。我已经证实了这是可行的。
然后,我尝试返回 AnotherSectionsomeMetadata 字段自 topSection 分解器,但我的npe内部出现故障。

class TopSectionResolver implements DataFetcher<TopSection> {
    @Override
    public TopSection get(final DataFetchingEnvironment dataFetchingEnvironment) {
       AnotherSection anotherSection = AnotherSectionSubClass.builder().name().someMetadata().someAttribute().build();
       return TopSection.builder().anotherSection(anotherSection).build();
    }
}

class SomeAttributeResolver implements DataFetcher<Attribute> {
    @Override
    public Attribute get(final DataFetchingEnvironment dataFetchingEnvironment) {
        String someMetadata = ((AnotherSectionSubClass) dataFetchingEnvironment.getSource()).getSomeMetadata();
        // other processing
    }
}

AnotherSectionSubClass extends AnotherSection {
    String name;
    String someMetadata;
    Attribute someAttribute
}

错误:

{\"errors\":[{\"message\":\"The execution failed due to an internal error: java.util.concurrent.ExecutionException: java.lang.NullPointerException\",\"extensions\":{\"classification\":\"DataFetchingException\"}}]}

我无法从graphql日志中找出失败的原因。
你能解释一下为什么这种传递子类的方法失败了吗?
要实现我的目标还有什么其他的选择?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题