Skip to content

Conversation

@mohjak
Copy link

@mohjak mohjak commented Jul 26, 2023

This PR introduces a new feature to the wp-graphql-acf plugin that allows automatic registration of ACF select and radio fields as GraphQL Enum types. This feature enhances the plugin's type-safety.

The changes primarily consist of a new function register_choices_of_acf_fields_as_enum_type and modifications in the select and radio case block.

The register_choices_of_acf_fields_as_enum_type function takes an array representing an ACF field as input and checks if the field type is either 'select' or 'radio'. For such fields, the function generates a unique name and creates an array of Enum values using the provided choices in the field. If the enum type does not exist in the registry, it registers the enum type with its associated values and descriptions.

The modifications to the case block for the 'radio' type for example ensure that the newly registered enum type is used as the field's type.

case 'radio':
	$enum_type            = $this->register_choices_of_acf_fields_as_enum_type( $acf_field );
	$enum_type            = ! empty( $enum_type ) ? $enum_type : 'String';
	$field_config['type'] = $enum_type;
	break;

This change not only enhances the functionality of the wp-graphql-acf plugin but also aligns it more closely with GraphQL's philosophy of strong typing. As such, it is expected to increase the robustness and reliability of interactions with ACF select and radio fields via the GraphQL API.

The new feature has been thoroughly tested to ensure its seamless operation with existing functionalities.

Please review the changes and provide any feedback or questions you might have.

Copy link
Owner

@alaa-alshamy alaa-alshamy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the following notes:

  • You didn't take into account the case of having no choices, I think it will cause an error right now
  • It's better to make the function register_choices_of_acf_fields_as_enum_type return String if there's no choices or any error happened inside it
  • I think it's better to use the camel_case function to sanitize the key of the enum values
  • Please revert increasing the plugin vesrion I was wrong, I intendly didn't increase it before to can figure out from which version I took the clone and I just adding a new tag when finishing changes
  • It's better to rename the variable $enum_type to be $field_type because it's not always an enum

@mohjak
Copy link
Author

mohjak commented Jul 27, 2023

Finished the reported notes.

@alaa-alshamy alaa-alshamy merged commit b8c7a7b into alaa-alshamy:develop Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants