2
votes

I'm getting this error when trying to query a type and a descendant:

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

I set up a test install using recipe-core, admin, graphql, and graphql-devtools (all latest) to test this out in a basic setting. I've created 2 objects:

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)'
    ];

}

And set up simple scaffolding through configuration:

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

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

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

I'm able to query each of these types individually without any problems. But when I try to get TestChild as a descendant of TestObject I get the error above. Here's an example of my query:

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

Checking the documentation for the schema in graphiql, there is nothing under readAppTestObjects referencing descendants, although in the documentation for silverstripe/graphql it says:

When reading types that have exposed descendants (e.g. reading Page, when RedirectorPage is also exposed), the return type is a union of the base type and all exposed descendants. This union type takes on the name {BaseType}WithDescendants.
1

1 Answers

2
votes

Yeah this is a bug in the SilverStripe graphql module. What you're doing should work.

I believe the fix is in flight at https://github.com/silverstripe/silverstripe-graphql/pull/176, you can follow the progress there. Maybe try the patch out and leave some comments.