Skip to content

Commit 09c5478

Browse files
authored
fix(EntityLoad): Return NULL on NULL entity IDs when composing entity load (#1174)
1 parent 63dcdf7 commit 09c5478

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,23 @@ public function __construct(
133133
* Resolver.
134134
*
135135
* @param string $type
136-
* @param string $id
136+
* @param string|int|null $id
137137
* @param string|null $language
138138
* @param array|null $bundles
139139
* @param bool|null $access
140140
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
141141
* @param string|null $accessOperation
142142
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
143143
*
144-
* @return \GraphQL\Deferred
144+
* @return \GraphQL\Deferred|null
145145
*/
146-
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
146+
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context): ?Deferred {
147+
// If this data producer was composed to a field (entity reference) and
148+
// there is no ID then we can return immediately.
149+
if ($id === NULL) {
150+
return NULL;
151+
}
152+
147153
$resolver = $this->entityBuffer->add($type, $id);
148154

149155
return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {

src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public function __construct(
144144
* @return \GraphQL\Deferred
145145
*/
146146
public function resolve($type, array $ids, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
147+
// Remove any NULL IDs.
148+
$ids = array_filter($ids);
149+
147150
$resolver = $this->entityBuffer->add($type, $ids);
148151

149152
return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {

tests/src/Kernel/DataProducer/EntityMultipleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,18 @@ public function testResolveEntityLoadMultiple(): void {
111111
], $nids);
112112
}
113113

114+
/**
115+
* Make sure that passing a NULL id does not produce any warnings.
116+
*/
117+
public function testResolveEntityLoadWithNullId(): void {
118+
$result = $this->executeDataProducer('entity_load_multiple', [
119+
'type' => $this->node1->getEntityTypeId(),
120+
'ids' => [NULL],
121+
'access' => TRUE,
122+
'access_operation' => 'view',
123+
]);
124+
125+
$this->assertSame([], $result);
126+
}
127+
114128
}

tests/src/Kernel/DataProducer/EntityTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ public function testResolveTranslatedEntityLoad(): void {
366366
$this->assertEquals('sit amet fr', $result->getTitle());
367367
}
368368

369+
/**
370+
* Make sure that passing a NULL id does not produce any warnings.
371+
*/
372+
public function testResolveEntityLoadWithNullId(): void {
373+
$result = $this->executeDataProducer('entity_load', [
374+
'type' => $this->node->getEntityTypeId(),
375+
'id' => NULL,
376+
]);
377+
378+
$this->assertNull($result);
379+
}
380+
369381
/**
370382
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityLoad::resolve
371383
*/

0 commit comments

Comments
 (0)