GraphQL - 联合类型给出错误 no class could be found for that type name

Posted

技术标签:

【中文标题】GraphQL - 联合类型给出错误 no class could be found for that type name【英文标题】:GraphQL - Union Types giving the error no class could be found for that type name 【发布时间】:2020-05-04 02:05:37 【问题描述】:

在我的 GraphQL 架构文件中,我尝试添加一个联合类型,如下所示:

type CoOverrides 
    coId: String
    type: String


type SoOverrides 
    soId: String
    freeInterval: String


union CoOrSoOverrides = CoOverrides | SoOverrides

然后我将它分配给我的类型,如下所示:

type Campaign 
    id: ID!
    title: String
    overrides: [CoOrSoOverrides]

但是在启动 graphql 服务器时,我不断收到错误消息:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.coxautodev.graphql.tools.SchemaParser]: Factory method 'schemaParser' threw exception; 
nested exception is com.coxautodev.graphql.tools.SchemaClassScannerError: 
Object type 'CoOverrides' is a member of a known union, but no class could be found for that type name.  Please pass a class for type 'CoOverrides' in the parser's dictionary.

这到底是什么以及如何将这个类添加到解析器的字典中?

任何帮助表示赞赏。

谢谢。

【问题讨论】:

【参考方案1】:

如错误消息中所示 - 将 CoOverrides 类传递给字典。

@Configuration
public class SchemaParserConfig 

    @Bean
    public SchemaParserDictionary schemaParserDictionary() 
        return new SchemaParserDictionary()
                .add(CoOverrides.class);
    


【讨论】:

以上是关于GraphQL - 联合类型给出错误 no class could be found for that type name的主要内容,如果未能解决你的问题,请参考以下文章

联合类型导致 graphql 订阅

使用 graphql-ruby 实现联合类型

解决 graphql 查询中的联合结果时出现意外的 *Missing Field* 错误(Vue Apollo 客户端)

GraphQL 联合和输入类型?

了解 GraphQL 联合类型

类型-graphql。字符串、布尔值和数字的联合类型失败