Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3401211 by daniel.bosen, IT-Cru: Possible break of Thunder GraphQL schema with drupal/graphql:4.6.0 #756

Merged
merged 11 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}

}