Skip to content

Commit 65a8384

Browse files
authored
Merge pull request #155 from wp-graphql/fix/153-show_in_graphql
fix: "show in graphql" setting on fields not respecting being turned "off"
2 parents e2b2ac3 + 19e3bdb commit 65a8384

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

src/Admin/OptionsPageRegistration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function add_registration_fields( array $args, array $post ): array {
3838
$show_in_graphql = false;
3939

4040
if ( isset( $args['show_in_graphql'] ) ) {
41-
$show_in_graphql = $args['show_in_graphql'];
41+
$show_in_graphql = (bool) $args['show_in_graphql'];
4242
} elseif ( isset( $post['show_in_graphql'] ) ) {
43-
$show_in_graphql = $post['show_in_graphql'];
43+
$show_in_graphql = (bool) $post['show_in_graphql'];
4444
}
4545

4646
$args['show_in_graphql'] = $show_in_graphql;

src/Admin/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function display_graphql_field_group_fields( $field_group ): void {
231231
'type' => 'text',
232232
'prefix' => 'acf_field_group',
233233
'name' => 'graphql_field_name',
234-
'required' => isset( $field_group['show_in_graphql'] ) && $field_group['show_in_graphql'],
234+
'required' => isset( $field_group['show_in_graphql'] ) && (bool) $field_group['show_in_graphql'],
235235
'placeholder' => __( 'FieldGroupTypeName', 'wpgraphql-acf' ),
236236
'value' => ! empty( $field_group['graphql_field_name'] ) ? $field_group['graphql_field_name'] : '',
237237
],

src/FieldConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function get_graphql_field_config(): ?array {
186186

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

src/LocationRules/LocationRules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ public function determine_block_rules( string $field_group_name, string $param,
850850
}
851851

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

904904
// Get the options page to unset
905905
$options_page = acf_get_options_page( $value );
906-
if ( ! isset( $options_page['show_in_graphql'] ) || false === $options_page['show_in_graphql'] ) {
906+
if ( ! isset( $options_page['show_in_graphql'] ) || false === (bool) $options_page['show_in_graphql'] ) {
907907
return;
908908
}
909909
if ( ! empty( $options_page['graphql_single_name'] ) ) {

src/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static function should_field_group_show_in_graphql( array $acf_field_grou
277277
$acf_field_group['show_in_graphql'] = $show_in_rest;
278278
}
279279

280-
if ( isset( $acf_field_group['show_in_graphql'] ) && false === $acf_field_group['show_in_graphql'] ) {
280+
if ( isset( $acf_field_group['show_in_graphql'] ) && false === (bool) $acf_field_group['show_in_graphql'] ) {
281281
$should = false;
282282
}
283283

tests/_support/WPUnit/AcfFieldTestCase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,45 @@ public function testFieldShowsInSchemaIfShowInGraphqlIsNull() {
551551

552552
}
553553

554+
public function testFieldDoesNotShowInSchemaIfShowInGraphqlIsZero() {
555+
556+
$field_key = $this->register_acf_field([
557+
'show_in_graphql' => 0
558+
]);
559+
560+
$query = '
561+
query GetType( $name: String! ) {
562+
__type( name: $name ) {
563+
name
564+
fields {
565+
name
566+
}
567+
}
568+
}
569+
';
570+
571+
$actual = $this->graphql( [
572+
'query' => $query,
573+
'variables' => [
574+
'name' => 'AcfTestGroup',
575+
]
576+
]);
577+
578+
codecept_debug( $actual );
579+
580+
// the query should succeed
581+
self::assertQuerySuccessful( $actual, [
582+
// the instructions should be used for the description
583+
$this->not()->expectedNode( '__type.fields', [
584+
'name' => $this->get_formatted_field_name(),
585+
]),
586+
] );
587+
588+
// remove the local field
589+
acf_remove_local_field( $field_key );
590+
591+
}
592+
554593
public function testFieldDoesNotShowInSchemaIfShowInGraphqlIsFalse() {
555594

556595
$field_key = $this->register_acf_field([

0 commit comments

Comments
 (0)