From a07368bb5e0eac0f6996bd3e4b3d2524fb87b81d Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Feb 2022 19:00:05 +0100 Subject: [PATCH 1/5] fix(entity_reference): Return emtpy arrays instead of NULL --- .../DataProducer/Field/EntityReference.php | 8 +- .../Field/EntityReferenceLayoutRevisions.php | 14 ++-- .../Field/EntityReferenceRevisions.php | 12 +-- .../Field/EntityReferenceTrait.php | 4 +- .../DataProducer/EntityReferenceTest.php | 84 ++++++++++++++++++- 5 files changed, 103 insertions(+), 19 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/Field/EntityReference.php b/src/Plugin/GraphQL/DataProducer/Field/EntityReference.php index 0cd411b1d..55e5bbd06 100644 --- a/src/Plugin/GraphQL/DataProducer/Field/EntityReference.php +++ b/src/Plugin/GraphQL/DataProducer/Field/EntityReference.php @@ -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); @@ -166,7 +168,7 @@ public function resolve(EntityInterface $entity, $field, ?string $language, ?arr }); } - return NULL; + return []; } } diff --git a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceLayoutRevisions.php b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceLayoutRevisions.php index 2928bae60..c09266dbc 100644 --- a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceLayoutRevisions.php +++ b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceLayoutRevisions.php @@ -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 @@ -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); @@ -170,7 +170,7 @@ public function resolve(EntityInterface $entity, string $field, ?string $languag }); } - return NULL; + return []; } } diff --git a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php index 7dd09bfa5..479feaffb 100644 --- a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php +++ b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php @@ -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 @@ -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); diff --git a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php index 8155242cd..e2f835d1f 100644 --- a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php +++ b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php @@ -37,7 +37,7 @@ trait EntityReferenceTrait { * @return \Drupal\Core\Entity\EntityInterface[]|null * The list of references entities. Or NULL. */ - 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)) { @@ -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; diff --git a/tests/src/Kernel/DataProducer/EntityReferenceTest.php b/tests/src/Kernel/DataProducer/EntityReferenceTest.php index 128691e04..bfa7ec85f 100644 --- a/tests/src/Kernel/DataProducer/EntityReferenceTest.php +++ b/tests/src/Kernel/DataProducer/EntityReferenceTest.php @@ -12,7 +12,7 @@ use Drupal\user\UserInterface; /** - * Data producers Field test class. + * Tests the entity_reference data producers. * * @group graphql */ @@ -92,4 +92,86 @@ 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); + } + + /** + * Tests the upper case data producer. + */ + 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', + ]], + ]; + } + } From c33b127b39a351f1457219a8f999ce1d6f72cc12 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Feb 2022 19:09:41 +0100 Subject: [PATCH 2/5] better docs --- .../GraphQL/DataProducer/Field/EntityReferenceTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php index e2f835d1f..6e33d72cc 100644 --- a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php +++ b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php @@ -34,8 +34,8 @@ 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 { $entities = $resolver() ?: []; From 6755ccb152f5dface4226e00741a2f8fb6b0f426 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Feb 2022 19:13:47 +0100 Subject: [PATCH 3/5] Fix another instance of NULL --- .../GraphQL/DataProducer/Field/EntityReferenceRevisions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php index 479feaffb..b0f9a509a 100644 --- a/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php +++ b/src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php @@ -170,7 +170,7 @@ public function resolve(EntityInterface $entity, string $field, ?string $languag }); } - return NULL; + return []; } } From d99fdb8cbeb96624e6f8939e792f406809183917 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Feb 2022 19:19:30 +0100 Subject: [PATCH 4/5] coding standards --- .../DataProducer/EntityReferenceTest.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/src/Kernel/DataProducer/EntityReferenceTest.php b/tests/src/Kernel/DataProducer/EntityReferenceTest.php index bfa7ec85f..c77c46efd 100644 --- a/tests/src/Kernel/DataProducer/EntityReferenceTest.php +++ b/tests/src/Kernel/DataProducer/EntityReferenceTest.php @@ -120,57 +120,66 @@ public function emptyResultsProvider(): array { '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', - ]], + ], + ], ]; } From a7ccbc1ca4514ffc5543cc1a26cc62cea9a1d3fe Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Feb 2022 19:24:46 +0100 Subject: [PATCH 5/5] fix doc comment --- tests/src/Kernel/DataProducer/EntityReferenceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Kernel/DataProducer/EntityReferenceTest.php b/tests/src/Kernel/DataProducer/EntityReferenceTest.php index c77c46efd..3197dd8bd 100644 --- a/tests/src/Kernel/DataProducer/EntityReferenceTest.php +++ b/tests/src/Kernel/DataProducer/EntityReferenceTest.php @@ -111,7 +111,7 @@ public function testEmptyResults(string $data_producer, array $contexts): void { } /** - * Tests the upper case data producer. + * Data provider for testEmptyResults(). */ public function emptyResultsProvider(): array { return [