Skip to content

Commit

Permalink
Merge pull request #2882 from kidunot89/feat/post-object-cursor-filters
Browse files Browse the repository at this point in the history
fix: Config and Cursor classes refactor.
  • Loading branch information
jasonbahl committed Aug 28, 2023
2 parents 30d3622 + da3476f commit 20f38c5
Show file tree
Hide file tree
Showing 18 changed files with 8,678 additions and 3,613 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"simpod/php-coveralls-mirror": "^3.0",
"slevomat/coding-standard": "^8.9",
"szepeviktor/phpstan-wordpress": "~1.3.0",
"wp-graphql/wp-graphql-testcase": "~2.3"
"wp-graphql/wp-graphql-testcase": "~2.3",
"wp-cli/wp-cli-bundle": "^2.8"
},
"config": {
"platform": {
Expand Down
9,798 changes: 6,381 additions & 3,417 deletions composer.lock

Large diffs are not rendered by default.

196 changes: 167 additions & 29 deletions src/Data/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ static function ( $query ) {
'graphql_wp_user_query_cursor_pagination_stability',
],
10,
1
2
);
}

Expand All @@ -162,28 +162,50 @@ static function ( $query ) {
* and for their cursors to properly go forward/backward to the proper place in the database.
*
* @param string $orderby The ORDER BY clause of the query.
* @param \WP_Query $wp_query The WP_Query instance executing
* @param \WP_Query $query The WP_Query instance executing.
*
* @return string
*/
public function graphql_wp_query_cursor_pagination_stability( string $orderby, WP_Query $wp_query ) {
public function graphql_wp_query_cursor_pagination_stability( string $orderby, WP_Query $query ) {
// Bail early if it's not a GraphQL Request.
if ( true !== is_graphql_request() ) {
return $orderby;
}

global $wpdb;
/**
* If pre-filter hooked, return $pre_orderby.
*
* @param null|string $pre_orderby The pre-filtered ORDER BY clause of the query.
* @param string $orderby The ORDER BY clause of the query.
* @param \WP_Query $query The WP_Query instance (passed by reference).
*
* @return null|string
*/
$pre_orderby = apply_filters( 'graphql_pre_wp_query_cursor_pagination_stability', null, $orderby, $query );
if ( null !== $pre_orderby ) {
return $pre_orderby;
}

// Bail early if disabled by connection.
if ( isset( $query->query_vars['graphql_apply_cursor_pagination_orderby'] )
&& false === $query->query_vars['graphql_apply_cursor_pagination_orderby'] ) {
return $orderby;
}

// If the cursor "graphql_cursor_compare" arg is not in the query,
// default to using ID DESC as the stabilizer
if ( ! isset( $wp_query->query['graphql_cursor_compare'] ) ) {
return "{$orderby}, {$wpdb->posts}.ID DESC ";
// Bail early if the cursor "graphql_cursor_compare" arg is not in the query,
if ( ! isset( $query->query_vars['graphql_cursor_compare'] ) ) {
return $orderby;
}

// Check the cursor compare order
$order = '>' === $wp_query->query['graphql_cursor_compare'] ? 'ASC' : 'DESC';
$order = '>' === $query->query_vars['graphql_cursor_compare'] ? 'ASC' : 'DESC';

// Get Cursor ID key.
$cursor = new PostObjectCursor( $query->query_vars );
$key = $cursor->get_cursor_id_key();

// If there is a cursor compare in the arguments, use it as the stablizer for cursors.
return "{$orderby}, {$wpdb->posts}.ID {$order} ";
return "{$orderby}, {$key} {$order}";
}

/**
Expand All @@ -196,19 +218,38 @@ public function graphql_wp_query_cursor_pagination_stability( string $orderby, W
* @return string
*/
public function graphql_wp_query_cursor_pagination_support( string $where, WP_Query $query ) {

// if it's not a graphql request, return early
// Bail early if it's not a GraphQL Request.
if ( true !== is_graphql_request() ) {
return $where;
}

// apply the after cursor to the query
/**
* If pre-filter hooked, return $pre_where.
*
* @param null|string $pre_where The pre-filtered WHERE clause of the query.
* @param string $where The WHERE clause of the query.
* @param \WP_Query $query The WP_Query instance (passed by reference).
*
* @return null|string
*/
$pre_where = apply_filters( 'graphql_pre_wp_query_cursor_pagination_support', null, $where, $query );
if ( null !== $pre_where ) {
return $pre_where;
}

// Bail early if disabled by connection.
if ( isset( $query->query_vars['graphql_apply_cursor_pagination_where'] )
&& false === $query->query_vars['graphql_apply_cursor_pagination_where'] ) {
return $where;
}

// Apply the after cursor, moving forward through results
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
$after_cursor = new PostObjectCursor( $query->query_vars, 'after' );
$where .= $after_cursor->get_where();
}

// apply the before cursor to the query
// Apply the after cursor, moving backward through results.
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
$before_cursor = new PostObjectCursor( $query->query_vars, 'before' );
$where .= $before_cursor->get_where();
Expand All @@ -226,15 +267,46 @@ public function graphql_wp_query_cursor_pagination_support( string $where, WP_Qu
*
* @return string
*/
public function graphql_wp_user_query_cursor_pagination_stability( $orderby ) {
if ( true === is_graphql_request() ) {
global $wpdb;
public function graphql_wp_user_query_cursor_pagination_stability( $orderby, \WP_User_Query $query ) {

// Bail early if it's not a GraphQL Request.
if ( true !== is_graphql_request() ) {
return $orderby;
}

/**
* If pre-filter hooked, return $pre_orderby.
*
* @param null|string $pre_orderby The pre-filtered ORDER BY clause of the query.
* @param string $orderby The ORDER BY clause of the query.
* @param \WP_User_Query $query The WP_User_Query instance (passed by reference).
*
* @return null|string
*/
$pre_orderby = apply_filters( 'graphql_pre_wp_user_query_cursor_pagination_stability', null, $orderby, $query );
if ( null !== $pre_orderby ) {
return $pre_orderby;
}

// Bail early if disabled by connection.
if ( isset( $query->query_vars['graphql_apply_cursor_pagination_orderby'] )
&& false === $query->query_vars['graphql_apply_cursor_pagination_orderby'] ) {
return $orderby;
}

// phpcs:ignore
return "{$orderby}, {$wpdb->users}.ID DESC ";
// Bail early if the cursor "graphql_cursor_compare" arg is not in the query,
if ( ! isset( $query->query_vars['graphql_cursor_compare'] ) ) {
return $orderby;
}

return $orderby;
// Check the cursor compare order
$order = '>' === $query->query_vars['graphql_cursor_compare'] ? 'ASC' : 'DESC';

// Get Cursor ID key.
$cursor = new UserCursor( $query->query_vars );
$key = $cursor->get_cursor_id_key();

return "{$orderby}, {$key} {$order}";
}

/**
Expand All @@ -248,18 +320,38 @@ public function graphql_wp_user_query_cursor_pagination_stability( $orderby ) {
*/
public function graphql_wp_user_query_cursor_pagination_support( $where, \WP_User_Query $query ) {

// if it's not a graphql request, return early
// Bail early if it's not a GraphQL Request.
if ( true !== is_graphql_request() ) {
return $where;
}

// apply the after cursor to the query
/**
* If pre-filter hooked, return $pre_where.
*
* @param null|string $pre_where The pre-filtered WHERE clause of the query.
* @param string $where The WHERE clause of the query.
* @param \WP_User_Query $query The WP_Query instance (passed by reference).
*
* @return null|string
*/
$pre_where = apply_filters( 'graphql_pre_wp_user_query_cursor_pagination_support', null, $where, $query );
if ( null !== $pre_where ) {
return $pre_where;
}

// Bail early if disabled by connection.
if ( isset( $query->query_vars['graphql_apply_cursor_pagination_where'] )
&& false === $query->query_vars['graphql_apply_cursor_pagination_where'] ) {
return $where;
}

// Apply the after cursor.
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
$after_cursor = new UserCursor( $query->query_vars, 'after' );
$where = $where . $after_cursor->get_where();
}

// apply the before cursor to the query
// Apply the after cursor.
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
$before_cursor = new UserCursor( $query->query_vars, 'before' );
$where = $where . $before_cursor->get_where();
Expand All @@ -281,23 +373,49 @@ public function graphql_wp_user_query_cursor_pagination_support( $where, \WP_Use
*/
public function graphql_wp_term_query_cursor_pagination_support( array $pieces, array $taxonomies, array $args ) {

// if it's not a graphql request, return early
// Bail early if it's not a GraphQL Request.
if ( true !== is_graphql_request() ) {
return $pieces;
}

// determine the limit for the query
/**
* If pre-filter hooked, return $pre_pieces.
*
* @param null|array $pre_pieces The pre-filtered term query SQL clauses.
* @param array $pieces Terms query SQL clauses.
* @param array $taxonomies An array of taxonomies.
* @param array $args An array of terms query arguments.
*
* @return null|array
*/
$pre_pieces = apply_filters( 'graphql_pre_wp_term_query_cursor_pagination_support', null, $pieces, $taxonomies, $args );
if ( null !== $pre_pieces ) {
return $pre_pieces;
}

// Bail early if disabled by connection.
if ( isset( $args['graphql_apply_cursor_pagination_where'] )
&& false === $args['graphql_apply_cursor_pagination_where'] ) {
return $pieces;
}

// Bail early if the cursor "graphql_cursor_compare" arg is not in the query,
if ( ! isset( $args['graphql_cursor_compare'] ) ) {
return $pieces;
}

// Determine the limit for the query
if ( isset( $args['number'] ) && absint( $args['number'] ) ) {
$pieces['limits'] = sprintf( ' LIMIT 0, %d', absint( $args['number'] ) );
}

// apply the after cursor
// Apply the after cursor.
if ( ! empty( $args['graphql_after_cursor'] ) ) {
$after_cursor = new TermObjectCursor( $args, 'after' );
$pieces['where'] = $pieces['where'] . $after_cursor->get_where();
}

// apply the before cursor
// Apply the before cursor.
if ( ! empty( $args['graphql_before_cursor'] ) ) {
$before_cursor = new TermObjectCursor( $args, 'before' );
$pieces['where'] = $pieces['where'] . $before_cursor->get_where();
Expand All @@ -317,18 +435,38 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
*/
public function graphql_wp_comments_query_cursor_pagination_support( array $pieces, WP_Comment_Query $query ) {

// Bail early if it's not a GraphQL Request.
if ( true !== is_graphql_request() ) {
return $pieces;
}

/**
* Access the global $wpdb object
* If pre-filter hooked, return $pre_pieces.
*
* @param null|array $pre_pieces The pre-filtered comment query clauses.
* @param array $pieces A compacted array of comment query clauses.
* @param \WP_Comment_Query $query Current instance of WP_Comment_Query, passed by reference.
*
* @return null|array
*/
if ( true !== is_graphql_request() ) {
$pre_pieces = apply_filters( 'graphql_pre_wp_comments_query_cursor_pagination_support', null, $pieces, $query );
if ( null !== $pre_pieces ) {
return $pre_pieces;
}

// Bail early if disabled by connection.
if ( isset( $query->query_vars['graphql_apply_cursor_pagination_where'] )
&& false === $query->query_vars['graphql_apply_cursor_pagination_where'] ) {
return $pieces;
}

// Apply the after cursor, moving forward through results.
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
$after_cursor = new CommentObjectCursor( $query->query_vars, 'after' );
$pieces['where'] .= $after_cursor->get_where();
}

// Apply the after cursor, moving backward through results.
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
$before_cursor = new CommentObjectCursor( $query->query_vars, 'before' );
$pieces['where'] .= $before_cursor->get_where();
Expand Down
27 changes: 12 additions & 15 deletions src/Data/Connection/TermObjectConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ public function get_query_args() {
*/
$query_args['number'] = min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1;

/**
* Orderby Name by default
*/
$query_args['orderby'] = 'name';
$query_args['order'] = 'ASC';

/**
* Don't calculate the total rows, it's not needed and can be expensive
*/
Expand Down Expand Up @@ -127,16 +121,19 @@ public function get_query_args() {
$query_args['fields'] = 'ids';

/**
* If there's no orderby params in the inputArgs, set order based on the first/last argument
* If there's no orderby params in the inputArgs, default to ordering by name.
*/
if ( ! empty( $query_args['order'] ) ) {
if ( ! empty( $last ) ) {
if ( 'ASC' === $query_args['order'] ) {
$query_args['order'] = 'DESC';
} else {
$query_args['order'] = 'ASC';
}
}
if ( empty( $query_args['orderby'] ) ) {
$query_args['orderby'] = 'name';
}

/**
* If orderby params set but not order, default to ASC if going forward, DESC if going backward.
*/
if ( empty( $query_args['order'] ) && 'name' === $query_args['orderby'] ) {
$query_args['order'] = ! empty( $last ) ? 'DESC' : 'ASC';
} elseif ( empty( $query_args['order'] ) ) {
$query_args['order'] = ! empty( $last ) ? 'ASC' : 'DESC';
}

/**
Expand Down

0 comments on commit 20f38c5

Please sign in to comment.