Skip to content

Commit 77d99b9

Browse files
committed
dev: refactor AbstractConnectionResolver::get_args() to ::prepare_args()
1 parent f6babda commit 77d99b9

8 files changed

+131
-105
lines changed

src/Data/Connection/AbstractConnectionResolver.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class AbstractConnectionResolver {
3636
*
3737
* Filterable by `graphql_connection_args`.
3838
*
39-
* @var array<string,mixed>
39+
* @var ?array<string,mixed>
4040
*/
4141
protected $args;
4242

@@ -113,6 +113,8 @@ abstract class AbstractConnectionResolver {
113113

114114
/**
115115
* @var mixed[]
116+
*
117+
* @deprecated @todo This is an artifact and is unused. It will be removed in a future release.
116118
*/
117119
protected $items;
118120

@@ -172,9 +174,9 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
172174

173175
/**
174176
* @todo This exists for b/c, where extenders may be directly accessing `$this->args` in ::get_loader() or even `::get_args()`.
175-
* We can remove this once the rest of lifecycle has been updated.
177+
* We can call it later in the lifecycle once that's no longer the case.
176178
*/
177-
$this->args = $args;
179+
$this->args = $this->get_args();
178180

179181
// Pre-check if the connection should execute so we can skip expensive logic if we already know it shouldn't execute.
180182
if ( ! $this->get_pre_should_execute( $this->source, $this->unfiltered_args, $this->context, $this->info ) ) {
@@ -185,7 +187,6 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
185187
$this->loader = $this->get_loader();
186188

187189
/**
188-
*
189190
* Filters the GraphQL args before they are used in get_query_args().
190191
*
191192
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
@@ -194,7 +195,7 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
194195
*
195196
* @since 1.11.0
196197
*/
197-
$this->args = apply_filters( 'graphql_connection_args', $this->get_args(), $this, $this->get_unfiltered_args() );
198+
$this->args = apply_filters( 'graphql_connection_args', $this->args, $this, $this->get_unfiltered_args() );
198199

199200
// Get the query amount for the connection.
200201
$this->query_amount = $this->get_query_amount();
@@ -223,17 +224,6 @@ protected function loader_name(): string {
223224
return '';
224225
}
225226

226-
/**
227-
* Returns the $args passed to the connection.
228-
*
229-
* Useful for modifying the $args before they are passed to $this->get_query_args().
230-
*
231-
* @return array<string,mixed>
232-
*/
233-
public function get_args(): array {
234-
return $this->args;
235-
}
236-
237227
/**
238228
* Get_query_args
239229
*
@@ -285,6 +275,19 @@ protected function pre_should_execute( $source, array $args, AppContext $context
285275
return $should_execute;
286276
}
287277

278+
/**
279+
* Prepares the GraphQL args for use by the connection.
280+
*
281+
* Useful for modifying the $args before they are passed to $this->get_query_args().
282+
*
283+
* @param array<string,mixed> $args The GraphQL input args to prepare.
284+
*
285+
* @return array<string,mixed>
286+
*/
287+
protected function prepare_args( array $args ): array {
288+
return $args;
289+
}
290+
288291
/**
289292
* The maximum number of items that should be returned by the query.
290293
*
@@ -458,7 +461,6 @@ public function get_loader_name() {
458461
return $this->loader_name;
459462
}
460463

461-
462464
/**
463465
* Returns the $args passed to the connection, before any modifications.
464466
*
@@ -468,6 +470,19 @@ public function get_unfiltered_args(): array {
468470
return $this->unfiltered_args;
469471
}
470472

473+
/**
474+
* Returns the $args passed to the connection.
475+
*
476+
* @return array<string,mixed>
477+
*/
478+
public function get_args(): array {
479+
if ( ! isset( $this->args ) ) {
480+
$this->args = $this->prepare_args( $this->get_unfiltered_args() );
481+
}
482+
483+
return $this->args;
484+
}
485+
471486
/**
472487
* Returns the amount of items to query from the database.
473488
*
@@ -493,7 +508,7 @@ public function get_query_amount() {
493508
*
494509
* @since 0.0.6
495510
*/
496-
$max_query_amount = (int) apply_filters( 'graphql_connection_max_query_amount', $this->max_query_amount(), $this->source, $this->args, $this->context, $this->info );
511+
$max_query_amount = (int) apply_filters( 'graphql_connection_max_query_amount', $this->max_query_amount(), $this->source, $this->get_args(), $this->context, $this->info );
497512

498513
// We don't want the requested amount to be lower than 0.
499514
$requested_query_amount = (int) max(
@@ -809,8 +824,8 @@ function () {
809824
*
810825
* This filter allows additional fields to be returned to the connection resolver
811826
*
812-
* @param ?array<string,mixed> $connection The connection data being returned. A single edge or null if the connection is one-to-one.
813-
* @param self $resolver The instance of the connection resolver
827+
* @param ?array<string,mixed> $connection The connection data being returned. A single edge or null if the connection is one-to-one.
828+
* @param self $resolver The instance of the connection resolver
814829
*/
815830
return apply_filters( 'graphql_connection', $connection, $this );
816831
}

src/Data/Connection/CommentConnectionResolver.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ class CommentConnectionResolver extends AbstractConnectionResolver {
2525
* @throws \GraphQL\Error\UserError
2626
*/
2727
public function get_query_args() {
28+
$args = $this->get_args();
2829

2930
/**
3031
* Prepare for later use
3132
*/
32-
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
33+
$last = ! empty( $args['last'] ) ? $args['last'] : null;
3334

3435
$query_args = [];
3536

@@ -56,18 +57,18 @@ public function get_query_args() {
5657
$query_args['orderby'] = 'comment_date';
5758

5859
/**
59-
* Take any of the $this->args that were part of the GraphQL query and map their
60+
* Take any of the $args that were part of the GraphQL query and map their
6061
* GraphQL names to the WP_Term_Query names to be used in the WP_Term_Query
6162
*
6263
* @since 0.0.5
6364
*/
6465
$input_fields = [];
65-
if ( ! empty( $this->args['where'] ) ) {
66-
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
66+
if ( ! empty( $args['where'] ) ) {
67+
$input_fields = $this->sanitize_input_fields( $args['where'] );
6768
}
6869

6970
/**
70-
* Merge the default $query_args with the $this->args that were entered
71+
* Merge the default $query_args with the $args that were entered
7172
* in the query.
7273
*
7374
* @since 0.0.5
@@ -113,14 +114,14 @@ public function get_query_args() {
113114
$query_args['graphql_before_cursor'] = $this->get_before_offset();
114115

115116
/**
116-
* Pass the graphql $this->args to the WP_Query
117+
* Pass the graphql $args to the WP_Query
117118
*/
118-
$query_args['graphql_args'] = $this->args;
119+
$query_args['graphql_args'] = $args;
119120

120121
// encode the graphql args as a cache domain to ensure the
121122
// graphql_args are used to identify different queries.
122123
// see: https://core.trac.wordpress.org/ticket/35075
123-
$encoded_args = wp_json_encode( $this->args );
124+
$encoded_args = wp_json_encode( $args );
124125
$query_args['cache_domain'] = ! empty( $encoded_args ) ? 'graphql:' . md5( $encoded_args ) : 'graphql';
125126

126127
/**
@@ -141,7 +142,7 @@ public function get_query_args() {
141142
*
142143
* @since 0.0.6
143144
*/
144-
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this->source, $this->args, $this->context, $this->info );
145+
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this->source, $args, $this->context, $this->info );
145146
}
146147

147148
/**
@@ -169,21 +170,19 @@ public function get_ids_from_query() {
169170
$ids = ! empty( $this->query->get_comments() ) ? $this->query->get_comments() : [];
170171

171172
// If we're going backwards, we need to reverse the array.
172-
if ( ! empty( $this->args['last'] ) ) {
173+
$args = $this->get_args();
174+
175+
if ( ! empty( $args['last'] ) ) {
173176
$ids = array_reverse( $ids );
174177
}
175178

176179
return $ids;
177180
}
178181

179182
/**
180-
* Filters the GraphQL args before they are used in get_query_args().
181-
*
182-
* @return array<string,mixed>
183+
* {@inheritDoc}
183184
*/
184-
public function get_args(): array {
185-
$args = $this->get_unfiltered_args();
186-
185+
protected function prepare_args( array $args ): array {
187186
if ( ! empty( $args['where'] ) ) {
188187
// Ensure all IDs are converted to database IDs.
189188
foreach ( $args['where'] as $input_key => $input_value ) {
@@ -238,8 +237,8 @@ static function ( $id ) {
238237
/**
239238
* Filters the GraphQL args before they are used in get_query_args().
240239
*
241-
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
242-
* @param \WPGraphQL\Data\Connection\CommentConnectionResolver $connection_resolver Instance of the ConnectionResolver
240+
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
241+
* @param self $resolver Instance of the ConnectionResolver
243242
*
244243
* @since 1.11.0
245244
*/
@@ -298,7 +297,7 @@ public function sanitize_input_fields( array $args ) {
298297
*
299298
* @since 0.0.5
300299
*/
301-
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $args, $this->source, $this->args, $this->context, $this->info );
300+
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $args, $this->source, $this->get_args(), $this->context, $this->info );
302301

303302
return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
304303
}

src/Data/Connection/MenuConnectionResolver.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,26 @@ public function get_query_args() {
2222
'fields' => 'ids',
2323
];
2424

25-
if ( ! empty( $this->args['where']['slug'] ) ) {
26-
$term_args['slug'] = $this->args['where']['slug'];
25+
$args = $this->get_args();
26+
27+
if ( ! empty( $args['where']['slug'] ) ) {
28+
$term_args['slug'] = $args['where']['slug'];
2729
$term_args['include'] = null;
2830
}
2931

3032
$theme_locations = get_nav_menu_locations();
3133

3234
// If a location is specified in the args, use it
33-
if ( ! empty( $this->args['where']['location'] ) ) {
35+
if ( ! empty( $args['where']['location'] ) ) {
3436
// Exclude unset and non-existent locations
35-
$term_args['include'] = ! empty( $theme_locations[ $this->args['where']['location'] ] ) ? $theme_locations[ $this->args['where']['location'] ] : -1;
37+
$term_args['include'] = ! empty( $theme_locations[ $args['where']['location'] ] ) ? $theme_locations[ $args['where']['location'] ] : -1;
3638
// If the current user cannot edit theme options
3739
} elseif ( ! current_user_can( 'edit_theme_options' ) ) {
3840
$term_args['include'] = array_values( $theme_locations );
3941
}
4042

41-
if ( ! empty( $this->args['where']['id'] ) ) {
42-
$term_args['include'] = $this->args['where']['id'];
43+
if ( ! empty( $args['where']['id'] ) ) {
44+
$term_args['include'] = $args['where']['id'];
4345
}
4446

4547
$query_args = parent::get_query_args();

src/Data/Connection/MenuItemConnectionResolver.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,36 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
2323
* {@inheritDoc}
2424
*/
2525
public function get_query_args() {
26+
$args = $this->get_args();
27+
2628
/**
2729
* Prepare for later use
2830
*/
29-
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
31+
$last = ! empty( $args['last'] ) ? $args['last'] : null;
3032

3133
$menu_locations = get_theme_mod( 'nav_menu_locations' );
3234

3335
$query_args = parent::get_query_args();
3436
$query_args['orderby'] = 'menu_order';
3537
$query_args['order'] = isset( $last ) ? 'DESC' : 'ASC';
3638

37-
if ( isset( $this->args['where']['parentDatabaseId'] ) ) {
39+
if ( isset( $args['where']['parentDatabaseId'] ) ) {
3840
$query_args['meta_key'] = '_menu_item_menu_item_parent';
39-
$query_args['meta_value'] = (int) $this->args['where']['parentDatabaseId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
41+
$query_args['meta_value'] = (int) $args['where']['parentDatabaseId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
4042
}
4143

42-
if ( ! empty( $this->args['where']['parentId'] ) || ( isset( $this->args['where']['parentId'] ) && 0 === (int) $this->args['where']['parentId'] ) ) {
44+
if ( ! empty( $args['where']['parentId'] ) || ( isset( $args['where']['parentId'] ) && 0 === (int) $args['where']['parentId'] ) ) {
4345
$query_args['meta_key'] = '_menu_item_menu_item_parent';
44-
$query_args['meta_value'] = $this->args['where']['parentId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
46+
$query_args['meta_value'] = $args['where']['parentId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
4547
}
4648

4749
// Get unique list of location term IDs as the default limitation of locations to allow public queries for.
4850
// Public queries should only be allowed to query for Menu Items assigned to a Menu Location.
4951
$locations = is_array( $menu_locations ) && ! empty( $menu_locations ) ? array_unique( array_values( $menu_locations ) ) : [];
5052

5153
// If the location argument is set, set the argument to the input argument
52-
if ( ! empty( $this->args['where']['location'] ) ) {
53-
$locations = isset( $menu_locations[ $this->args['where']['location'] ] ) ? [ $menu_locations[ $this->args['where']['location'] ] ] : []; // We use an empty array to prevent fetching all media items if the location has no items assigned.
54+
if ( ! empty( $args['where']['location'] ) ) {
55+
$locations = isset( $menu_locations[ $args['where']['location'] ] ) ? [ $menu_locations[ $args['where']['location'] ] ] : []; // We use an empty array to prevent fetching all media items if the location has no items assigned.
5456

5557
} elseif ( current_user_can( 'edit_theme_options' ) ) {
5658
// If the $locations are NOT set, let a user with proper capability query all menu items.
@@ -79,9 +81,7 @@ public function get_query_args() {
7981
/**
8082
* {@inheritDoc}
8183
*/
82-
public function get_args(): array {
83-
$args = $this->get_unfiltered_args();
84-
84+
public function prepare_args( array $args ): array {
8585
if ( ! empty( $args['where'] ) ) {
8686
// Ensure all IDs are converted to database IDs.
8787
foreach ( $args['where'] as $input_key => $input_value ) {

src/Data/Connection/PluginConnectionResolver.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ public function get_ids_from_query() {
4444
* {@inheritDoc}
4545
*/
4646
public function get_query_args() {
47-
if ( ! empty( $this->args['where']['status'] ) ) {
48-
$this->args['where']['stati'] = [ $this->args['where']['status'] ];
49-
} elseif ( ! empty( $this->args['where']['stati'] ) && is_string( $this->args['where']['stati'] ) ) {
50-
$this->args['where']['stati'] = [ $this->args['where']['stati'] ];
47+
$args = $this->get_args();
48+
49+
if ( ! empty( $args['where']['status'] ) ) {
50+
$args['where']['stati'] = [ $args['where']['status'] ];
51+
} elseif ( ! empty( $args['where']['stati'] ) && is_string( $args['where']['stati'] ) ) {
52+
$args['where']['stati'] = [ $args['where']['stati'] ];
5153
}
5254

53-
return $this->args;
55+
return $args;
5456
}
5557

5658
/**
@@ -69,6 +71,9 @@ public function get_query() {
6971
return [];
7072
}
7173

74+
// Get the GraphQL args for later.
75+
$args = $this->get_args();
76+
7277
// Holds the plugin names sorted by status. The other ` status => [ plugin_names ] ` will be added later.
7378
$plugins_by_status = [
7479
'mustuse' => array_flip( array_keys( $plugins['mustuse'] ) ),
@@ -81,7 +86,7 @@ public function get_query() {
8186
$show_network_plugins = apply_filters( 'show_network_active_plugins', current_user_can( 'manage_network_plugins' ) );
8287

8388
// Store the plugin stati as array keys for performance.
84-
$active_stati = ! empty( $this->args['where']['stati'] ) ? array_flip( $this->args['where']['stati'] ) : [];
89+
$active_stati = ! empty( $args['where']['stati'] ) ? array_flip( $args['where']['stati'] ) : [];
8590

8691
// Get additional plugin info.
8792
$upgradable_list = $can_update && isset( $active_stati['upgrade'] ) ? get_site_transient( 'update_plugins' ) : [];
@@ -196,9 +201,9 @@ public function get_query() {
196201
// If plugins exist for the filter, flatten and return them. Otherwise, return the full list.
197202
$filtered_plugins = ! empty( $filtered_plugins ) ? array_merge( [], ...$filtered_plugins ) : $plugins_by_status['all'];
198203

199-
if ( ! empty( $this->args['where']['search'] ) ) {
204+
if ( ! empty( $args['where']['search'] ) ) {
200205
// Filter by search args.
201-
$s = sanitize_text_field( $this->args['where']['search'] );
206+
$s = sanitize_text_field( $args['where']['search'] );
202207
$matches = array_keys(
203208
array_filter(
204209
$all_plugins,

0 commit comments

Comments
 (0)