Repository: wp-graphql/wp-graphql (PHP)

Issue:
rename_graphql_field doesn't work properly for connections
The new `rename_graphql_field` API doesn't work properly for connections. 

## Steps to Reproduce

Add the following code to functions.php or custom plugin code:

```php
add_action( 'graphql_register_types', function() {

  rename_graphql_field( 'RootQuery', 'users', 'wpUsers' );

});
```

### Expectation

The `RootQuery->Users` connections should be named `wpUsers` and I should be able to query: 

```graphql
{
  wpUsers {
    nodes {
       id
       name
    }
  }
}
```

### Actual Behavior

There is no `wpUsers` field added to the Schema. The `RootQuery.users` field remains in the Schema.

## More Info

This appears to be related largely to these filters: 

- https://github.com/wp-graphql/wp-graphql/blob/develop/src/Type/WPObjectType.php#L209
- https://github.com/wp-graphql/wp-graphql/blob/develop/src/Type/WPObjectType.php#L227
- https://github.com/wp-graphql/wp-graphql/blob/develop/src/Type/WPObjectType.php#L239

When the filter to rename fields runs, not all fields (connection fields, for example) have been registered to their connecting Type, so there's no field to rename yet. 

I believe we should formally deprecate the latter 2 filters in favor of the former. This way there's one central filter used to filter fields, instead of 3 different ones. 

This will allow us to better use priorities with filters to determine what's filtered early/later, vs using separate filters altogether.

We should be able to do this in a backward compatible way by having the latter 2 filters hook into the first one, instead of running _after_ the first one. 

There's likely more to the issue than these filters, but I think this is some good starting context.




Discussion: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed because it has not had recent activity.  If you believe this issue is still valid, please open a new issue and mark this as a related issue.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@jasonbahl I'm not entirely sure the relevance of the `graphql_${lc_type_name}_fields` and `graphql_${uc_type_name_fields` to this issue (though I'm 💯 on board for dropping them).

Ironically, #2945 fixes the bug by _adding_ a filter, but I've marked it as internal since "This way there's one central filter" for users (in this case `graphql_wp_connection_type_config` ) is still a good guiding rule.

Up to you if you want to parcel off the aforementioned filter deprecations to a separate issue (so they dont get lost when #2945 closes out this one ), or if #331 is enough.



Fix (patch):
diff --git a/access-functions.php b/access-functions.php
index c4e96eb4f..2cf757976 100644
--- a/access-functions.php
+++ b/access-functions.php
@@ -402,15 +402,34 @@ static function ( TypeRegistry $type_registry ) use ( $connection_name, $fields
  * @since 1.3.4
  */
 function rename_graphql_field( string $type_name, string $field_name, string $new_field_name ) {
+	// Rename fields on the type.
 	add_filter(
 		"graphql_{$type_name}_fields",
 		static function ( $fields ) use ( $field_name, $new_field_name ) {
+			// Bail if the field doesn't exist.
+			if ( ! isset( $fields[ $field_name ] ) ) {
+				return $fields;
+			}
+
 			$fields[ $new_field_name ] = $fields[ $field_name ];
 			unset( $fields[ $field_name ] );
 
 			return $fields;
 		}
 	);
+
+	// Rename fields registered to the type by connections.
+	add_filter(
+		"graphql_wp_connection_{$type_name}_from_field_name",
+		static function ( $old_field_name ) use ( $field_name, $new_field_name ) {
+			// Bail if the field name doesn't match.
+			if ( $old_field_name !== $field_name ) {
+				return $old_field_name;
+			}
+
+			return $new_field_name;
+		}
+	);
 }
 
 /**
diff --git a/src/Type/WPConnectionType.php b/src/Type/WPConnectionType.php
index 819ae1ab7..9883ccc3f 100644
--- a/src/Type/WPConnectionType.php
+++ b/src/Type/WPConnectionType.php
@@ -150,10 +150,19 @@ public function __construct( array $config, TypeRegistry $type_registry ) {
 
 		$this->validate_config( $config );
 
-		$this->config          = $config;
-		$this->from_type       = $config['fromType'];
-		$this->to_type         = $config['toType'];
-		$this->from_field_name = $config['fromFieldName'];
+		$this->config    = $config;
+		$this->from_type = $config['fromType'];
+		$this->to_type   = $config['toType'];
+
+		/**
+		 * Filter the connection field name.
+		 *
+		 * @internal This filter is internal and used by rename_graphql_field(). It is not intended for use by external code.
+		 *
+		 * @param string $from_field_name The name of the field the connection will be exposed as.
+		 */
+		$this->from_field_name = apply_filters( "graphql_wp_connection_{$this->from_type}_from_field_name", $config['fromFieldName'] );
+
 		$this->connection_name = ! empty( $config['connectionTypeName'] ) ? $config['connectionTypeName'] : $this->get_connection_name( $this->from_type, $this->to_type, $this->from_field_name );
 
 		/**