SilverStripe GraphQL-查询带有后代的类型时出错

时间:2018-11-07 05:47:26

标签: php graphql silverstripe silverstripe-4

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

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

我使用配方核心,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。

1 个答案:

答案 0 :(得分:2)

是的,这是SilverStripe graphql模块中的错误。您在做什么应该可以。

我认为此修复程序已在https://github.com/silverstripe/silverstripe-graphql/pull/176进行中,您可以按照那里的进度进行操作。也许尝试补丁并留下一些评论。