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 #253 from jasonbahl/bug/#251-field-groups-not-in-g…
Browse files Browse the repository at this point in the history
…raphql-are-preventing-other-groups-from-showing

Bug/#251 - field groups not in graphql are preventing other groups from showing
  • Loading branch information
jasonbahl authored Apr 23, 2021
2 parents a0ec93c + 592d855 commit 50901fa
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
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 50901fa

Please sign in to comment.