Skip to content

Commit a3069cd

Browse files
committed
feat: register choices of acf fields as graphql enum types
1 parent fcf04c4 commit a3069cd

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

src/class-config.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,19 +602,21 @@ protected function register_graphql_field( string $type_name, string $field_name
602602
*
603603
* @see: https://github.com/wp-graphql/wp-graphql-acf/issues/25
604604
*/
605+
$enum_type = $this->register_choices_of_acf_fields_as_enum_type( $acf_field );
606+
$enum_type = ! empty( $enum_type ) ? $enum_type : 'String';
605607
if ( empty( $acf_field['multiple'] ) ) {
606608
if('array' === $acf_field['return_format'] ){
607-
$field_config['type'] = [ 'list_of' => 'String' ];
609+
$field_config['type'] = [ 'list_of' => $enum_type ];
608610
$field_config['resolve'] = function( $root ) use ( $acf_field) {
609611
$value = $this->get_acf_field_value( $root, $acf_field, true);
610612

611613
return ! empty( $value ) && is_array( $value ) ? $value : [];
612614
};
613615
}else{
614-
$field_config['type'] = 'String';
616+
$field_config['type'] = $enum_type;
615617
}
616618
} else {
617-
$field_config['type'] = [ 'list_of' => 'String' ];
619+
$field_config['type'] = [ 'list_of' => $enum_type ];
618620
$field_config['resolve'] = function( $root ) use ( $acf_field ) {
619621
$value = $this->get_acf_field_value( $root, $acf_field );
620622

@@ -623,7 +625,9 @@ protected function register_graphql_field( string $type_name, string $field_name
623625
}
624626
break;
625627
case 'radio':
626-
$field_config['type'] = 'String';
628+
$enum_type = $this->register_choices_of_acf_fields_as_enum_type( $acf_field );
629+
$enum_type = ! empty( $enum_type ) ? $enum_type : 'String';
630+
$field_config['type'] = $enum_type;
627631
break;
628632
case 'number':
629633
case 'range':
@@ -1501,4 +1505,42 @@ protected function add_acf_fields_to_graphql_types() {
15011505

15021506
}
15031507

1508+
public function register_choices_of_acf_fields_as_enum_type( array $acf_field ): string {
1509+
// If the field isn't a select or radio field, return an empty string.
1510+
if ( 'select' !== $acf_field['type'] && 'radio' !== $acf_field['type'] ) {
1511+
return '';
1512+
}
1513+
1514+
// Generate a unique name for the enum type using the field key.
1515+
$enum_type_name = ucfirst( self::camel_case( $acf_field['name'] ) ) . 'Enum';
1516+
if ( ! $this->type_registry->has_type( $enum_type_name ) ) {
1517+
// Initialize an empty array to hold your enum values.
1518+
$enum_values = [];
1519+
1520+
// Loop over the choices in the field and add them to the enum values array.
1521+
foreach ( $acf_field['choices'] as $key => $choice ) {
1522+
// Use the sanitize_key function to create a valid enum name from the choice key.
1523+
$enum_key = strtoupper( sanitize_key( $key ) );
1524+
1525+
// Add the choice to the enum values array.
1526+
$enum_values[ $enum_key ] = [
1527+
'value' => $key,
1528+
'description' => $choice,
1529+
];
1530+
}
1531+
1532+
// Register enum type.
1533+
$this->type_registry->register_enum_type(
1534+
$enum_type_name,
1535+
[
1536+
'description' => $acf_field['label'],
1537+
'values' => $enum_values,
1538+
]
1539+
);
1540+
}
1541+
1542+
// Return the enum type name.
1543+
return $enum_type_name;
1544+
}
1545+
15041546
}

src/class-mutations.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,27 @@ private function register_graphql_field( array $config, string $parent_type_name
316316
case 'wysiwyg':
317317
case 'url':
318318
case 'textarea':
319-
case 'radio':
320319
$field_config['type'] = 'String';
321320
break;
321+
322+
case 'radio':
323+
$enum_type = $this->config->register_choices_of_acf_fields_as_enum_type( $acf_field );
324+
$enum_type = ! empty( $enum_type ) ? $enum_type : 'String';
325+
$field_config['type'] = $enum_type;
326+
break;
322327
case 'select':
323328

324329
/**
325330
* If the select field is configured to not allow multiple values
326331
* the field will accept a string, but if it is configured to allow
327332
* multiple values it will accept a list of strings
328333
*/
334+
$enum_type = $this->config->register_choices_of_acf_fields_as_enum_type( $acf_field );
335+
$enum_type = ! empty( $enum_type ) ? $enum_type : 'String';
329336
if ( empty( $acf_field['multiple'] ) ) {
330-
$field_config['type'] = 'String';
337+
$field_config['type'] = $enum_type;
331338
} else {
332-
$field_config['type'] = [ 'list_of' => 'String' ];
339+
$field_config['type'] = [ 'list_of' => $enum_type ];
333340
}
334341
break;
335342
case 'number':

0 commit comments

Comments
 (0)