Skip to content

Commit 471ca6a

Browse files
committed
dev: output GRAPHQL_DEBUG message if requested amount > connection limit
1 parent 683352d commit 471ca6a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Data/Connection/AbstractConnectionResolver.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,9 @@ public function get_node_by_id( $id ) {
415415
/**
416416
* Get_query_amount
417417
*
418-
* Returns the max between what was requested and what is defined as the $max_query_amount to
419-
* ensure that queries don't exceed unwanted limits when querying data.
418+
* Returns the max between what was requested and what is defined as the $max_query_amount to ensure that queries don't exceed unwanted limits when querying data.
419+
*
420+
* If the amount requested is greater than the max query amount, a debug message will be included in the GraphQL response.
420421
*
421422
* @return int
422423
* @throws \Exception
@@ -438,16 +439,25 @@ public function get_query_amount() {
438439
* @since 0.0.6
439440
*/
440441
$max_query_amount = apply_filters( 'graphql_connection_max_query_amount', 100, $this->source, $this->args, $this->context, $this->info );
442+
443+
$requested_amount = $this->get_amount_requested();
441444

442-
return min( $max_query_amount, absint( $this->get_amount_requested() ) );
445+
if ( $requested_amount > $max_query_amount ) {
446+
graphql_debug(
447+
sprintf( 'The number of items requested by the connection (%s) exceeds the max query amount. Only the first %s items will be returned.', $requested_amount, $max_query_amount ),
448+
[ 'connection' => static::class ]
449+
);
450+
}
451+
452+
return min( $max_query_amount, $requested_amount );
443453
}
444454

445455
/**
446456
* Get_amount_requested
447457
*
448458
* This checks the $args to determine the amount requested, and if
449459
*
450-
* @return int|null
460+
* @return int
451461
* @throws \GraphQL\Error\UserError If there is an issue with the pagination $args.
452462
*/
453463
public function get_amount_requested() {
@@ -495,7 +505,7 @@ public function get_amount_requested() {
495505
* @param int $amount the requested amount
496506
* @param \WPGraphQL\Data\Connection\AbstractConnectionResolver $resolver Instance of the connection resolver class
497507
*/
498-
return max( 0, apply_filters( 'graphql_connection_amount_requested', $amount_requested, $this ) );
508+
return (int) max( 0, apply_filters( 'graphql_connection_amount_requested', $amount_requested, $this ) );
499509
}
500510

501511
/**

tests/wpunit/PostObjectConnectionQueriesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ static function () {
195195

196196
$this->assertCount( 20, $actual['data']['posts']['edges'] );
197197
$this->assertTrue( $actual['data']['posts']['pageInfo']['hasNextPage'] );
198+
$this->assertStringContainsString( 'The number of items requested by the connection (150) exceeds the max query amount.', $actual['extensions']['debug'][0]['message'] );
198199
}
199200

200201
/**

0 commit comments

Comments
 (0)