You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Usually, the returned value is an instantiated WP_Query class, but it can be any collection of data. The `get_ids_from_query()` method will be used to extract the IDs from the returned value.
227
-
*
228
-
* @param array $query_args The query args to use to query the data.
@@ -292,6 +294,52 @@ protected function max_query_amount() : int {
292
294
return100;
293
295
}
294
296
297
+
/**
298
+
* The default query class to use for the connection. Should `null` if the resolver does not use a query class to fetch the data.
299
+
*/
300
+
protectedfunctionquery_class() : ?string {
301
+
returnnull;
302
+
}
303
+
304
+
/**
305
+
* Validates the query class. Will be ignored if the Connection Resolver does not use a query class.
306
+
*
307
+
* By default this checks if the query class has a `query()` method. If the query class requires the `query()` method to be named something else (e.g. $query_class->get_results()` ) this method should be overloaded.
308
+
*
309
+
* @param string $query_class The query class to validate.
* Usually, the returned value is an instantiated `$query_class` (e.g. `WP_Query`), but it can be any collection of data. The `get_ids_from_query()` method will be used to extract the IDs from the returned value.
319
+
*
320
+
* If the resolver does not rely on a query class, this should be overloaded to return the data directly.
321
+
*
322
+
* @param array $query_args The query args to use to query the data.
323
+
*
324
+
* @return mixed
325
+
*/
326
+
protectedfunctionquery( array$query_args ) {
327
+
// If there is no query class, we need the child class to overload this method.
328
+
$query_class = $this->get_query_class();
329
+
330
+
if ( empty( $query_class ) ) {
331
+
thrownewInvariantViolation(
332
+
// translators: %s is the name of the connection resolver class.
333
+
sprintf(
334
+
__( 'The %s class does not rely on a query class. Please define a `query()` method to return the data directly.', 'wp-graphql' ),
335
+
static::class
336
+
)
337
+
);
338
+
}
339
+
340
+
returnnew$query_class( $query_args );
341
+
}
342
+
295
343
/**
296
344
* Determine whether or not the query should execute.
297
345
*
@@ -495,6 +543,32 @@ public function get_query_args() : array {
495
543
return$this->query_args;
496
544
}
497
545
546
+
/**
547
+
* Gets the query class to be instantiated by the `query()` method.
548
+
*/
549
+
publicfunctionget_query_class() : ?string {
550
+
if ( ! isset( $this->query_class ) ) {
551
+
$default_query_class = $this->query_class();
552
+
553
+
// Attempt to get the query class from the context.
* Filters the `$query_class` that will be used to execute the query.
560
+
*
561
+
* This is useful for replacing the default query (e.g `WP_Query` ) with a custom one (E.g. `WP_Term_Query` or WooCommerce's `WC_Query`).
562
+
*
563
+
* @param ?string $query_class The query class to be used with the executable query to get data. `null` if the ConnectionResolver does not use a query class.
564
+
* @param self $resolver Instance of the ConnectionResolver
thrownewInvariantViolation( __( 'WP_Query has been modified by a plugin or theme to suppress_filters, which will cause issues with WPGraphQL Execution. If you need to suppress filters for a specific reason within GraphQL, consider registering a custom field to the WPGraphQL Schema with a custom resolver.', 'wp-graphql' ) );
0 commit comments