Skip to content

Commit 56aa0f0

Browse files
committed
- update resolvers that use set_query_arg to pass certain args as $args _before_ the resolver is instantiated, and then use set_query_args to override some args after the resolver is instantiated. This allows for args such as in or include to be used by the ConnectionResolver in determining things like pagination and cursor slicing
1 parent dbd010c commit 56aa0f0

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

src/FieldType/Gallery.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ static function ( $id ) {
5858
$value
5959
) : $value;
6060

61+
if ( empty( $value ) ) {
62+
return null;
63+
}
64+
65+
$args['where']['in'] = $value;
6166
$resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'attachment' );
62-
return $resolver
63-
->set_query_arg( 'post__in', $value )
64-
->set_query_arg( 'orderby', 'post__in' )
65-
->get_connection();
67+
$resolver->set_query_arg( 'post_status', 'any' );
68+
return $resolver->get_connection();
6669
},
6770
]
6871
);

src/FieldType/Relationship.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,20 @@ static function ( $id ) {
9595
)
9696
);
9797

98-
$resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'any' );
98+
if ( empty( $ids ) ) {
99+
return null;
100+
}
99101

102+
// override the args to filter by a specific set of IDs
103+
$args['where']['in'] = $ids;
104+
$resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'any' );
100105

101106
if ( $is_one_to_one ) {
102107
$resolver = $resolver->one_to_one();
103108
}
104109

105-
return $resolver
106-
// the relationship field doesn't require related things to be published
107-
// so we set the status to "any"
108-
->set_query_arg( 'post_status', 'any' )
109-
->set_query_arg( 'post__in', $ids )
110-
->set_query_arg( 'orderby', 'post__in' )
111-
->get_connection();
110+
$resolver->set_query_arg( 'post_status', 'any' );
111+
return $resolver->get_connection();
112112
},
113113
];
114114

src/FieldType/Taxonomy.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ static function ( $id ) {
4444
return null;
4545
}
4646

47-
$resolver = new TermObjectConnectionResolver( $root, $args, $context, $info );
48-
return $resolver
49-
// Set the query to include only the IDs passed in from the field
50-
// and orderby the ids
51-
->set_query_arg( 'include', $ids )
52-
->set_query_arg( 'orderby', 'include' )
53-
->get_connection();
47+
$args['where']['include'] = $ids;
48+
49+
return ( new TermObjectConnectionResolver( $root, $args, $context, $info ) )->get_connection();
5450
},
5551
];
5652

src/FieldType/User.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,31 @@ public static function register_field_type(): void {
4242
$values = [];
4343
if ( ! is_array( $value ) ) {
4444
$values[] = $value;
45+
} else {
46+
$values = $value;
4547
}
4648

4749
$value = array_map(
4850
static function ( $user ) {
4951
if ( is_array( $user ) && isset( $user['ID'] ) ) {
5052
return absint( $user['ID'] );
5153
}
54+
if ( is_object( $user ) && isset( $user->ID ) ) {
55+
return absint( $user->ID );
56+
}
5257
return absint( $user );
5358
},
5459
$values
5560
);
5661

62+
if ( empty( $value ) ) {
63+
return null;
64+
}
65+
66+
$args['where']['include'] = $value;
5767
$resolver = new UserConnectionResolver( $root, $args, $context, $info );
58-
return $resolver->set_query_arg( 'include', $value )->set_query_arg( 'orderby', 'include' )->get_connection();
68+
$resolver->set_query_arg( 'orderby', 'include' );
69+
return $resolver->get_connection();
5970
},
6071
]
6172
);

0 commit comments

Comments
 (0)