Skip to content

#897 - Cursor pagination does not preserve order with the "search" where arg#898

Merged
jasonbahl merged 2 commits into
wp-graphql:developfrom
jasonbahl:bug/#897-cursor-pagination-with-search-arg
Jul 10, 2019
Merged

#897 - Cursor pagination does not preserve order with the "search" where arg#898
jasonbahl merged 2 commits into
wp-graphql:developfrom
jasonbahl:bug/#897-cursor-pagination-with-search-arg

Conversation

@jasonbahl

@jasonbahl jasonbahl commented Jul 9, 2019

Copy link
Copy Markdown
Collaborator

What does this implement/fix? Explain your changes.

WP_Query applies default ordering by title when the "s" arg is passed to it, which was causing funky issues with WPGraphQL Cursor Pagination.

This sets the search_orderby_title to false, and defaults to orderby date. Custom orderby options can be passed to the connection as well.

Does this close any currently open issues?

#897

Any relevant logs, error output, GraphiQL screenshots, etc?

To test, I created 4 posts with the word "floof" as the content.

In the admin, you can see a search for "floof" shows posts: `Four, Three, Two, One"

Screen Shot 2019-07-09 at 2 13 50 PM

Query the last 2

Here, I query for the last: 2 items, and as expected, we get Posts Two and One.

Query

query GET_POSTS($first: Int, $last: Int, $after: String, $before: String, $where: RootQueryToPostConnectionWhereArgs) {
  posts(last: $last, before: $before, first: $first, after: $after, where: $where) {
    pageInfo {
      hasPreviousPage
      hasNextPage
      startCursor
      endCursor
    }
    edges {
      cursor
      node {
        id
        date
        title
      }
    }
  }
}

Variables

{
  "last": 2,
  "before": "YXJyYXljb25uZWN0aW9uOjEzOTc=",
  "where": {
    "search": "floof"
  }
}

Screenshot of results

Screen Shot 2019-07-09 at 2 15 29 PM

Query with before cursor

Now, if we execute the same query, but use the startCursor from the previous query results as the value for the before input we get items Four and Three:

Variables

{
  "last": 2,
  "before": "YXJyYXljb25uZWN0aW9uOjEzOTc=",
  "where": {
    "search": "floof"
  }
}

Screenshot of paginated query

Screen Shot 2019-07-09 at 2 17 54 PM

Any other comments

Tests are included for forward and backward pagination using the {where: { search: ""} } argument

Where has this been tested?

Operating System: Mac OSX 10.14.5
WordPress Version: WordPress v5.2.2

…search" where arg

WP_Query applies default ordering by title when the "s" arg is passed to it, which was causing funky issues with WPGraphQL Cursor Pagination.

This sets the `search_orderby_title` to false, and defaults to orderby date. Custom orderby options can be passed to the connection as well.
@jasonbahl jasonbahl self-assigned this Jul 9, 2019
@jasonbahl jasonbahl added the type: bug Issue that causes incorrect or unexpected behavior label Jul 9, 2019
Comment thread src/Data/Cursor/PostObjectCursor.php Outdated

@hughdevore hughdevore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good stuff, just a couple extra spaces

$this->compare_with( $by, $order );
}
} else if ( ! empty( $orderby ) && is_string( $orderby ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👀

Comment thread src/Data/Cursor/PostObjectCursor.php Outdated
$orderby = $this->get_query_var( 'orderby' );
$order = $this->get_query_var( 'order' );


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👀

@codecov

codecov Bot commented Jul 10, 2019

Copy link
Copy Markdown

Codecov Report

Merging #898 into develop will decrease coverage by 0.03%.
The diff coverage is 63.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##           develop    #898      +/-   ##
==========================================
- Coverage    60.83%   60.8%   -0.04%     
==========================================
  Files          132     132              
  Lines         7795    7809      +14     
==========================================
+ Hits          4742    4748       +6     
- Misses        3053    3061       +8
Impacted Files Coverage Δ
...c/Data/Connection/PostObjectConnectionResolver.php 63.5% <100%> (+1.09%) ⬆️
src/Data/Cursor/PostObjectCursor.php 82.85% <36.36%> (-10.48%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8afef43...f2d0843. Read the comment docs.

$this->app_info = new \GraphQL\Type\Definition\ResolveInfo( array() );

$this->query = '
query GET_POSTS($first: Int, $last: Int, $after: String, $before: String $where:RootQueryToPostConnectionWhereArgs) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jasonbahl seems like a comma is missing before the where arg

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

good call. Commas in variable declarations are optional, so not a big deal, but probably should adjust for consistency 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Issue that causes incorrect or unexpected behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants