From 37bf25a7d256fb3bd4a217fa69f9dae3ba9d922c Mon Sep 17 00:00:00 2001 From: Casey James Perno Date: Wed, 17 Mar 2021 16:23:11 -0400 Subject: [PATCH 1/4] added docs for registering fields with php --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) 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 From d92f99a2489a8f6fb020b2d9dd694a023cc6b575 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 23 Apr 2021 08:28:42 -0600 Subject: [PATCH 2/4] - Update additional return statement to be a continue statement --- src/class-config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/class-config.php b/src/class-config.php index d13003c..0a9d447 100644 --- a/src/class-config.php +++ b/src/class-config.php @@ -200,7 +200,7 @@ protected function add_options_pages_to_schema() { global $acf_options_page; if ( ! isset( $acf_options_page ) ) { - return ; + return; } /** @@ -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'] ); From fd6f8eb21494690339faa1300bad22fcab45b16f Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 23 Apr 2021 09:08:23 -0600 Subject: [PATCH 3/4] - add failing test for https://github.com/wp-graphql/wp-graphql-acf/issues/251 --- src/class-config.php | 4 +- tests/wpunit/LocationRulesTest.php | 95 ++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/class-config.php b/src/class-config.php index 0a9d447..d13003c 100644 --- a/src/class-config.php +++ b/src/class-config.php @@ -200,7 +200,7 @@ protected function add_options_pages_to_schema() { global $acf_options_page; if ( ! isset( $acf_options_page ) ) { - return; + return ; } /** @@ -1458,7 +1458,7 @@ protected function add_acf_fields_to_graphql_types() { * to graphql */ if ( ! $this->should_field_group_show_in_graphql( $field_group ) ) { - continue; + return; } $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' ); + + } + } From 592d855cc347a49bae4dd241c8f523e963a206a6 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 23 Apr 2021 09:09:50 -0600 Subject: [PATCH 4/4] - Fix to pass the failing test --- src/class-config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'] );