Skip to content

set_query_arg is not properly respected when connections are resolved #3036

@jasonbahl

Description

@jasonbahl

Description

When writing a resolver for a connection and using the $connection->set_query_arg() method, the args are not properly being respected by the resolving class.

i.e. PostObjectConnectionResolver isn't properly consideration of any args set by set_query_arg().

Steps to reproduce

  1. register a custom connection:
add_action( 'graphql_register_types', function() {
	register_graphql_connection( [
		'fromType' => 'RootQuery',
		'toType' => 'Post',
		'fromFieldName' => 'curatedPosts',
		'resolve' => function( $root, $args, $context, $info ) {
			$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $root, $args, $context, $info, 'any' );

			// REPLACE WITH A LIST OF VALID POST IDS.
			// ENOUGH TO PAGINATE AGAINST. IF NONE EXIST, GENERATE SOME.
			// ALSO MAKE SURE THEY ARE NOT STRICTLY ORDERED NUMERICALLY FOR SANITY CHECKING (i.e. 1, 5, 3, 2 instead of 1,2,3,4,5)
			$ids = [17, 105, 106, 15, 16, 18, 19, 30, 20, 21, 22, 23, 24, 7, 25, 26, 27, 28, 29, 31];
			return $resolver
				->set_query_arg( 'post_status', 'any' )
				->set_query_arg( 'post__in', $ids )
				->set_query_arg( 'orderby', 'post__in' )
				->get_connection();
		}
	] );
} );
  1. Query for the posts:
{
  curatedPosts(
    first: 5
    after:null
) {
    nodes {
      databaseId
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
  1. ✅ Verify that the first 5 posts match the first 5 in the set_query_arg( 'post__in' )

$ids = [17, 105, 106, 15, 16, 18, 19, 30, 20, 21, 22, 23, 24, 7, 25, 26, 27, 28, 29, 31];

first 5: = 17, 105, 106, 15, 16

looks good!

CleanShot 2024-02-06 at 10 55 54

  1. Use the endCursor and query the next 5:

  2. 👀 Expect to see the next 5 posts:

$ids = [17, 105, 106, 15, 16, 18, 19, 30, 20, 21, 22, 23, 24, 7, 25, 26, 27, 28, 29, 31];

first 5: = 17, 105, 106, 15, 16
first 5, after 16 = 18, 19, 30, 20, 21

CleanShot 2024-02-06 at 11 20 33

  1. 🙅🏻 🚫 Actually see that the response is NOT actually the next 5 items

CleanShot 2024-02-06 at 10 58 17

  1. 😭

Additional context

This was brought to light by @AndrewKepson while using the WPGraphQL for ACF extension, where fields of the relationship type were not paginating as expected.

We were able to identify that it's not a general pagination bug, as using the { where: { in: [ ... ] } } arg on root connections paginates as expected.

It initially appeared to be a bug with WPGraphQL for ACF but we narrowed it down to a bug with WPGraphQL Connection Resolvers.

WPGraphQL Version

1.20.0

WordPress Version

7.4.2

PHP Version

8.2

Additional environment details

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have disabled ALL plugins except for WPGraphQL.

  • Yes
  • My issue is with compatibility with a specific WordPress plugin, and I have listed all my installed plugins (and version info) above.

Metadata

Metadata

Assignees

Labels

type: bugIssue that causes incorrect or unexpected behavior

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions