Skip to content

Commit 2ee6fc8

Browse files
authored
Merge pull request #2457 from justlevine/fix/connection-resolver-pagination
fix: refactor resolvers to better follow Relay spec
2 parents a59ed5b + 9fc6928 commit 2ee6fc8

42 files changed

Lines changed: 5318 additions & 4823 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Data/Config.php

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace WPGraphQL\Data;
44

55
use WP_Comment_Query;
6+
use WP_Query;
7+
use WPGraphQL\Data\Cursor\CommentObjectCursor;
68
use WPGraphQL\Data\Cursor\PostObjectCursor;
79
use WPGraphQL\Data\Cursor\TermObjectCursor;
810
use WPGraphQL\Data\Cursor\UserCursor;
@@ -158,11 +160,11 @@ function ( $query ) {
158160
* and for their cursors to properly go forward/backward to the proper place in the database.
159161
*
160162
* @param string $orderby The ORDER BY clause of the query.
161-
* @param \WP_Query $wp_query The WP_Query instance executing
163+
* @param WP_Query $wp_query The WP_Query instance executing
162164
*
163165
* @return string
164166
*/
165-
public function graphql_wp_query_cursor_pagination_stability( string $orderby, \WP_Query $wp_query ) {
167+
public function graphql_wp_query_cursor_pagination_stability( string $orderby, WP_Query $wp_query ) {
166168

167169
if ( true !== is_graphql_request() ) {
168170
return $orderby;
@@ -188,28 +190,28 @@ public function graphql_wp_query_cursor_pagination_stability( string $orderby, \
188190
* This filters the WPQuery 'where' $args, enforcing the query to return results before or
189191
* after the referenced cursor
190192
*
191-
* @param string $where The WHERE clause of the query.
192-
* @param \WP_Query $query The WP_Query instance (passed by reference).
193+
* @param string $where The WHERE clause of the query.
194+
* @param WP_Query $query The WP_Query instance (passed by reference).
193195
*
194196
* @return string
195197
*/
196-
public function graphql_wp_query_cursor_pagination_support( $where, \WP_Query $query ) {
198+
public function graphql_wp_query_cursor_pagination_support( string $where, WP_Query $query ) {
197199

198-
/**
199-
* If there's a graphql_cursor_offset in the query, we should check to see if
200-
* it should be applied to the query
201-
*/
202-
if ( true === is_graphql_request() ) {
200+
// if it's not a graphql request, return early
201+
if ( true !== is_graphql_request() ) {
202+
return $where;
203+
}
203204

204-
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
205-
$after_cursor = new PostObjectCursor( $query, 'after' );
206-
$where = $where . $after_cursor->get_where();
207-
}
205+
// apply the after cursor to the query
206+
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
207+
$after_cursor = new PostObjectCursor( $query, 'after' );
208+
$where .= $after_cursor->get_where();
209+
}
208210

209-
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
210-
$before_cursor = new PostObjectCursor( $query, 'before' );
211-
$where = $where . $before_cursor->get_where();
212-
}
211+
// apply the before cursor to the query
212+
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
213+
$before_cursor = new PostObjectCursor( $query, 'before' );
214+
$where .= $before_cursor->get_where();
213215
}
214216

215217
return $where;
@@ -236,7 +238,6 @@ public function graphql_wp_user_query_cursor_pagination_stability( $orderby ) {
236238
return $orderby;
237239
}
238240

239-
240241
/**
241242
* This filters the WP_User_Query 'where' $args, enforcing the query to return results before or
242243
* after the referenced cursor
@@ -248,27 +249,26 @@ public function graphql_wp_user_query_cursor_pagination_stability( $orderby ) {
248249
*/
249250
public function graphql_wp_user_query_cursor_pagination_support( $where, \WP_User_Query $query ) {
250251

251-
/**
252-
* If there's a graphql_cursor_offset in the query, we should check to see if
253-
* it should be applied to the query
254-
*/
255-
if ( true === is_graphql_request() ) {
252+
// if it's not a graphql request, return early
253+
if ( true !== is_graphql_request() ) {
254+
return $where;
255+
}
256256

257-
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
258-
$after_cursor = new UserCursor( $query, 'after' );
259-
$where = $where . $after_cursor->get_where();
260-
}
257+
// apply the after cursor to the query
258+
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
259+
$after_cursor = new UserCursor( $query, 'after' );
260+
$where = $where . $after_cursor->get_where();
261+
}
261262

262-
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
263-
$before_cursor = new UserCursor( $query, 'before' );
264-
$where = $where . $before_cursor->get_where();
265-
}
263+
// apply the before cursor to the query
264+
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
265+
$before_cursor = new UserCursor( $query, 'before' );
266+
$where = $where . $before_cursor->get_where();
266267
}
267268

268269
return $where;
269270
}
270271

271-
272272
/**
273273
* This filters the term_clauses in the WP_Term_Query to support cursor based pagination, where
274274
* we can move forward or backward from a particular record, instead of typical offset
@@ -282,22 +282,26 @@ public function graphql_wp_user_query_cursor_pagination_support( $where, \WP_Use
282282
*/
283283
public function graphql_wp_term_query_cursor_pagination_support( array $pieces, array $taxonomies, array $args ) {
284284

285-
if ( true === is_graphql_request() ) {
286-
287-
if ( isset( $args['number'] ) && absint( $args['number'] ) ) {
288-
$pieces['limits'] = sprintf( ' LIMIT 0, %d', absint( $args['number'] ) );
289-
}
285+
// if it's not a graphql request, return early
286+
if ( true !== is_graphql_request() ) {
287+
return $pieces;
288+
}
290289

291-
if ( ! empty( $args['graphql_after_cursor'] ) ) {
290+
// determine the limit for the query
291+
if ( isset( $args['number'] ) && absint( $args['number'] ) ) {
292+
$pieces['limits'] = sprintf( ' LIMIT 0, %d', absint( $args['number'] ) );
293+
}
292294

293-
$after_cursor = new TermObjectCursor( $args, 'after' );
294-
$pieces['where'] = $pieces['where'] . $after_cursor->get_where();
295-
}
295+
// apply the after cursor
296+
if ( ! empty( $args['graphql_after_cursor'] ) ) {
297+
$after_cursor = new TermObjectCursor( $args, 'after' );
298+
$pieces['where'] = $pieces['where'] . $after_cursor->get_where();
299+
}
296300

297-
if ( ! empty( $args['graphql_before_cursor'] ) ) {
298-
$before_cursor = new TermObjectCursor( $args, 'before' );
299-
$pieces['where'] = $pieces['where'] . $before_cursor->get_where();
300-
}
301+
// apply the before cursor
302+
if ( ! empty( $args['graphql_before_cursor'] ) ) {
303+
$before_cursor = new TermObjectCursor( $args, 'before' );
304+
$pieces['where'] = $pieces['where'] . $before_cursor->get_where();
301305
}
302306

303307
return $pieces;
@@ -306,7 +310,7 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
306310

307311
/**
308312
* This returns a modified version of the $pieces of the comment query clauses if the request
309-
* is a GraphQL Request and the query has a graphql_cursor_offset defined
313+
* is a GraphQL Request and before or after cursors are passed to the query
310314
*
311315
* @param array $pieces A compacted array of comment query clauses.
312316
* @param WP_Comment_Query $query Current instance of WP_Comment_Query, passed by reference.
@@ -318,37 +322,18 @@ public function graphql_wp_comments_query_cursor_pagination_support( array $piec
318322
/**
319323
* Access the global $wpdb object
320324
*/
321-
global $wpdb;
325+
if ( true !== is_graphql_request() ) {
326+
return $pieces;
327+
}
322328

323-
if (
324-
true === is_graphql_request() &&
325-
( is_array( $query->query_vars ) && array_key_exists( 'graphql_cursor_offset', $query->query_vars ) )
326-
) {
327-
328-
$cursor_offset = $query->query_vars['graphql_cursor_offset'];
329-
330-
/**
331-
* Ensure the cursor_offset is a positive integer
332-
*/
333-
if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {
334-
335-
$compare = ! empty( $query->query_vars['graphql_cursor_compare'] ) ? $query->query_vars['graphql_cursor_compare'] : '>';
336-
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
337-
338-
$order_by = ! empty( $query->query_vars['order_by'] ) ? $query->query_vars['order_by'] : 'comment_date';
339-
$order = ! empty( $query->query_vars['order'] ) ? $query->query_vars['order'] : 'DESC';
340-
$order_compare = ( 'ASC' === $order ) ? '>' : '<';
341-
342-
// Get the $cursor_post
343-
$cursor_comment = get_comment( $cursor_offset );
344-
if ( ! empty( $cursor_comment ) ) {
345-
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
346-
$pieces['where'] .= $wpdb->prepare( " AND {$order_by} {$order_compare} %s", $cursor_comment->{$order_by} );
347-
} else {
348-
// phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder
349-
$pieces['where'] .= $wpdb->prepare( ' AND comment_ID %1$s %2$d', $compare, $cursor_offset );
350-
}
351-
}
329+
if ( ! empty( $query->query_vars['graphql_after_cursor'] ) ) {
330+
$after_cursor = new CommentObjectCursor( $query->query_vars, 'after' );
331+
$pieces['where'] .= $after_cursor->get_where();
332+
}
333+
334+
if ( ! empty( $query->query_vars['graphql_before_cursor'] ) ) {
335+
$before_cursor = new CommentObjectCursor( $query->query_vars, 'before' );
336+
$pieces['where'] .= $before_cursor->get_where();
352337
}
353338

354339
return $pieces;

0 commit comments

Comments
 (0)