diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderPagesSchemaExtension.php b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderPagesSchemaExtension.php index f0c9802e1..cdcd5e66c 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderPagesSchemaExtension.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderPagesSchemaExtension.php @@ -57,7 +57,7 @@ protected function resolveFields(): void { ); $this->addFieldResolverIfNotExists($type, 'channel', - $this->builder->fromPath('entity', 'field_channel.entity') + $this->fromEntityReference('field_channel', NULL, FALSE) ); $this->addFieldResolverIfNotExists($type, 'tags', diff --git a/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php b/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php index 5dcaac0ec..d09fbca9e 100644 --- a/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php +++ b/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php @@ -54,14 +54,24 @@ protected function createResolverBuilder(): void { * Name of the filed. * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface|null $entity * Entity to get the field property. + * @param bool $multiValue + * Whether the field is returns multiple values. * - * @return \Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerProxy + * @return \Drupal\graphql\GraphQL\Resolver\Composite * The field data producer. */ - public function fromEntityReference(string $field, ResolverInterface $entity = NULL) { - return $this->builder->produce('entity_reference') - ->map('field', $this->builder->fromValue($field)) - ->map('entity', $entity ?: $this->builder->fromParent()); + public function fromEntityReference(string $field, ResolverInterface $entity = NULL, bool $multiValue = TRUE) { + return $this->builder->compose( + $this->builder->produce('entity_reference') + ->map('field', $this->builder->fromValue($field)) + ->map('entity', $entity ?: $this->builder->fromParent()), + $this->builder->callback(function ($parent) use ($multiValue) { + if ($multiValue) { + return $parent; + } + return $parent[0] ?? NULL; + }) + ); } /** diff --git a/modules/thunder_gqls/tests/src/Functional/SchemaTest.php b/modules/thunder_gqls/tests/src/Functional/SchemaTest.php index ebefb07e3..a74c5422a 100644 --- a/modules/thunder_gqls/tests/src/Functional/SchemaTest.php +++ b/modules/thunder_gqls/tests/src/Functional/SchemaTest.php @@ -178,4 +178,36 @@ public function testValidSchema(): void { $this->assertEmpty($validator->getMissingResolvers($server), "The schema 'thunder_graphql' contains types without a resolver."); } + /** + * Tests query of an unpublished channel. + */ + public function testLabelAccess(): void { + $this->loadTermByUuid('bfc251bc-de35-467d-af44-1f7a7012b845') + ->setUnpublished() + ->save(); + + $query = << 'duis-autem-vel-eum-iriure']; + $response = $this->query($query, Json::encode($variables)); + $this->assertEquals(200, $response->getStatusCode(), 'Response not 200'); + + $page = $this->jsonDecode($response->getBody()); + $this->assertArrayNotHasKey('errors', $page); + $this->assertArrayHasKey('data', $page); + $this->assertArrayHasKey('page', $page['data']); + $this->assertArrayHasKey('channel', $page['data']['page']); + $this->assertNull($page['data']['page']['channel']); + } + }