Skip to content
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
8 changes: 5 additions & 3 deletions src/Plugin/GraphQL/DataProducer/Field/EntityReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ public function __construct(
* @param string|null $accessOperation
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return \GraphQL\Deferred|null
* @return \GraphQL\Deferred|array
* A promise that will return referenced entities or empty array if there
* aren't any.
*/
public function resolve(EntityInterface $entity, $field, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
if (!$entity instanceof FieldableEntityInterface || !$entity->hasField($field)) {
return NULL;
return [];
}

$definition = $entity->getFieldDefinition($field);
Expand All @@ -166,7 +168,7 @@ public function resolve(EntityInterface $entity, $field, ?string $language, ?arr
});
}

return NULL;
return [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* id = "entity_reference_layout_revisions",
* name = @Translation("Entity reference layout revisions"),
* description = @Translation("Loads entities from an entity reference layout revisions field."),
* provider = "entity_reference_layout",
* produces = @ContextDefinition("entity",
* label = @Translation("Entity"),
* multiple = TRUE
Expand Down Expand Up @@ -143,17 +142,18 @@ public function __construct(
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
* The caching context related to the current field.
*
* @return \GraphQL\Deferred|null
* A promise that will return entities or NULL if there aren't any.
* @return \GraphQL\Deferred|array
* A promise that will return referenced entities or empty array if there
* aren't any.
*/
public function resolve(EntityInterface $entity, string $field, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context): ?Deferred {
public function resolve(EntityInterface $entity, string $field, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context) {
if (!$entity instanceof FieldableEntityInterface || !$entity->hasField($field)) {
return NULL;
return [];
}

$definition = $entity->getFieldDefinition($field);
if ($definition->getType() !== 'entity_reference_layout_revisioned') {
return NULL;
return [];
}

$definition = $entity->getFieldDefinition($field);
Expand All @@ -170,7 +170,7 @@ public function resolve(EntityInterface $entity, string $field, ?string $languag
});
}

return NULL;
return [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* id = "entity_reference_revisions",
* name = @Translation("Entity reference revisions"),
* description = @Translation("Loads entities from an entity reference revisions field."),
* provider = "entity_reference_revisions",
* produces = @ContextDefinition("entity",
* label = @Translation("Entity"),
* multiple = TRUE
Expand Down Expand Up @@ -143,17 +142,18 @@ public function __construct(
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
* The caching context related to the current field.
*
* @return \GraphQL\Deferred|null
* A promise that will return entities or NULL if there aren't any.
* @return \GraphQL\Deferred|array
* A promise that will return referenced entities or empty array if there
* aren't any.
*/
public function resolve(EntityInterface $entity, string $field, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context): ?Deferred {
public function resolve(EntityInterface $entity, string $field, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context) {
if (!$entity instanceof FieldableEntityInterface || !$entity->hasField($field)) {
return NULL;
return [];
}

$definition = $entity->getFieldDefinition($field);
if ($definition->getType() !== 'entity_reference_revisions') {
return NULL;
return [];
}

$definition = $entity->getFieldDefinition($field);
Expand All @@ -170,7 +170,7 @@ public function resolve(EntityInterface $entity, string $field, ?string $languag
});
}

return NULL;
return [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ trait EntityReferenceTrait {
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
* The caching context related to the current field.
*
* @return \Drupal\Core\Entity\EntityInterface[]|null
* The list of references entities. Or NULL.
* @return \Drupal\Core\Entity\EntityInterface[]
* The list of references entities.
*/
protected function getReferencedEntities(string $type, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, \Closure $resolver, FieldContext $context): ?array {
protected function getReferencedEntities(string $type, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, \Closure $resolver, FieldContext $context): array {
$entities = $resolver() ?: [];

if (isset($bundles)) {
Expand All @@ -57,7 +57,7 @@ protected function getReferencedEntities(string $type, ?string $language, ?array
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$tags = $type->getListCacheTags();
$context->addCacheTags($tags);
return NULL;
return [];
}

return $entities;
Expand Down
93 changes: 92 additions & 1 deletion tests/src/Kernel/DataProducer/EntityReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Drupal\user\UserInterface;

/**
* Data producers Field test class.
* Tests the entity_reference data producers.
*
* @group graphql
*/
Expand Down Expand Up @@ -92,4 +92,95 @@ public function testResolveEntityReference(): void {
$this->assertEquals('Dolor2 French', reset($result)->label());
}

/**
* Tests that a given data producer returns an empty array.
*
* @dataProvider emptyResultsProvider
*/
public function testEmptyResults(string $data_producer, array $contexts): void {
$node = Node::create([
'title' => 'Dolor',
'type' => 'test1',
]);
$node->save();
$contexts['entity'] = $node;

$result = $this->executeDataProducer($data_producer, $contexts);
$this->assertIsArray($result);
$this->assertEmpty($result);
}

/**
* Data provider for testEmptyResults().
*/
public function emptyResultsProvider(): array {
return [
// Test that an empty reference field returns an empty array.
['entity_reference', [
'field' => 'field_test1_to_test2',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Test that an invalid field name returns an empty array.
['entity_reference', [
'field' => 'does_not_exist',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Test that an invalid field type returns an empty array.
['entity_reference', [
'field' => 'title',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Same test set for the entity_reference_revisions data producer.
// Test that an empty reference field returns an empty array.
['entity_reference_revisions', [
'field' => 'field_test1_to_test2',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Test that an invalid field name returns an empty array.
['entity_reference_revisions', [
'field' => 'does_not_exist',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Test that an invalid field type returns an empty array.
['entity_reference_revisions', [
'field' => 'title',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Same test set for the entity_reference_layout_revisions data producer.
// Test that an empty reference field returns an empty array.
['entity_reference_layout_revisions', [
'field' => 'field_test1_to_test2',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Test that an invalid field name returns an empty array.
['entity_reference_layout_revisions', [
'field' => 'does_not_exist',
'access' => TRUE,
'access_operation' => 'view',
],
],
// Test that an invalid field type returns an empty array.
['entity_reference_layout_revisions', [
'field' => 'title',
'access' => TRUE,
'access_operation' => 'view',
],
],
];
}

}