diff --git a/README.md b/README.md index bf26740..f231b0f 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,41 @@ Setting the value of this field to "Yes" will show the field group in the WPGrap ##### Registering Fields in PHP -When registering ACF Fields in PHP, `@todo` +When registering ACF Fields in PHP, you need to add `show_in_graphql` and `graphql_field_name` when defining your field group. See below as an example. + +``` +function my_acf_add_local_field_groups() { + + acf_add_local_field_group(array( + 'key' => 'group_1', + 'title' => 'My Group', + 'show_in_graphql' => true, + 'graphql_field_name' => 'myGroup', + 'fields' => array ( + array ( + 'key' => 'field_1', + 'label' => 'Sub Title', + 'name' => 'sub_title', + 'type' => 'text', + ) + ), + 'location' => array ( + array ( + array ( + 'param' => 'post_type', + 'operator' => '==', + 'value' => 'post', + ), + ), + ), + )); + +} + +add_action('acf/init', 'my_acf_add_local_field_groups'); +``` + +Each individual field will inherit its GraphQL name from the supplied `name` tag. In this example, `sub_title` will become `subTitle` when requested through GraphQL. If you want more granular control, you can pass `graphql_field_name` to each individual field as well. ## Supported Fields 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' ); + + } + }