silverstripe / silverstripe-graphql

Serves Silverstripe data as GraphQL representations
BSD 3-Clause "New" or "Revised" License
52 stars 61 forks source link

Test schema generation differs from normal schema generation #539

Open GuySartorelli opened 1 year ago

GuySartorelli commented 1 year ago

There's some difference in building test schemas vs building real schemas which results in an error when building the below schema in tests which is perfectly fine and valid with normal runtime execution. This discrepancy is making it impossible to test https://github.com/silverstripe/silverstripe-graphql/pull/538 and could mean we are getting some false positives in existing tests.

Steps to reproduce

Create the following models.yml file in both vendor/silverstripe/graphql/tests/Schema/_testRecursiveSortSchemaCanBuild/models.yml and app/_graphql/models.yml:

SilverStripe\Security\Group:
  fields:
    '*': true

SilverStripe\Security\Permission:
  fields:
    id: true
    group: true
  operations:
    readOne: true
    read:
      plugins:
        sort:
          fields:
            id: true
            group:
              '*': true

Add this config to app/_config/graphql.yml:

SilverStripe\Control\Director:
  rules:
    'graphql': '%$SilverStripe\GraphQL\Controller.default'

SilverStripe\GraphQL\Schema\Schema:
  schemas:
    default:
      src:
        - app/_graphql

Add this method in vendor/silverstripe/graphql/tests/Schema/IntegrationTest.php:

public function testRecursiveSortSchemaCanBuild()
{
    // We're just testing that there's no exception on schema generation.
    $this->expectNotToPerformAssertions();

    $dir = '_' . __FUNCTION__;
    $this->createSchema(new TestSchemaBuilder([$dir]));
}

Finally, compare the results of running sake dev/graphql/build (which will run fine without any errors) vs running the new test method via PHPUnit. The PHPUnit test will fail with this exception:

SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException: Failed to apply plugin SilverStripe\GraphQL\Schema\DataObject\Plugin\QuerySort to readPermissions. Got error "Could not find field "id" on type "PermissionConnection". If it is a custom filter field, you will need to provide a resolver function in the "resolver" config for that field along with an explicit type."