Skip to content

Commit

Permalink
- prevent graphql_debug message from being output if 2 connections ar…
Browse files Browse the repository at this point in the history
…e registered that have the same fieldName, toType and ConnectionTypeName

- add isConnectionField identifier to fields registered via WPConnectionType
- fix typo in docblock
  • Loading branch information
jasonbahl committed Apr 18, 2024
1 parent c0db0e9 commit 18018e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/Registry/TypeRegistry.php
Expand Up @@ -1089,7 +1089,28 @@ function ( $fields ) use ( $type_name, $field_name, $config ) {
return $fields;
}

// if a field has already been registered with the same name output a debug message
if ( isset( $fields[ $field_name ] ) ) {

// if the existing field is a connection type
// and the new field is also a connection type
// and the toType is the same for both
// then we can allow the duplicate field
if (
isset(
$fields[ $field_name ]['isConnectionField'],
$config['isConnectionField'],
$fields[ $field_name ]['toType'],
$config['toType'],
$fields[ $field_name ]['connectionTypeName'],
$config['connectionTypeName']
) &&
$fields[ $field_name ]['toType'] === $config['toType'] &&
$fields[ $field_name ]['connectionTypeName'] === $config['connectionTypeName']
) {
return $fields;
}

graphql_debug(
sprintf(
// translators: %1$s is the field name, %2$s is the type name.
Expand All @@ -1098,9 +1119,11 @@ function ( $fields ) use ( $type_name, $field_name, $config ) {
$type_name
),
[
'type' => 'DUPLICATE_FIELD',
'field_name' => $field_name,
'type_name' => $type_name,
'type' => 'DUPLICATE_FIELD',
'field_name' => $field_name,
'type_name' => $type_name,
'existing_field' => $fields[ $field_name ],
'duplicate_field' => $config,
]
);
return $fields;
Expand Down
1 change: 1 addition & 0 deletions src/Type/WPConnectionType.php
Expand Up @@ -515,6 +515,7 @@ public function register_connection_field(): void {
'type' => true === $this->one_to_one ? $this->connection_name . 'Edge' : $this->connection_name,
'args' => array_merge( $this->get_pagination_args(), $this->where_args ),
'auth' => $this->auth,
'isConnectionField' => true,
'deprecationReason' => ! empty( $this->config['deprecationReason'] ) ? $this->config['deprecationReason'] : null,
'description' => ! empty( $this->config['description'] )
? $this->config['description']
Expand Down
2 changes: 1 addition & 1 deletion src/Type/WPInterfaceType.php
Expand Up @@ -97,7 +97,7 @@ public function getInterfaces(): array {
public function prepare_fields( array $fields, string $type_name, array $config ): array {

/**
* Filter all object fields, passing the $typename as a param
* Filter all interface fields, passing the $typename as a param
*
* This is useful when several different types need to be easily filtered at once. . .for example,
* if ALL types with a field of a certain name needed to be adjusted, or something to that tune
Expand Down

0 comments on commit 18018e0

Please sign in to comment.