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
@@ -257,22 +261,29 @@ abstract public function get_query_args();
257
261
abstractpublicfunctionget_query();
258
262
259
263
/**
260
-
* Should_execute
261
-
*
262
-
* Determine whether or not the query should execute.
263
-
*
264
-
* Return true to execute, return false to prevent execution.
265
-
*
266
-
* Various criteria can be used to determine whether a Connection Query should
267
-
* be executed.
264
+
* Used to determine whether the connection query should be executed. This is useful for short-circuiting the connection resolver before executing the query.
268
265
*
269
-
* For example, if a user is requesting revisions of a Post, and the user doesn't have
270
-
* permission to edit the post, they don't have permission to view the revisions, and therefore
271
-
* we can prevent the query to fetch revisions from executing in the first place.
266
+
* When `pre_should_excecute()` returns false, that's a sign the Resolver shouldn't execute the query. Otherwise, the more expensive logic logic in `should_execute()` will run later in the lifecycle.
272
267
*
273
-
* @return bool
268
+
* @param mixed $source Source passed down from the resolve tree
269
+
* @param array<string,mixed> $args Array of arguments input in the field as part of the GraphQL query.
270
+
* @param \WPGraphQL\AppContext $context The app context that gets passed down the resolve tree.
271
+
* @param \GraphQL\Type\Definition\ResolveInfo $info Info about fields passed down the resolve tree.
* If the source is a Post and the ID is empty (i.e. if the user doesn't have permissions to view the source), we should not execute the query.
278
+
*
279
+
* @todo This can probably be abstracted to check if _any_ source is private, and not just `PostObject` models.
280
+
*/
281
+
if ( $sourceinstanceof Post && empty( $source->ID ) ) {
282
+
$should_execute = false;
283
+
}
284
+
285
+
return$should_execute;
286
+
}
276
287
277
288
/**
278
289
* The maximum number of items that should be returned by the query.
@@ -322,6 +333,25 @@ public function get_ids_from_query() {
322
333
);
323
334
}
324
335
336
+
/**
337
+
* Determine whether or not the query should execute.
338
+
*
339
+
* Return true to exeucte, return false to prevent execution.
340
+
*
341
+
* Various criteria can be used to determine whether a Connection Query should be executed.
342
+
*
343
+
* For example, if a user is requesting revisions of a Post, and the user doesn't have permission to edit the post, they don't have permission to view the revisions, and therefore we can prevent the query to fetch revisions from executing in the first place.
344
+
*
345
+
* Runs only if `pre_should_execute()` returns true.
346
+
*
347
+
* @todo This is public for b/c but it should be protected.
348
+
*
349
+
* @return bool
350
+
*/
351
+
publicfunctionshould_execute() {
352
+
returntrue;
353
+
}
354
+
325
355
/**
326
356
* Returns the offset for a given cursor.
327
357
*
@@ -428,12 +458,6 @@ public function get_loader_name() {
428
458
return$this->loader_name;
429
459
}
430
460
431
-
/**
432
-
* Returns whether the connection should execute.
433
-
*/
434
-
publicfunctionget_should_execute(): bool {
435
-
return$this->should_execute;
436
-
}
437
461
438
462
/**
439
463
* Returns the $args passed to the connection, before any modifications.
@@ -496,6 +520,19 @@ public function get_query_amount() {
496
520
return$this->query_amount;
497
521
}
498
522
523
+
/**
524
+
* Returns whether the connection should execute.
525
+
*
526
+
* If conditions are met that should prevent the execution, we can bail from resolving early, before the query is executed.
527
+
*/
528
+
publicfunctionget_should_execute(): bool {
529
+
// If `pre_should_execute()` or other logic has yet to run, we should run the full `should_execute()` logic.
530
+
if ( ! isset( $this->should_execute ) ) {
531
+
$this->should_execute = $this->should_execute();
532
+
}
533
+
534
+
return$this->should_execute;
535
+
}
499
536
500
537
/**
501
538
* Returns an array of IDs for the connection.
@@ -649,6 +686,33 @@ public function one_to_one() {
649
686
return$this;
650
687
}
651
688
689
+
/**
690
+
* Gets whether or not the query should execute, BEFORE any data is fetched or altered, filtered by 'graphql_connection_pre_should_execute'.
691
+
*
692
+
* @param mixed $source The source that's passed down the GraphQL queries.
693
+
* @param array<string,mixed> $args The inputArgs on the field.
694
+
* @param \WPGraphQL\AppContext $context The AppContext passed down the GraphQL tree.
695
+
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down the GraphQL tree.
* Filters whether or not the query should execute, BEFORE any data is fetched or altered.
702
+
*
703
+
* This is evaluated based solely on the values passed to the constructor, before any data is fetched or altered, and is useful for shortcircuiting the Connection Resolver before any heavy logic is executed.
704
+
*
705
+
* For more in-depth checks, use the `graphql_connection_should_execute` filter instead.
706
+
*
707
+
* @param bool $should_execute Whether or not the query should execute.
708
+
* @param mixed $source The source that's passed down the GraphQL queries.
709
+
* @param array $args The inputArgs on the field.
710
+
* @param \WPGraphQL\AppContext $context The AppContext passed down the GraphQL tree.
711
+
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down the GraphQL tree.
0 commit comments