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 Include default_sort in sortChildren method #1282

Merged

Conversation

emteknetnz
Copy link
Member

@emteknetnz emteknetnz commented Jul 25, 2022

Issue silverstripe/gha-ci#37

Fix https://github.com/silverstripe/silverstripe-asset-admin/runs/7492970779?check_suite_focus=true#step:12:213
Then I should see the gallery item "xsubfolder1" in position "1"#

Not sure why this wasn't showing up on the travis graphql 4 behat jobs, as I was able to replicate this locally using both php 8.1 with mysql 8 as well as php 7.4 with mysql 5.7. I replicated by creating a pair of folders 'xsubfolder1' and 'zsubfolder2' then renaming them to 'zsubfolder1' and 'xsubfolder2' respectively. They should have then sorted by 'xsubfolder2', 'zsubfolder1' but they did not

This issue was not replicatable using graphql3, so this PR matches the graphql3 behaviour.

@GuySartorelli
Copy link
Member

this PR matches the graphql3 behaviour

Just to clarify, did you take any code from the legacy/graphql directory for this, or did you look at the way this behaves with GraphQL3 and replicate that behaviour with new code?

@emteknetnz
Copy link
Member Author

I looked at the GraphQL 3 behavior and copied that

Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some considerations in the GraphQL3 implementation that are not taken into account here, such as custom aggregate sort fields:

// some fields may be surrogates added by extending augmentSQL
// we have to preserve those expressions rather than auto-generated names
// that SQLSelect::addOrderBy leaves for them (e.g. _SortColumn0)
$field = $query->expressionForField(trim($field, '"')) ?: $field;

It might be worth considering using the existing GraphQL3 logic which preserves all existing sort orders (including default sort which is applied in DataObject::get()).

The sortChildren() method has ?Sortable as its type for $list but I think it would be safe to bump this to ?DataList since we know this admin section only handles DataList anyway - alternatively, you could do a if ($list instanceof DataList) check and do it the gql3 way if true, and this new way if false.

$list = $list->alterDataQuery(static function (DataQuery $dataQuery) {
$query = $dataQuery->query();
$existingOrderBys = [];
foreach ($query->getOrderBy() as $field => $direction) {
if (strpos($field, '.') === false) {
// some fields may be surrogates added by extending augmentSQL
// we have to preserve those expressions rather than auto-generated names
// that SQLSelect::addOrderBy leaves for them (e.g. _SortColumn0)
$field = $query->expressionForField(trim($field, '"')) ?: $field;
}
$existingOrderBys[$field] = $direction;
}
// Folders should always go first
$dataQuery->sort(
sprintf(
'(CASE WHEN "ClassName"=%s THEN 1 ELSE 0 END)',
DB::get_conn()->quoteString(Folder::class)
),
'DESC',
true
);
foreach ($existingOrderBys as $field => $dir) {
$dataQuery->sort($field, $dir, false);
}
return $dataQuery;
});

code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
code/GraphQL/Resolvers/FolderTypeResolver.php Outdated Show resolved Hide resolved
@emteknetnz emteknetnz force-pushed the pulls/1.11/graphql-sort branch from cc8e4e8 to 7e25669 Compare July 26, 2022 01:12
@emteknetnz emteknetnz force-pushed the pulls/1.11/graphql-sort branch from 7e25669 to b346e50 Compare July 26, 2022 01:13
Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, merge on green

@GuySartorelli GuySartorelli merged commit fefaffa into silverstripe:1.11 Jul 26, 2022
@GuySartorelli GuySartorelli deleted the pulls/1.11/graphql-sort branch July 26, 2022 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants