Skip to content

Conversation

Leksat
Copy link
Contributor

@Leksat Leksat commented Aug 11, 2021

Fixes entity_reference, entity_reference_layout_revisions and entity_reference_revisions data producers. Now they return the translated entities in case if language is provided.

Based on @drupov's PR #974

drupov and others added 24 commits February 11, 2020 14:20
# Conflicts:
#	src/Plugin/GraphQL/DataProducer/Field/EntityReference.php
#	src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php
@Leksat Leksat force-pushed the fix-entity_reference-language branch from b6fc386 to d68443a Compare August 11, 2021 08:03
@Leksat Leksat force-pushed the fix-entity_reference-language branch from d68443a to 6320702 Compare August 11, 2021 08:11
@Leksat Leksat marked this pull request as ready for review August 11, 2021 08:41
Copy link
Contributor

@pmelab pmelab left a comment

Choose a reason for hiding this comment

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

From a code perspective it seems fine. As I understand we will fall back to the source language if a language does not exist? Then it's up to the consumer to decide what to do. An option for "strict" language (only return items that exist in that language) could he helpful, but i would not put it in scope of this PR.

But we also need some tests for that. We only have one entity reference test right now:
https://github.com/drupal-graphql/graphql/blob/8.x-4.x/tests/src/Kernel/DataProducer/FieldTest.php

And it's weirdly named? Maybe we could use this as an opportunity to name it properly and add cases for this fix?

Copy link
Contributor

@klausi klausi left a comment

Choose a reason for hiding this comment

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

Agreed with @pmelab , tests would be good to cover the language. And we should use type hints in function signatures for all new code.

@Leksat
Copy link
Contributor Author

Leksat commented Aug 11, 2021

@pmelab @klausi I guess this is out of scope, but there seems to be an issue with the data producer argument default values.

For example, the entity_reference plugin definition sets a default value for access argument:

* "access" = @ContextDefinition("boolean",
* label = @Translation("Check access"),
* required = FALSE,
* default_value = TRUE
* ),

So I would guess that the resolve method never gets null for $access argument. Right?

However, it is typehinted as ?bool:

public function resolve(EntityInterface $entity, $field, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {

Then, when we call DataProducerExecutionTrait::executeDataProducer() without providing access argument, the data producer receives access as null. So it does not check the access, even if this should be the default behavior.

I would suggest:

  • Remove ? from data producer arguments having default values.
  • Improve DataProducerExecutionTrait::executeDataProducer() to fill in default argument values.

@Leksat Leksat requested review from pmelab and klausi August 11, 2021 11:32
@pmelab
Copy link
Contributor

pmelab commented Aug 11, 2021

Does the incorrect default value behavior apply to actual execution within a schema request? Or is it scoped to DataProducerExecutionTrait::executeDataProducer(). We shouldn't introduce breaking changes by adding typehints.

@Leksat
Copy link
Contributor Author

Leksat commented Aug 11, 2021

Does the incorrect default value behavior apply to actual execution within a schema request?

No.

With a real request, the default values are set from the plugin definition (here).

Or is it scoped to DataProducerExecutionTrait::executeDataProducer()

Yes. But this method is used a lot in tests.

@Leksat
Copy link
Contributor Author

Leksat commented Aug 11, 2021

We shouldn't introduce breaking changes by adding typehints.

Well, yeah... If a data producer is extended from outside of the module, things might break.

@pmelab
Copy link
Contributor

pmelab commented Aug 11, 2021

@klausi Do we consider a typehint change that will invalidate subclasses a breaking change?

I can't tell if a lot of people extend existing resolvers.

@klausi
Copy link
Contributor

klausi commented Aug 16, 2021

It depends if we consider data producer plugins as part of the API surface of graphql module or not. I'm not sure, when in doubt we should probably consider it as part of the stable API and we cannot change it.

I think we are lucky here and do not have a security issue since in real requests the access default value gets filled in correctly as you found out. Anyway, I think it makes sense to improve DataProducerExecutionTrait in the test to fill in default values if possible

@klausi klausi merged commit 92d3ec4 into drupal-graphql:8.x-4.x Aug 16, 2021
@klausi
Copy link
Contributor

klausi commented Aug 16, 2021

Thanks a lot @Leksat ! I merged this now and we can follow-up with DataProducerExecutionTrait default value test improvements.

@Leksat Leksat deleted the fix-entity_reference-language branch August 16, 2021 14:37
chrfritsch added a commit to chrfritsch/graphql that referenced this pull request Nov 24, 2021
* 8.x-4.x:
  fix(dataproducer): Fix language definition to be single value in entity reference producers (drupal-graphql#1241)
  docs(server): Improved class property descriptions
  feat(server): Add configurable validation security rules for introspection and query complexity (drupal-graphql#1244)
  tests(buffer): Fix ArryObject usage to not depend on Zend (drupal-graphql#1247)
  test(phpstan): Add missing return type hint
  fix(file-upldoad): Validate files in the correct order
  fix(DataProducer): Fix entity reference loading by language (drupal-graphql#1232)
  tests(assertions): Implement leaked metadata detection for QueryResultAssertionTrait (drupal-graphql#1207)
  tests(github): Switch testing to Drupal core 9.2.x (drupal-graphql#1215)
  chore(voyager): yarn audit security updates (drupal-graphql#1214)
  test(phpstan): Enable PHPStan run on PHP 8.0 (drupal-graphql#1211)
  refactor(executor): Replace deprecated AST::getOperation() with AST::getOperationAST() (drupal-graphql#1210)
  fix(executor): Remove swapped errors compatibility mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.