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

Make sure the order of nested order filters is preserved #4171

Merged
merged 3 commits into from Mar 29, 2021

Conversation

jnugh
Copy link
Contributor

@jnugh jnugh commented Mar 29, 2021

Order filters are being written with underscores in GraphQL but are sent along with dots to access nested entities.

The previous solution appended the dot version to the order array. This leads to an issue where every order filter that accesses a nested property will always be added to the query after local properties.

Looking at the test case: The first order that WAS applied was localField. It should have been some.field which is now the case.

Q A
Branch? 2.6
Bug fix? yes
New feature? yes
Deprecations? no
Tickets I did not create any
License MIT
Doc PR -

Order filters are being written with underscores in GraphQL but are
sent along with dots to access nested entities.

The previous solution appended the dot version to the order array.
This leads to an issue where every order filter that accesses a nested
property will always be added to the query after local properties.
$filters =
\array_slice($filters, 0, $index + 1) +
[str_replace($this->nestingSeparator, '.', $name) => $value] +
\array_slice($filters, $index);
Copy link
Member

Choose a reason for hiding this comment

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

Why starting at index?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not make a difference in the output as the duplicated key gets merged so I did not notice that this code adds it twice. Will add +1 there as well.

CHANGELOG.md Outdated
@@ -13,6 +13,7 @@
* Swagger UI: Remove Google fonts (#4112)
* Doctrine: Revert #3774 support for binary UUID in search filter (#4134)
* Do not override Vary headers already set in the Response
* GraphQL: Make sure the order of order filters is preserved if nested resources are used
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* GraphQL: Make sure the order of order filters is preserved if nested resources are used
* GraphQL: Make sure the order of order filters is preserved if nested resources are used (#4171)

Comment on lines 225 to 228
/**
* Test a regression where the order in which order fitlers were applied
* was wrong when the data had been ordered by a nested field.
*/
Copy link
Member

Choose a reason for hiding this comment

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

I think this comment is unnecessary.

@alanpoulain
Copy link
Member

Hello! A tricky one 🙂 Thanks for spotting it and fixing it.

@jnugh
Copy link
Contributor Author

jnugh commented Mar 29, 2021

It's probably not a super common use case to sort on a child entity first...
Thanks for the great work on api-platform! 😄

Co-authored-by: Alan Poulain <contact@alanpoulain.eu>
@alanpoulain alanpoulain merged commit d5f8692 into api-platform:2.6 Mar 29, 2021
@alanpoulain
Copy link
Member

Thank you @jnugh.

@jnugh jnugh deleted the preserve-order-nested branch March 29, 2021 13:25
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.

None yet

2 participants