-
Notifications
You must be signed in to change notification settings - Fork 470
Cursor pagination doesn't work with search queries #1818
Copy link
Copy link
Labels
compat: possible breakThere is a possibility that this might lead to breaking changes, but not confirmed yetThere is a possibility that this might lead to breaking changes, but not confirmed yetcomponent: paginationRelating to paginationRelating to paginationeffort: highMore than a weekMore than a weekhas: workaroundA temporary workaround has been providedA temporary workaround has been providedimpact: highUnblocks new use cases, substantial improvement to existing feature, fixes a major bugUnblocks new use cases, substantial improvement to existing feature, fixes a major bugobject type: postRelating to the Post Object TypesRelating to the Post Object Typesstatus: in progressCurrently being worked onCurrently being worked ontype: bugIssue that causes incorrect or unexpected behaviorIssue that causes incorrect or unexpected behavior
Description
Metadata
Metadata
Assignees
Labels
compat: possible breakThere is a possibility that this might lead to breaking changes, but not confirmed yetThere is a possibility that this might lead to breaking changes, but not confirmed yetcomponent: paginationRelating to paginationRelating to paginationeffort: highMore than a weekMore than a weekhas: workaroundA temporary workaround has been providedA temporary workaround has been providedimpact: highUnblocks new use cases, substantial improvement to existing feature, fixes a major bugUnblocks new use cases, substantial improvement to existing feature, fixes a major bugobject type: postRelating to the Post Object TypesRelating to the Post Object Typesstatus: in progressCurrently being worked onCurrently being worked ontype: bugIssue that causes incorrect or unexpected behaviorIssue that causes incorrect or unexpected behavior
Type
Fields
Give feedbackNo fields configured for Bug.
Projects
StatusShow more project fields
✅ Done
Cursor-based pagination combined with search queries generates SQL queries that do not correctly paginate search results. Investigating SQL Query Logs shows this is because cursor-based pagination translates to post date comparisons, which depends on queries being ordered by by post date. However, search queries are ordered by
wp_posts.post_title LIKE '%query%'first, therefore causing the start/end cursors to reference posts that are not in chronological order.During my investigation I found logic that was already put in place to prevent this, right here:
https://github.com/wp-graphql/wp-graphql/blob/develop/src/Data/Connection/PostObjectConnectionResolver.php#L262
However this block is never executing because it looks like the search query is at
$query_args['s']not$query_args['search'].Example query:
Will translate into the following SQL query:
SELECT wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%example%') OR (wp_posts.post_excerpt LIKE '%example%') OR (wp_posts.post_content LIKE '%example%'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) AND CAST( wp_posts.post_date as DATETIME ) <= CAST( '2014-06-03 12:19:21' as DATETIME ) AND ( CAST( wp_posts.post_date as DATETIME ) < CAST( '2014-06-03 12:19:21' as DATETIME ) OR ( wp_posts.ID < 10768 ) ) ORDER BY wp_posts.post_title LIKE '%example%' DESC, wp_posts.post_date DESC, wp_posts.ID DESC LIMIT 0, 6WPGraphQL version 1.3.3