Skip to content

Commit

Permalink
Merge pull request #415 from justlevine/feat/narrow-choice-input-inte…
Browse files Browse the repository at this point in the history
…rface-types

feat!: Narrow `FormField.choices` and `.inputs` types to their implemations
  • Loading branch information
justlevine authored May 13, 2024
2 parents 36e6e2a + 0930429 commit 2be7e1e
Show file tree
Hide file tree
Showing 35 changed files with 202 additions and 240 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- feat!: Refactor `FormsConnectionResolver` and `EntriesConnectionResolver` for compatibility with WPGraphQL v1.26.0 improvements.
- feat!: Narrow `FormField.choices` and `FormField.inputs` field types to their implementations.
- chore!: Bump minimum WPGraphQL version to v1.26.0.
- chore!: Bump minimum WordPress version to v6.0.0.
- chore!: Bump minimum Gravity Forms version to v2.7.0.
Expand Down
3 changes: 3 additions & 0 deletions docs/querying-formfields.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ As of v0.10.0, all `formFields` have access to the `value` GraphQL field, which
checkboxValues {
inputId
value
... on CheckboxFieldChoice {
isSelected
}
}
}
... on NameField {
Expand Down
4 changes: 4 additions & 0 deletions src/Registry/FieldChoiceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use WPGraphQL\GF\Type\WPInterface\FieldChoice;
use WPGraphQL\GF\Utils\Utils;


/**
* Class - FieldChoiceRegistry
*/
Expand Down Expand Up @@ -88,6 +89,9 @@ static function () use ( $field, $settings, $as_interface ) {
register_graphql_object_type( $choice_name, $config );
}

// Overload the field type with the new choice type.
Utils::overload_graphql_field_type( $field->graphql_single_name, 'choices', [ 'list_of' => $choice_name ] );

// Store in static array to prevent duplicate registration.
self::$registered_types[] = $choice_name;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Registry/FieldInputRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ static function () use ( $field, $settings, $as_interface ) {
register_graphql_object_type( $input_name, $config );
}

Utils::overload_graphql_field_type( $field->graphql_single_name, 'inputs', [ 'list_of' => $input_name ] );

// Store in static array to prevent duplicate registration.
self::$registered_types[] = $input_name;
}
Expand Down
26 changes: 26 additions & 0 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,30 @@ protected static function get_database_id_from_id( $id, $type ): int {

return absint( $id_parts['id'] );
}

/**
* Overloads the field type of an existing GraphQL field.
*
* This is necessary because register_graphql_field() doesn't have a way to check inheritance.
*
* @see https://github.com/wp-graphql/wp-graphql/issues/3096
*
* @param string $object_type The WPGraphQL object type name where the field is located.
* @param string $field_name The field name to overload.
* @param string|array<string|string> $new_type_name The new GraphQL type name to use.
*/
public static function overload_graphql_field_type( string $object_type, string $field_name, $new_type_name ): void {
add_filter(
'graphql_' . $object_type . '_fields',
static function ( array $fields ) use ( $field_name, $new_type_name ) {
if ( isset( $fields[ $field_name ] ) ) {
$fields[ $field_name ]['type'] = $new_type_name;
}

return $fields;
},
10,
1
);
}
}
20 changes: 9 additions & 11 deletions tests/wpunit/AddressFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,15 @@ public function field_query(): string {
errorMessage
hasAutocomplete
inputs {
... on AddressInputProperty {
customLabel
defaultValue
id
isHidden
key
label
name
placeholder
autocompleteAttribute
}
customLabel
defaultValue
id
isHidden
key
label
name
placeholder
autocompleteAttribute
}
isRequired
label
Expand Down
22 changes: 7 additions & 15 deletions tests/wpunit/ChainedSelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,19 @@ public function field_query(): string {
canPrepopulate
chainedSelectsAlignment
choices {
... on ChainedSelectFieldChoice {
choices {
choices {
... on ChainedSelectFieldChoice {
choices {
... on ChainedSelectFieldChoice {
isSelected
text
value
}
}
isSelected
text
value
}
}
isSelected
text
value
}
isSelected
text
value
}
conditionalLogic {
actionType
Expand All @@ -174,11 +168,9 @@ public function field_query(): string {
hasChoiceValue
inputName
inputs {
... on ChainedSelectInputProperty {
label
id
name
}
label
id
name
}
isRequired
label
Expand Down
16 changes: 6 additions & 10 deletions tests/wpunit/CheckboxFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,9 @@ public function field_query(): string {
}
}
choices {
... on CheckboxFieldChoice {
isSelected
text
value
}
isSelected
text
value
}
conditionalLogic {
actionType
Expand All @@ -234,11 +232,9 @@ public function field_query(): string {
hasSelectAll
inputName
inputs {
... on CheckboxInputProperty {
id
label
name
}
id
label
name
}
isRequired
label
Expand Down
14 changes: 6 additions & 8 deletions tests/wpunit/DateFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ public function field_query(): string {
errorMessage
inputName
inputs {
... on DateInputProperty {
autocompleteAttribute
customLabel
defaultValue
id
label
placeholder
}
autocompleteAttribute
customLabel
defaultValue
id
label
placeholder
}
isRequired
label
Expand Down
16 changes: 7 additions & 9 deletions tests/wpunit/EmailFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,13 @@ public function field_query(): string {
hasAutocomplete
hasEmailConfirmation
inputs {
... on EmailInputProperty {
autocompleteAttribute
customLabel
defaultValue
id
label
name
placeholder
}
autocompleteAttribute
customLabel
defaultValue
id
label
name
placeholder
}
isRequired
label
Expand Down
2 changes: 0 additions & 2 deletions tests/wpunit/MultiSelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ public function field_query(): string {
adminLabel
canPrepopulate
choices {
... on MultiSelectFieldChoice {
isSelected
text
value
}
}
conditionalLogic {
actionType
Expand Down
30 changes: 14 additions & 16 deletions tests/wpunit/NameFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,21 @@ public function field_query(): string {
hasAutocomplete
inputName
inputs {
... on NameInputProperty {
autocompleteAttribute
choices {
isSelected
text
value
}
customLabel
defaultValue
hasChoiceValue
id
isHidden
key
label
name
placeholder
autocompleteAttribute
choices {
isSelected
text
value
}
customLabel
defaultValue
hasChoiceValue
id
isHidden
key
label
name
placeholder
}
isRequired
labelPlacement
Expand Down
20 changes: 8 additions & 12 deletions tests/wpunit/OptionCheckboxFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,11 @@ public function field_query(): string {
adminLabel
canPrepopulate
choices{
... on OptionFieldChoice {
formattedPrice
isSelected
price
text
value
}
formattedPrice
isSelected
price
text
value
}
cssClass
defaultValue
Expand Down Expand Up @@ -322,11 +320,9 @@ public function field_query(): string {
}
hasSelectAll
inputs {
... on OptionCheckboxInputProperty {
id
label
name
}
id
label
name
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions tests/wpunit/OptionRadioFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,12 @@ public function field_query(): string {
... on OptionField {
adminLabel
canPrepopulate
choices{
... on OptionFieldChoice {
formattedPrice
isSelected
price
text
value
}
choices {
formattedPrice
isSelected
price
text
value
}
cssClass
defaultValue
Expand Down
14 changes: 6 additions & 8 deletions tests/wpunit/OptionSelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,12 @@ public function field_query(): string {
... on OptionField {
adminLabel
canPrepopulate
choices{
... on OptionFieldChoice {
formattedPrice
isSelected
price
text
value
}
choices {
formattedPrice
isSelected
price
text
value
}
cssClass
defaultValue
Expand Down
16 changes: 6 additions & 10 deletions tests/wpunit/PostCategoryCheckboxFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,9 @@ public function field_query(): string {
adminLabel
canPrepopulate
choices {
... on PostCategoryFieldChoice {
isSelected
text
value
}
isSelected
text
value
}
conditionalLogic {
actionType
Expand Down Expand Up @@ -349,11 +347,9 @@ public function field_query(): string {
}
hasSelectAll
inputs {
... on PostCategoryCheckboxInputProperty {
id
label
name
}
id
label
name
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions tests/wpunit/PostCategoryMultiSelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@ public function field_query(): string {
adminLabel
canPrepopulate
choices {
... on PostCategoryFieldChoice {
isSelected
text
value
}
isSelected
text
value
}
conditionalLogic {
actionType
Expand Down
Loading

0 comments on commit 2be7e1e

Please sign in to comment.