Skip to content

Commit 2d5c97e

Browse files
authored
Merge pull request #3101 from wp-graphql/chore/connection-resolvers-cleanup
chore: connection resolvers cleanup
2 parents 36f3a7e + f7309af commit 2d5c97e

14 files changed

+421
-237
lines changed

src/Data/Connection/AbstractConnectionResolver.php

Lines changed: 326 additions & 149 deletions
Large diffs are not rendered by default.

src/Data/Connection/CommentConnectionResolver.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public function get_query_args() {
2929
/**
3030
* Prepare for later use
3131
*/
32-
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
33-
$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
32+
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
3433

3534
$query_args = [];
3635

@@ -49,7 +48,7 @@ public function get_query_args() {
4948
*
5049
* @since 0.0.6
5150
*/
52-
$query_args['number'] = min( max( absint( $first ), absint( $last ), 10 ), $this->get_query_amount() ) + 1;
51+
$query_args['number'] = $this->get_query_amount() + 1;
5352

5453
/**
5554
* Set the default order
@@ -158,7 +157,7 @@ public function get_query() {
158157
/**
159158
* {@inheritDoc}
160159
*/
161-
public function get_loader_name() {
160+
protected function loader_name(): string {
162161
return 'comment';
163162
}
164163

@@ -183,7 +182,7 @@ public function get_ids_from_query() {
183182
* @return array<string,mixed>
184183
*/
185184
public function get_args(): array {
186-
$args = $this->args;
185+
$args = $this->get_unfiltered_args();
187186

188187
if ( ! empty( $args['where'] ) ) {
189188
// Ensure all IDs are converted to database IDs.

src/Data/Connection/ContentTypeConnectionResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function get_query() {
6161
/**
6262
* {@inheritDoc}
6363
*/
64-
public function get_loader_name() {
64+
protected function loader_name(): string {
6565
return 'post_type';
6666
}
6767

src/Data/Connection/EnqueuedScriptsConnectionResolver.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11
<?php
22
namespace WPGraphQL\Data\Connection;
33

4-
use GraphQL\Type\Definition\ResolveInfo;
5-
use WPGraphQL\AppContext;
6-
74
/**
85
* Class EnqueuedScriptsConnectionResolver
96
*
107
* @package WPGraphQL\Data\Connection
118
*/
129
class EnqueuedScriptsConnectionResolver extends AbstractConnectionResolver {
13-
14-
/**
15-
* {@inheritDoc}
16-
*/
17-
public function __construct( $source, array $args, AppContext $context, ResolveInfo $info ) {
18-
19-
/**
20-
* Filter the query amount to be 1000 for
21-
*/
22-
add_filter(
23-
'graphql_connection_max_query_amount',
24-
static function ( $max, $source, $args, $context, ResolveInfo $info ) {
25-
if ( 'enqueuedScripts' === $info->fieldName || 'registeredScripts' === $info->fieldName ) {
26-
return 1000;
27-
}
28-
return $max;
29-
},
30-
10,
31-
5
32-
);
33-
34-
parent::__construct( $source, $args, $context, $info );
35-
}
36-
3710
/**
3811
* {@inheritDoc}
3912
*/
@@ -72,10 +45,17 @@ public function get_query() {
7245
/**
7346
* {@inheritDoc}
7447
*/
75-
public function get_loader_name() {
48+
protected function loader_name(): string {
7649
return 'enqueued_script';
7750
}
7851

52+
/**
53+
* {@inheritDoc}
54+
*/
55+
protected function max_query_amount(): int {
56+
return 1000;
57+
}
58+
7959
/**
8060
* {@inheritDoc}
8161
*

src/Data/Connection/EnqueuedStylesheetConnectionResolver.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?php
22
namespace WPGraphQL\Data\Connection;
33

4-
use GraphQL\Type\Definition\ResolveInfo;
5-
use WPGraphQL\AppContext;
6-
74
/**
85
* Class EnqueuedStylesheetConnectionResolver
96
*
@@ -17,29 +14,6 @@ class EnqueuedStylesheetConnectionResolver extends AbstractConnectionResolver {
1714
*/
1815
protected $query;
1916

20-
/**
21-
* {@inheritDoc}
22-
*/
23-
public function __construct( $source, array $args, AppContext $context, ResolveInfo $info ) {
24-
25-
/**
26-
* Filter the query amount to be 1000 for
27-
*/
28-
add_filter(
29-
'graphql_connection_max_query_amount',
30-
static function ( $max, $source, $args, $context, ResolveInfo $info ) {
31-
if ( 'enqueuedStylesheets' === $info->fieldName || 'registeredStylesheets' === $info->fieldName ) {
32-
return 1000;
33-
}
34-
return $max;
35-
},
36-
10,
37-
5
38-
);
39-
40-
parent::__construct( $source, $args, $context, $info );
41-
}
42-
4317
/**
4418
* {@inheritDoc}
4519
*/
@@ -78,10 +52,17 @@ public function get_query() {
7852
/**
7953
* {@inheritDoc}
8054
*/
81-
public function get_loader_name() {
55+
protected function loader_name(): string {
8256
return 'enqueued_stylesheet';
8357
}
8458

59+
/**
60+
* {@inheritDoc}
61+
*/
62+
protected function max_query_amount(): int {
63+
return 1000;
64+
}
65+
8566
/**
8667
* {@inheritDoc}
8768
*

src/Data/Connection/MenuItemConnectionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function get_query_args() {
8080
* {@inheritDoc}
8181
*/
8282
public function get_args(): array {
83-
$args = $this->args;
83+
$args = $this->get_unfiltered_args();
8484

8585
if ( ! empty( $args['where'] ) ) {
8686
// Ensure all IDs are converted to database IDs.
@@ -106,6 +106,6 @@ public function get_args(): array {
106106
*
107107
* @since 1.11.0
108108
*/
109-
return apply_filters( 'graphql_menu_item_connection_args', $args, $this->args );
109+
return apply_filters( 'graphql_menu_item_connection_args', $args, $this->get_unfiltered_args() );
110110
}
111111
}

src/Data/Connection/PluginConnectionResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static function ( $plugin ) use ( $s ) {
225225
/**
226226
* {@inheritDoc}
227227
*/
228-
public function get_loader_name() {
228+
protected function loader_name(): string {
229229
return 'plugin';
230230
}
231231

src/Data/Connection/PostObjectConnectionResolver.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
6969
/**
7070
* {@inheritDoc}
7171
*/
72-
public function get_loader_name() {
72+
protected function loader_name(): string {
7373
return 'post';
7474
}
7575

@@ -149,8 +149,7 @@ public function get_query_args() {
149149
/**
150150
* Prepare for later use
151151
*/
152-
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
153-
$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
152+
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
154153

155154
$query_args = [];
156155
/**
@@ -176,10 +175,10 @@ public function get_query_args() {
176175
/**
177176
* Set posts_per_page the highest value of $first and $last, with a (filterable) max of 100
178177
*/
179-
$query_args['posts_per_page'] = $this->one_to_one ? 1 : min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1;
178+
$query_args['posts_per_page'] = $this->one_to_one ? 1 : $this->get_query_amount() + 1;
180179

181180
// set the graphql cursor args
182-
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';
181+
$query_args['graphql_cursor_compare'] = ! empty( $last ) ? '>' : '<';
183182
$query_args['graphql_after_cursor'] = $this->get_after_offset();
184183
$query_args['graphql_before_cursor'] = $this->get_before_offset();
185184

@@ -533,7 +532,7 @@ static function ( $status ) use ( $post_type_objects ) {
533532
* {@inheritDoc}
534533
*/
535534
public function get_args(): array {
536-
$args = $this->args;
535+
$args = $this->get_unfiltered_args();
537536

538537
if ( ! empty( $args['where'] ) ) {
539538
// Ensure all IDs are converted to database IDs.
@@ -580,7 +579,7 @@ static function ( $id ) {
580579
*
581580
* @since 1.11.0
582581
*/
583-
return apply_filters( 'graphql_post_object_connection_args', $args, $this, $this->args );
582+
return apply_filters( 'graphql_post_object_connection_args', $args, $this, $this->get_unfiltered_args() );
584583
}
585584

586585
/**

src/Data/Connection/TaxonomyConnectionResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function get_query() {
6262
/**
6363
* {@inheritDoc}
6464
*/
65-
public function get_loader_name() {
65+
protected function loader_name(): string {
6666
return 'taxonomy';
6767
}
6868

src/Data/Connection/TermObjectConnectionResolver.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public function get_query_args() {
5959
/**
6060
* Prepare for later use
6161
*/
62-
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
63-
$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
62+
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
6463

6564
/**
6665
* Set hide_empty as false by default
@@ -70,7 +69,7 @@ public function get_query_args() {
7069
/**
7170
* Set the number, ensuring it doesn't exceed the amount set as the $max_query_amount
7271
*/
73-
$query_args['number'] = min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1;
72+
$query_args['number'] = $this->get_query_amount() + 1;
7473

7574
/**
7675
* Don't calculate the total rows, it's not needed and can be expensive
@@ -174,7 +173,7 @@ public function get_ids_from_query() {
174173
/**
175174
* {@inheritDoc}
176175
*/
177-
public function get_loader_name() {
176+
protected function loader_name(): string {
178177
return 'term';
179178
}
180179

@@ -243,7 +242,7 @@ public function sanitize_input_fields() {
243242
* {@inheritDoc}
244243
*/
245244
public function get_args(): array {
246-
$args = $this->args;
245+
$args = $this->get_unfiltered_args();
247246

248247
if ( ! empty( $args['where'] ) ) {
249248
// Ensure all IDs are converted to database IDs.
@@ -284,7 +283,7 @@ static function ( $id ) {
284283
*
285284
* @since 1.11.0
286285
*/
287-
return apply_filters( 'graphql_term_object_connection_args', $args, $this, $this->args );
286+
return apply_filters( 'graphql_term_object_connection_args', $args, $this, $this->get_unfiltered_args() );
288287
}
289288

290289
/**

0 commit comments

Comments
 (0)