Skip to content

Commit 76a5dc9

Browse files
committed
fix: handle callables/resolved types when comparing interface args
1 parent 7ee16f8 commit 76a5dc9

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/Type/WPInterfaceTrait.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ private function merge_field_args( string $field_name, array $field_args, array
248248
}
249249

250250
// Check if the interface arg type is different from the new field arg type.
251-
$field_arg_type = $this->field_arg_type_to_string( $config['type'] );
252-
$interface_arg_type = $merged_args[ $arg_name ]['type']();
251+
$field_arg_type = $this->normalize_type_name( $config['type'] );
252+
$interface_arg_type = $this->normalize_type_name( $merged_args[ $arg_name ]['type'] );
253253

254254
if ( ! empty( $field_arg_type ) && $interface_arg_type !== $field_arg_type ) {
255255
graphql_debug(
@@ -281,14 +281,26 @@ private function merge_field_args( string $field_name, array $field_args, array
281281
*
282282
* This is used for optimistic comparison of the arg types.
283283
*
284-
* @param string|array<string,mixed>|mixed $type A GraphQL Type
284+
* @param string|array<string,mixed>|callable|\GraphQL\Type\Definition\Type $type The type to normalize.
285285
*/
286-
private function field_arg_type_to_string( $type ): string {
287-
// Bail if the type is empty.
286+
private function normalize_type_name( $type ): string {
287+
// Bail early if the type is empty.
288288
if ( empty( $type ) ) {
289289
return '';
290-
} elseif ( is_string( $type ) ) {
291-
// If the type is already a string, return it as is.
290+
}
291+
292+
// If the type is a callable, we need to resolve it.
293+
if ( is_callable( $type ) ) {
294+
$type = $type();
295+
}
296+
297+
// If the type is an instance of a Type, we can get the name.
298+
if ( $type instanceof \GraphQL\Type\Definition\Type ) {
299+
$type = $type->name;
300+
}
301+
302+
// If the type is *now* a string, we can return it.
303+
if ( is_string( $type ) ) {
292304
return $type;
293305
} elseif ( ! is_array( $type ) ) {
294306
// If the type is not an array, we can't do anything with it.
@@ -299,12 +311,14 @@ private function field_arg_type_to_string( $type ): string {
299311
$output = '';
300312
$modifier = array_keys( $type )[0];
301313
$type = $type[ $modifier ];
314+
315+
// Convert the type wrappers to a string, and recursively get the internals.
302316
switch ( $modifier ) {
303317
case 'list_of':
304-
$output = '[' . $this->field_arg_type_to_string( $type ) . ']';
318+
$output = '[' . $this->normalize_type_name( $type ) . ']';
305319
break;
306320
case 'non_null':
307-
$output = '!' . $this->field_arg_type_to_string( $type );
321+
$output = '!' . $this->normalize_type_name( $type );
308322
break;
309323
}
310324

0 commit comments

Comments
 (0)