diff --git a/src/Schema/Traits/SortTrait.php b/src/Schema/Traits/SortTrait.php index 95f6d0fb..32fb0d45 100644 --- a/src/Schema/Traits/SortTrait.php +++ b/src/Schema/Traits/SortTrait.php @@ -2,6 +2,7 @@ namespace SilverStripe\GraphQL\Schema\Traits; +use GraphQL\Language\AST\ObjectValueNode; use GraphQL\Type\Definition\ResolveInfo; trait SortTrait @@ -44,6 +45,12 @@ private static function getSortOrder(ResolveInfo $info, string $fieldName) continue; } + // If the sort has been passed as a variable, we can't attempt to fix it + // See https://github.com/silverstripe/silverstripe-graphql/issues/573 + if (!$arg->value instanceof ObjectValueNode) { + continue; + } + // Get the sort order from the query $sortOrder = []; foreach ($arg->value->fields as $field) { diff --git a/tests/Schema/IntegrationTest.php b/tests/Schema/IntegrationTest.php index c3cab186..eaa81efa 100644 --- a/tests/Schema/IntegrationTest.php +++ b/tests/Schema/IntegrationTest.php @@ -798,9 +798,9 @@ public function provideFilterAndSortOnlyRead(): array } /** - * @dataProvider provideFilterAndSortOnlyRead + * @dataProvider provideFilterAndSortWithArgsOnlyRead */ - public function testFilterAndSortOnlyRead(string $fixture, string $query, array $expected) + public function testFilterAndSortWithArgsOnlyRead(string $fixture, string $query, array $args, array $expected) { $author = Member::create(['FirstName' => 'tester1']); $author->write(); @@ -841,10 +841,13 @@ public function testFilterAndSortOnlyRead(string $fixture, string $query, array $factory = new TestSchemaBuilder(['_' . __FUNCTION__ . $fixture]); $schema = $this->createSchema($factory); - $result = $this->querySchema($schema, $query); + $result = $this->querySchema($schema, $query, $args); $this->assertSuccess($result); $records = $result['data']['readDataObjectFakes']['nodes'] ?? []; - $this->assertResults($expected, $records); + + // We intentionally don't check the sort order, as it's expected it may not match: + // See https://github.com/silverstripe/silverstripe-graphql/issues/573 + $this->assertEqualsCanonicalizing($expected, $records); } public function testAggregateProperties() diff --git a/tests/Schema/_testFilterAndSortWithArgsOnlyRead_QuerySort/models.yml b/tests/Schema/_testFilterAndSortWithArgsOnlyRead_QuerySort/models.yml new file mode 100644 index 00000000..f50a9d5e --- /dev/null +++ b/tests/Schema/_testFilterAndSortWithArgsOnlyRead_QuerySort/models.yml @@ -0,0 +1,12 @@ +SilverStripe\GraphQL\Tests\Fake\DataObjectFake: + operations: + read: + plugins: + sort: + before: paginateList + fields: + myField: true + author: + fields: + id: true + firstName: true diff --git a/tests/Schema/_testFilterAndSortWithArgsOnlyRead_SortPlugin/models.yml b/tests/Schema/_testFilterAndSortWithArgsOnlyRead_SortPlugin/models.yml new file mode 100644 index 00000000..8a6cb339 --- /dev/null +++ b/tests/Schema/_testFilterAndSortWithArgsOnlyRead_SortPlugin/models.yml @@ -0,0 +1,22 @@ +SilverStripe\GraphQL\Tests\Fake\DataObjectFake: + operations: + read: + plugins: + sort: + before: paginateList + fields: + myField: true + AuthorID: true + author: + fields: + firstName: true + files: + fields: + title: true + plugins: + sorter: + fields: + id: true + title: true + name: true + ParentID: true