首页 > 解决方案 > SilverStripe GraphQL - 查询具有后代的类型时出错

问题描述

尝试查询类型和后代时出现此错误:

"Fragment cannot be spread here as objects of type \"AppTestObject\" can never be of type \"AppTestChild\"."

我使用 recipe-core、admin、graphql 和 graphql-devtools(都是最新的)设置了一个测试安装,以在基本设置中进行测试。我创建了 2 个对象:

class TestObject extends DataObject {

    private static $singular_name = "Test Object";
    private static $plural_name = "Test Objects";
    private static $table_name = "TestObject";

    private static $db = [
        'Title' => 'Varchar(255)'
    ];

}

class TestChild extends DataObject {

    private static $singular_name = "Test Child";
    private static $plural_name = "Test Children";
    private static $table_name = "TestChild";

    private static $db = [
        'Title' => 'Varchar(255)'
    ];

}

并通过配置搭建简单的脚手架:

SilverStripe\GraphQL\Controller:
  schema:
    scaffolding:
      types:

        App\TestObject:
          fields: [ID]
          operations:
            read: true

        App\TestChild:
          fields: [ID, Title]
          operations:
            read: true

我可以毫无问题地单独查询这些类型中的每一个。但是当我尝试TestChild成为我的后代时,TestObject我得到了上面的错误。这是我的查询示例:

query {
  readAppTestObjects {
    edges {
      node {
        ...on AppTestChild {
          Title
        }
      }
    }
  }
}

检查 graphiql 中架构的文档,在readAppTestObjects引用后代下没有任何内容,尽管在 silverstripe/graphql 的文档中它说:

当读取具有暴露后代的类型时(例如读取 Page,当 RedirectorPage 也暴露时),返回类型是基类型和所有暴露后代的联合。此联合类型采用名称 {BaseType}WithDescendants。

标签: phpgraphqlsilverstripesilverstripe-4

解决方案


是的,这是 SilverStripe graphql 模块中的一个错误。你在做什么应该工作。

我相信修复程序已在https://github.com/silverstripe/silverstripe-graphql/pull/176进行,您可以在那里跟踪进度。也许尝试补丁并留下一些评论。


推荐阅读