Skip to content

Commit

Permalink
FIX: Don't attempt to correct sort when passed as an argument (closes s…
Browse files Browse the repository at this point in the history
  • Loading branch information
kinglozzer committed May 8, 2024
1 parent 2aabfb0 commit fc1c090
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Schema/Traits/SortTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\GraphQL\Schema\Traits;

use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Type\Definition\ResolveInfo;

trait SortTrait
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions tests/Schema/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SilverStripe\GraphQL\Tests\Fake\DataObjectFake:
operations:
read:
plugins:
sort:
before: paginateList
fields:
myField: true
author:
fields:
id: true
firstName: true
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fc1c090

Please sign in to comment.