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

Commit

Permalink
Merge pull request #255 from wp-graphql/release/v0.5.1
Browse files Browse the repository at this point in the history
Release/v0.5.1
  • Loading branch information
jasonbahl authored Apr 23, 2021
2 parents 49bf955 + 50901fa commit c2120f6
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand Down
95 changes: 95 additions & 0 deletions tests/wpunit/LocationRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

}

}

0 comments on commit c2120f6

Please sign in to comment.