Skip to content

Commit

Permalink
fix(EntityLoad): Return NULL on NULL entity IDs when composing entity…
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed Sep 21, 2023
1 parent beef2fe commit 65f997c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,23 @@ public function __construct(
* Resolver.
*
* @param string $type
* @param string $id
* @param string|int|null $id
* @param string|null $language
* @param array|null $bundles
* @param bool|null $access
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
* @param string|null $accessOperation
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return \GraphQL\Deferred
* @return \GraphQL\Deferred|null
*/
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context): ?Deferred {
// If this data producer was composed to a field (entity reference) and
// there is no ID then we can return immediately.
if ($id === NULL) {
return NULL;
}

$resolver = $this->entityBuffer->add($type, $id);

return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {
Expand Down
3 changes: 3 additions & 0 deletions src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public function __construct(
* @return \GraphQL\Deferred
*/
public function resolve($type, array $ids, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
// Remove any NULL IDs.
$ids = array_filter($ids);

$resolver = $this->entityBuffer->add($type, $ids);

return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {
Expand Down
14 changes: 14 additions & 0 deletions tests/src/Kernel/DataProducer/EntityMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,18 @@ public function testResolveEntityLoadMultiple(): void {
], $nids);
}

/**
* Make sure that passing a NULL id does not produce any warnings.
*/
public function testResolveEntityLoadWithNullId(): void {
$result = $this->executeDataProducer('entity_load_multiple', [
'type' => $this->node1->getEntityTypeId(),
'ids' => [NULL],
'access' => TRUE,
'access_operation' => 'view',
]);

$this->assertSame([], $result);
}

}
12 changes: 12 additions & 0 deletions tests/src/Kernel/DataProducer/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,18 @@ public function testResolveTranslatedEntityLoad(): void {
$this->assertEquals('sit amet fr', $result->getTitle());
}

/**
* Make sure that passing a NULL id does not produce any warnings.
*/
public function testResolveEntityLoadWithNullId(): void {
$result = $this->executeDataProducer('entity_load', [
'type' => $this->node->getEntityTypeId(),
'id' => NULL,
]);

$this->assertNull($result);
}

/**
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityLoad::resolve
*/
Expand Down

0 comments on commit 65f997c

Please sign in to comment.