-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #564 from creative-commoners/pulls/4.3/sortplugin-…
…tests FIX Add test cases for SortPlugin
- Loading branch information
Showing
5 changed files
with
221 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace SilverStripe\GraphQL\Schema\Traits; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
|
||
trait SortTrait | ||
{ | ||
private static function getSortArgs(ResolveInfo $info, array $args, string $fieldName): array | ||
{ | ||
$sortArgs = []; | ||
$sortOrder = self::getSortOrder($info, $fieldName); | ||
|
||
foreach ($sortOrder as $orderName) { | ||
if (!isset($args[$fieldName][$orderName])) { | ||
continue; | ||
} | ||
$sortArgs[$orderName] = $args[$fieldName][$orderName]; | ||
unset($args[$fieldName][$orderName]); | ||
} | ||
|
||
return array_merge($sortArgs, $args[$fieldName]); | ||
} | ||
|
||
/** | ||
* Gets the original order of fields to be sorted based on the query args order. | ||
* | ||
* This is necessary because the underlying GraphQL implementation we're using ignores the | ||
* order of query args, and uses the order that fields are defined in the schema instead. | ||
*/ | ||
private static function getSortOrder(ResolveInfo $info, string $fieldName) | ||
{ | ||
$relevantNode = $info->fieldDefinition->getName(); | ||
|
||
// Find the query field node that matches the schema | ||
foreach ($info->fieldNodes as $node) { | ||
if ($node->name->value !== $relevantNode) { | ||
continue; | ||
} | ||
|
||
// Find the sort arg | ||
foreach ($node->arguments as $arg) { | ||
if ($arg->name->value !== $fieldName) { | ||
continue; | ||
} | ||
|
||
// Get the sort order from the query | ||
$sortOrder = []; | ||
foreach ($arg->value->fields as $field) { | ||
$sortOrder[] = $field->name->value; | ||
} | ||
return $sortOrder; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/Schema/_testFilterAndSortOnlyRead_SortPlugin/models.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |