Skip to content

Commit

Permalink
Merge pull request #155 from wp-graphql/fix/153-show_in_graphql
Browse files Browse the repository at this point in the history
fix: "show in graphql" setting on fields not respecting being turned "off"
  • Loading branch information
jasonbahl committed Jan 19, 2024
2 parents e2b2ac3 + 19e3bdb commit 65a8384
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Admin/OptionsPageRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function add_registration_fields( array $args, array $post ): array {
$show_in_graphql = false;

if ( isset( $args['show_in_graphql'] ) ) {
$show_in_graphql = $args['show_in_graphql'];
$show_in_graphql = (bool) $args['show_in_graphql'];
} elseif ( isset( $post['show_in_graphql'] ) ) {
$show_in_graphql = $post['show_in_graphql'];
$show_in_graphql = (bool) $post['show_in_graphql'];
}

$args['show_in_graphql'] = $show_in_graphql;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function display_graphql_field_group_fields( $field_group ): void {
'type' => 'text',
'prefix' => 'acf_field_group',
'name' => 'graphql_field_name',
'required' => isset( $field_group['show_in_graphql'] ) && $field_group['show_in_graphql'],
'required' => isset( $field_group['show_in_graphql'] ) && (bool) $field_group['show_in_graphql'],
'placeholder' => __( 'FieldGroupTypeName', 'wpgraphql-acf' ),
'value' => ! empty( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : '',
],
Expand Down
2 changes: 1 addition & 1 deletion src/FieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function get_graphql_field_config(): ?array {

// if the field is explicitly set to not show in graphql, leave it out of the schema
// if the field is explicitly set to not show in graphql, leave it out of the schema
if ( isset( $this->acf_field['show_in_graphql'] ) && false === $this->acf_field['show_in_graphql'] ) {
if ( isset( $this->acf_field['show_in_graphql'] ) && false === (bool) $this->acf_field['show_in_graphql'] ) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/LocationRules/LocationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public function determine_block_rules( string $field_group_name, string $param,
}

$acf_block = acf_get_block_type( $value );
if ( ! isset( $acf_block['show_in_graphql'] ) || false === $acf_block['show_in_graphql'] ) {
if ( ! isset( $acf_block['show_in_graphql'] ) || false === (bool) $acf_block['show_in_graphql'] ) {
return;
}
$type_name = isset( $acf_block['graphql_field_name'] ) ? Utils::format_type_name( $acf_block['graphql_field_name'] ) : Utils::format_type_name( $acf_block['name'] );
Expand Down Expand Up @@ -903,7 +903,7 @@ public function determine_options_rules( string $field_group_name, string $param

// Get the options page to unset
$options_page = acf_get_options_page( $value );
if ( ! isset( $options_page['show_in_graphql'] ) || false === $options_page['show_in_graphql'] ) {
if ( ! isset( $options_page['show_in_graphql'] ) || false === (bool) $options_page['show_in_graphql'] ) {
return;
}
if ( ! empty( $options_page['graphql_single_name'] ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static function should_field_group_show_in_graphql( array $acf_field_grou
$acf_field_group['show_in_graphql'] = $show_in_rest;
}

if ( isset( $acf_field_group['show_in_graphql'] ) && false === $acf_field_group['show_in_graphql'] ) {
if ( isset( $acf_field_group['show_in_graphql'] ) && false === (bool) $acf_field_group['show_in_graphql'] ) {
$should = false;
}

Expand Down
39 changes: 39 additions & 0 deletions tests/_support/WPUnit/AcfFieldTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,45 @@ public function testFieldShowsInSchemaIfShowInGraphqlIsNull() {

}

public function testFieldDoesNotShowInSchemaIfShowInGraphqlIsZero() {

$field_key = $this->register_acf_field([
'show_in_graphql' => 0
]);

$query = '
query GetType( $name: String! ) {
__type( name: $name ) {
name
fields {
name
}
}
}
';

$actual = $this->graphql( [
'query' => $query,
'variables' => [
'name' => 'AcfTestGroup',
]
]);

codecept_debug( $actual );

// the query should succeed
self::assertQuerySuccessful( $actual, [
// the instructions should be used for the description
$this->not()->expectedNode( '__type.fields', [
'name' => $this->get_formatted_field_name(),
]),
] );

// remove the local field
acf_remove_local_field( $field_key );

}

public function testFieldDoesNotShowInSchemaIfShowInGraphqlIsFalse() {

$field_key = $this->register_acf_field([
Expand Down

0 comments on commit 65a8384

Please sign in to comment.