Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SortPlugin): amend sorting to all fields #541

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Schema/Plugin/SortPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public static function sort(array $context): Closure
return null;
}
$sortArgs = $args[$fieldName] ?? [];
foreach ($sortArgs as $field => $dir) {
$list = $list->sort($field, $dir);
}
$list = $list->sort($sortArgs);

return $list;
};
Expand Down
21 changes: 18 additions & 3 deletions tests/Schema/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ public function testFilterAndSort()
$dataObject2 = DataObjectFake::create(['MyField' => 'test2', 'AuthorID' => $author2->ID]);
$dataObject2->write();

$dataObject3 = DataObjectFake::create(['MyField' => 'test3', 'AuthorID' => $author2->ID]);
$dataObject3->write();

$file1 = File::create(['Title' => 'file1']);
$file1->write();

Expand All @@ -436,6 +439,7 @@ public function testFilterAndSort()

$id1 = $dataObject1->ID;
$id2 = $dataObject2->ID;
$id3 = $dataObject3->ID;

$schema = $this->createSchema(new TestSchemaBuilder([$dir]));

Expand Down Expand Up @@ -474,7 +478,7 @@ public function testFilterAndSort()

$query = <<<GRAPHQL
query {
readOneDataObjectFake(sort: { myField: DESC }) {
readOneDataObjectFake(sort: { AuthorID: DESC , myField: ASC }) {
myField
}
}
Expand All @@ -485,14 +489,25 @@ public function testFilterAndSort()

$query = <<<GRAPHQL
query {
readOneDataObjectFake(sort: { myField: DESC }, filter: { id: { ne: $id2 } }) {
readOneDataObjectFake(sort: { myField: DESC }) {
myField
}
}
GRAPHQL;
$result = $this->querySchema($schema, $query);
$this->assertSuccess($result);
$this->assertResult('readOneDataObjectFake.myField', 'test1', $result);
$this->assertResult('readOneDataObjectFake.myField', 'test3', $result);

$query = <<<GRAPHQL
query {
readOneDataObjectFake(sort: { myField: DESC }, filter: { id: { ne: $id3 } }) {
myField
}
}
GRAPHQL;
$result = $this->querySchema($schema, $query);
$this->assertSuccess($result);
$this->assertResult('readOneDataObjectFake.myField', 'test2', $result);

$query = <<<GRAPHQL
query {
Expand Down
1 change: 1 addition & 0 deletions tests/Schema/_testFilterAndSort/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SilverStripe\GraphQL\Tests\Fake\DataObjectFake:
sort: true
fields:
myField: true
AuthorID: true
author:
fields:
firstName: true
Expand Down