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

fix(EntityLoad): Return NULL on NULL entity IDs when composing entity load #1174

Merged
merged 6 commits into from
Feb 13, 2022

Conversation

klausi
Copy link
Contributor

@klausi klausi commented Feb 22, 2021

We saw the error array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple(), this happens when a NULL ID is passed into the EntityLoad dataproducer (for example when composing data producers)

Make this more robust by checking for NULL.

Copy link
Contributor

@rthideaway rthideaway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me this makes perfect sense

@otrolopezmas
Copy link

Hey!

Great that you found this.

I think for the test it would suffice with just adding a new test checking out for NULLs. See

/**
   * @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityLoad::resolve
   */
  public function testResolveEntityLoadWithNullId(): void {
    $result = $this->executeDataProducer('entity_load', [
      'type' => $this->node->getEntityTypeId(),
      'id' => NULL,
    ]);

    $this->assertNull($result);
  }

At the same time, I am not completely sure but I think I got the same problem with LoadMultiple? In that case we would need to do some kind of checking if there're NULL presents in the resolver in src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php. And of course we would need to do a similar test like

  /**
   * @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityLoadMultiple::resolve
   */
  public function testResolveEntityLoadMultipleWithNullIds(): void {
    $result = $this->executeDataProducer('entity_load_multiple', [
      'type' => $this->node1->getEntityTypeId(),
      'bundles' => [$this->node1->bundle(), $this->node2->bundle()],
      'ids' => [$this->node1->id(), $this->node2->id(), NULL],
      // @todo We need to set these default values here to make the access
      // handling work. Ideally that should not be needed.
      'access' => TRUE,
      'access_operation' => 'view',
    ]);

   $this->assertNull($result);
  }

What do you think? Would we need more test covering some kind of composing.

@klausi
Copy link
Contributor Author

klausi commented Dec 20, 2021

Thanks, yes I think that test case would be enough for EntityLoad. Can you make a new pull request so that we can see the tests run?

EntityLoadMultiple: right, would make sense to run the default array_filter() on the passed IDs to remove any NULL or falsy values. Your test for that makes sense!

@klausi klausi merged commit 09c5478 into drupal-graphql:8.x-4.x Feb 13, 2022
@klausi klausi deleted the entity-load-null branch February 13, 2022 20:20
@klausi
Copy link
Contributor Author

klausi commented Feb 13, 2022

Added a test case and merged this, thanks!

chrfritsch added a commit to chrfritsch/graphql that referenced this pull request Feb 28, 2022
* 8.x-4.x:
  test(dataprovider): Rename dataprovider functions to not be accidentally tested (drupal-graphql#1266)
  fix(EntityLoad): Return NULL on NULL entity IDs when composing entity load (drupal-graphql#1174)
  fix(entity_reference): Return emtpy arrays instead of NULL (drupal-graphql#1265)
  chore(voyager): JS dependency updates with yarn audit
  chore(explorer): JS dependency updates with yarn audit
  test(coder): Update Coder to 8.3.14 (drupal-graphql#1264)
  test(phpstan): Enable PHPStan on PHP 8 by disabling PHP opcache (drupal-graphql#1262)
  feat(server): Log unsafe server errors for better error tracing (drupal-graphql#1258)
  test(phpstan): Ignore PHPStan warning directly in code with a comment (drupal-graphql#1263)
  test(phpstan): Ignore a false unreachable statement error (drupal-graphql#1261)
  test(phpstan): Update PHPStan and dependencies (drupal-graphql#1257)
  test(php): Add PHP 8.1 for testing (drupal-graphql#1256)
  test(core): Update Drupal core to 9.3 for testing (drupal-graphql#1255)
klausi added a commit to klausi/graphql that referenced this pull request Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants