Skip to content

Commit

Permalink
Issue #3401211 by daniel.bosen, IT-Cru: Possible break of Thunder Gra…
Browse files Browse the repository at this point in the history
…phQL schema with drupal/graphql:4.6.0
  • Loading branch information
dbosen authored Nov 28, 2023
1 parent 981e6b2 commit 86c108e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 15 additions & 5 deletions modules/thunder_gqls/src/Traits/ResolverHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions modules/thunder_gqls/tests/src/Functional/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<GQL
query (\$path: String!) {
page(path: \$path) {
... on Article {
channel {
name
}
}
}
}
GQL;

$variables = ['path' => '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']);
}

}

0 comments on commit 86c108e

Please sign in to comment.