-
Notifications
You must be signed in to change notification settings - Fork 79
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
FIX Include default_sort in sortChildren method #1282
Conversation
5023adb
to
efd5620
Compare
Just to clarify, did you take any code from the |
I looked at the GraphQL 3 behavior and copied that |
There was a problem hiding this 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:
silverstripe-asset-admin/_legacy/GraphQL/FolderTypeCreator.php
Lines 185 to 188 in 17163c8
// 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.
silverstripe-asset-admin/_legacy/GraphQL/FolderTypeCreator.php
Lines 180 to 209 in 17163c8
$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; | |
}); |
efd5620
to
b176a49
Compare
b176a49
to
cc8e4e8
Compare
cc8e4e8
to
7e25669
Compare
7e25669
to
b346e50
Compare
There was a problem hiding this 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
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.