Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit c2120f6

Browse files
authored
Merge pull request #255 from wp-graphql/release/v0.5.1
Release/v0.5.1
2 parents 49bf955 + 50901fa commit c2120f6

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,41 @@ Setting the value of this field to "Yes" will show the field group in the WPGrap
9292

9393
##### Registering Fields in PHP
9494

95-
When registering ACF Fields in PHP, `@todo`
95+
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.
96+
97+
```
98+
function my_acf_add_local_field_groups() {
99+
100+
acf_add_local_field_group(array(
101+
'key' => 'group_1',
102+
'title' => 'My Group',
103+
'show_in_graphql' => true,
104+
'graphql_field_name' => 'myGroup',
105+
'fields' => array (
106+
array (
107+
'key' => 'field_1',
108+
'label' => 'Sub Title',
109+
'name' => 'sub_title',
110+
'type' => 'text',
111+
)
112+
),
113+
'location' => array (
114+
array (
115+
array (
116+
'param' => 'post_type',
117+
'operator' => '==',
118+
'value' => 'post',
119+
),
120+
),
121+
),
122+
));
123+
124+
}
125+
126+
add_action('acf/init', 'my_acf_add_local_field_groups');
127+
```
128+
129+
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.
96130

97131
## Supported Fields
98132

src/class-config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ protected function add_acf_fields_to_graphql_types() {
14581458
* to graphql
14591459
*/
14601460
if ( ! $this->should_field_group_show_in_graphql( $field_group ) ) {
1461-
return;
1461+
continue;
14621462
}
14631463

14641464
$graphql_types = array_unique( $field_group['graphql_types'] );

tests/wpunit/LocationRulesTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,99 @@ public function testFieldGroupAssignedToAcfOptionsPageShowsInSchema() {
671671

672672
}
673673

674+
/**
675+
* @see: https://github.com/wp-graphql/wp-graphql-acf/issues/251
676+
* @throws Exception
677+
*/
678+
public function testOnlyFieldGroupsSetToShowInGraphqlAreInTheSchema() {
679+
680+
$post_id = $this->factory()->post->create([ 'post_status' => 'publish' ]);
681+
682+
/**
683+
* Register a field group to a specific post type
684+
*/
685+
$this->register_acf_field_group([
686+
'key' => 'doNotShowInGraphQL',
687+
'location' => [
688+
[
689+
[
690+
'param' => 'post_type',
691+
'operator' => '==',
692+
'value' => 'post',
693+
],
694+
],
695+
],
696+
'show_in_graphql' => false,
697+
'graphql_field_name' => 'doNotShowInGraphQL',
698+
'graphql_types' => [ 'Post' ]
699+
]);
700+
701+
$this->register_acf_field_group([
702+
'key' => 'showInGraphqlTest',
703+
'location' => [
704+
[
705+
[
706+
'param' => 'post_type',
707+
'operator' => '==',
708+
'value' => 'post',
709+
],
710+
],
711+
],
712+
'show_in_graphql' => true,
713+
'graphql_field_name' => 'showInGraphqlTest',
714+
'graphql_types' => [ 'Post' ]
715+
]);
716+
717+
$query = '
718+
query GetPost($id:ID!) {
719+
post(id:$id idType:DATABASE_ID) {
720+
databaseId
721+
doNotShowInGraphQL {
722+
__typename
723+
}
724+
}
725+
}
726+
';
727+
728+
$actual = graphql([
729+
'query' => $query,
730+
'variables' => [
731+
'id' => $post_id,
732+
],
733+
]);
734+
735+
codecept_debug( $actual );
736+
737+
// doNotShowInGraphQL should not be in the Schema, so this should be an error
738+
$this->assertArrayHasKey( 'errors', $actual );
739+
740+
$query = '
741+
query GetPost($id:ID!) {
742+
post(id:$id idType:DATABASE_ID) {
743+
databaseId
744+
showInGraphqlTest {
745+
__typename
746+
}
747+
}
748+
}
749+
';
750+
751+
$actual = graphql([
752+
'query' => $query,
753+
'variables' => [
754+
'id' => $post_id,
755+
],
756+
]);
757+
758+
codecept_debug( $actual );
759+
760+
// showInGraphqlTest should be queryable against the Post type in the Schema
761+
$this->assertSame( $post_id, $actual['data']['post']['databaseId'] );
762+
$this->assertSame( 'Post_Showingraphqltest', $actual['data']['post']['showInGraphqlTest']['__typename'] );
763+
764+
acf_remove_local_field_group( 'doNotShowInGraphQL' );
765+
acf_remove_local_field_group( 'showInGraphqlTest' );
766+
767+
}
768+
674769
}

0 commit comments

Comments
 (0)