diff --git a/src/class-config.php b/src/class-config.php index d13003c..eeaa1a3 100644 --- a/src/class-config.php +++ b/src/class-config.php @@ -1458,7 +1458,7 @@ protected function add_acf_fields_to_graphql_types() { * to graphql */ if ( ! $this->should_field_group_show_in_graphql( $field_group ) ) { - return; + continue; } $graphql_types = array_unique( $field_group['graphql_types'] ); diff --git a/tests/wpunit/LocationRulesTest.php b/tests/wpunit/LocationRulesTest.php index 0578cde..5b1f155 100644 --- a/tests/wpunit/LocationRulesTest.php +++ b/tests/wpunit/LocationRulesTest.php @@ -671,4 +671,99 @@ public function testFieldGroupAssignedToAcfOptionsPageShowsInSchema() { } + /** + * @see: https://github.com/wp-graphql/wp-graphql-acf/issues/251 + * @throws Exception + */ + public function testOnlyFieldGroupsSetToShowInGraphqlAreInTheSchema() { + + $post_id = $this->factory()->post->create([ 'post_status' => 'publish' ]); + + /** + * Register a field group to a specific post type + */ + $this->register_acf_field_group([ + 'key' => 'doNotShowInGraphQL', + 'location' => [ + [ + [ + 'param' => 'post_type', + 'operator' => '==', + 'value' => 'post', + ], + ], + ], + 'show_in_graphql' => false, + 'graphql_field_name' => 'doNotShowInGraphQL', + 'graphql_types' => [ 'Post' ] + ]); + + $this->register_acf_field_group([ + 'key' => 'showInGraphqlTest', + 'location' => [ + [ + [ + 'param' => 'post_type', + 'operator' => '==', + 'value' => 'post', + ], + ], + ], + 'show_in_graphql' => true, + 'graphql_field_name' => 'showInGraphqlTest', + 'graphql_types' => [ 'Post' ] + ]); + + $query = ' + query GetPost($id:ID!) { + post(id:$id idType:DATABASE_ID) { + databaseId + doNotShowInGraphQL { + __typename + } + } + } + '; + + $actual = graphql([ + 'query' => $query, + 'variables' => [ + 'id' => $post_id, + ], + ]); + + codecept_debug( $actual ); + + // doNotShowInGraphQL should not be in the Schema, so this should be an error + $this->assertArrayHasKey( 'errors', $actual ); + + $query = ' + query GetPost($id:ID!) { + post(id:$id idType:DATABASE_ID) { + databaseId + showInGraphqlTest { + __typename + } + } + } + '; + + $actual = graphql([ + 'query' => $query, + 'variables' => [ + 'id' => $post_id, + ], + ]); + + codecept_debug( $actual ); + + // showInGraphqlTest should be queryable against the Post type in the Schema + $this->assertSame( $post_id, $actual['data']['post']['databaseId'] ); + $this->assertSame( 'Post_Showingraphqltest', $actual['data']['post']['showInGraphqlTest']['__typename'] ); + + acf_remove_local_field_group( 'doNotShowInGraphQL' ); + acf_remove_local_field_group( 'showInGraphqlTest' ); + + } + }