Skip to content

Commit

Permalink
Merge 579f6e5 into 89ee7cf
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed May 2, 2024
2 parents 89ee7cf + 579f6e5 commit ce07465
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 98 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions src/Type/WPInterfaceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ protected function get_implemented_interfaces(): array {
return array_unique( $new_interfaces );
}

/**
* Given a type it will return a string representation of the type
*
* @param mixed $type A GraphQL Type
*
* @return string
*/
private function field_arg_type_to_string( $type ) {
$output = '';
if ( is_string( $type ) ) {
$output = $type;
} elseif ( is_array( $type ) && 2 === count( $type ) ) {
switch ( $type[0] ) {
case 'list_of':
$output = '[' . $this->field_arg_type_to_string( $type[1] ) . ']';
break;
case 'non_null':
$output = '!' . $this->field_arg_type_to_string( $type[1] );
break;
}
}

return $output;
}

/**
* Returns the fields for a Type, applying any missing fields defined on interfaces implemented on the type
*
Expand Down Expand Up @@ -164,6 +189,50 @@ protected function get_fields( array $config, TypeRegistry $type_registry ): arr
$new_field['description'] = $interface_fields[ $field_name ]['description'];
}

if ( empty( $new_field['args'] ) && ! empty( $interface_fields[ $field_name ]['args'] ) ) {
$new_field['args'] = $interface_fields[ $field_name ]['args'];
} elseif ( ! empty( $new_field['args'] ) && ! empty( $interface_fields[ $field_name ]['args'] ) ) {
// Set field args to the interface fields to be overwrite with the new field args.
$field_args = $interface_fields[ $field_name ]['args'];

foreach ( $new_field['args'] as $arg_name => $arg_definition ) {
$new_field_arg_type = $arg_definition['type'];
if ( empty( $field_args[ $arg_name ] ) ) {
$field_args[ $arg_name ] = $arg_definition;
continue;
}

$interface_field_arg = $field_args[ $arg_name ];

if ( empty( $interface_field_arg['type'] ) || $interface_field_arg['type'] !== $new_field_arg_type ) {
graphql_debug(
sprintf(
/* translators: 1: Object type name, 2: Field name, 3: Argument name, 4: Expected argument type, 5: Actual argument type. */
__(
'Interface field argument "%1$s.%2$s(%3$s:)" expected to be of type "%4$s" but got "%5$s". Please ensure the field arguments match the interface field arguments or rename the argument.',
'wp-graphql'
),
$config['name'],
$field_name,
$arg_name,
$this->field_arg_type_to_string( $interface_field_arg['type'] ),
$this->field_arg_type_to_string( $new_field_arg_type )
)
);
continue;
}

// Set the field args to the new field args.
$field_args[ $arg_name ] = $arg_definition;
}

$new_field['args'] = array_merge( $interface_fields[ $field_name ]['args'], $new_field['args'] );
}

if ( empty( $new_field['resolve'] ) && ! empty( $interface_fields[ $field_name ]['resolve'] ) ) {
$new_field['resolve'] = $interface_fields[ $field_name ]['resolve'];
}

if ( ! isset( $new_field['type'] ) ) {
if ( isset( $interface_fields[ $field_name ]['type'] ) ) {
$new_field['type'] = $interface_fields[ $field_name ]['type'];
Expand Down

0 comments on commit ce07465

Please sign in to comment.