Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set_query_arg not properly respected by connection resolvers #3037

Conversation

jasonbahl
Copy link
Collaborator

What does this implement/fix? Explain your changes.

This addresses a bug where args set by $connection->set_query_arg() weren't being properly respected by the Connection Resolver class.

The instantiation of the query_args was happening at init, and so set_query_arg was happening too late.

By moving the instantiation to the execute_and_get_ids, this ensures that set_query_arg() has been properly applied between initializing and executing.

Does this close any currently open issues?

closes #3036

- set internal $query_args variable to be PostObjectConnectionResolver->query_args instead of empty array (respect what's been set by set_query_arg())
@jasonbahl jasonbahl self-assigned this Feb 6, 2024
@jasonbahl jasonbahl marked this pull request as draft February 6, 2024 18:43
@jasonbahl
Copy link
Collaborator Author

this is breaking a lot of tests, so I converted to a draft. It fixes the problem at hand from what I can tell, but clearly not the "right" solution overall yet.

Copy link

codeclimate bot commented Feb 6, 2024

Code Climate has analyzed commit 5dd69d2 and detected 0 issues on this pull request.

View more on Code Climate.

@justlevine
Copy link
Collaborator

this is breaking a lot of tests, so I converted to a draft. It fixes the problem at hand from what I can tell, but clearly not the "right" solution overall yet.

I'm not by my desk, but iirc this was part of the impetus for #2749 and might even be working there already. If so we can isolate and cherrypick in a way that isn't b/c.

@jasonbahl
Copy link
Collaborator Author

jasonbahl commented Feb 7, 2024

closing this as I believe there's a way to resolve this without making changes to core WPGraphQL (at least not immediately).

Instead of:

$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();

We can do the following:

// override the args that are passed in, as if a user passed them in.
$args['where']['in']  = [17, 105, 106, 15, 16, 18, 19, 30, 20, 21, 22, 23, 24, 7, 25, 26, 27, 28, 29, 31];
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $root, $args, $context, $info, 'page' );

This will let the internals of the Connection Resolvers do things as if a user were passing in the args.

@jasonbahl
Copy link
Collaborator Author

closing this as I was able to accomplish what was needed by adjusting the $args sent to the PostObjectConnectionResolver as outlined above.

Basically, for better or worse, the *ConnectionResolver classes use the $args passed in as the initial args that are used in mapping input args to query_args.

Then set_query_arg() is used to override any query_args already set.

For arguments like include or in, passing them via set_query_arg is too late for them to be used in mapping for things like pagination. But passing them in as "where" args to the connection resolver maps them as expected.

see: https://github.com/wp-graphql/wpgraphql-acf/pull/167/files

@jasonbahl jasonbahl closed this Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

set_query_arg is not properly respected when connections are resolved
2 participants