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(dataproducer): Populate context default values before resolving #1403

Merged
merged 14 commits into from
Jun 28, 2024

Conversation

klausi
Copy link
Contributor

@klausi klausi commented May 30, 2024

Fix for #1233.

It is a bit unfortunate that Drupal core does not provide a good API function to get context values with default values populated, but not a big problem. We can implement that ourselves.

Now we could also update a lot of resolve() function signatures to remove the ? null type parameter, because now default values will always be passed.

Should we do it as part of this change or do we consider this an API break?

Copy link
Contributor

@Kingdutch Kingdutch left a comment

Choose a reason for hiding this comment

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

Change looks good to me. I do think this is an API break that we may need to move to 5.x. I'm pretty sure I have default values in annotations somewhere which don't match what's in the code (which is silly, but that's not something that previously caused breakages).

@klausi
Copy link
Contributor Author

klausi commented Jun 14, 2024

That is a good point - if you have defined your default values wrong and they don't match the types on your resolver then there can be fatal errors.

We use quite a few dataproducers in Jobiqo and I did not see any problem in our automated testing, so probably this is a rare problem.

So I would risk it and push this to 4.x, with a warning in the release notes.

What do you think about changing the function signature of various dataproducer's resolve() methods? With this change we can remove a couple of ? operators of some types. I did not do that yet in this PR. I would consider the resolve() methods not part of the API surface of the graphql module, so we could risk that as well.

@Kingdutch
Copy link
Contributor

I think in general I'm a bit more conservative with these kinds of changes. With all the modules we update in Open Social these kinds of subtle changes can add significant overhead which we tend to pay attention to with major updates, but with minor updates we generally start from the "I expect this still works the same and update mostly blindly" principle.

What do you think would be the impact of making this an opt-in flag in a minor version with a deprecation notice? Then we remove the flag in the next major version and default to the new behaviour?

if (Settings::get('graphql_context_defaults')) {
  $context = $this->getContextValuesWithDefaults();
}
else {
  @trigger_error("Not using defaults from context is deprecated in GraphQL 4.x and this behaviour will change in 5.0. Opt-in to the new behaviour by adding `$settings['graphql_context_defaults'] = TRUE;` to your settings.php file");
  $context = $this->getContextValues();
}

(above code is illustrative and not tested)

@klausi
Copy link
Contributor Author

klausi commented Jun 15, 2024

I'm not a fan of the setting as it makes it more complicated and harder to maintain, but it could work. We could have an update function for old installations that sets the config to false, then at least all new installations will get the new default.

I will check this approach.

@klausi klausi requested a review from Kingdutch June 16, 2024 09:49
@klausi
Copy link
Contributor Author

klausi commented Jun 16, 2024

Alright, this seems to work now. Can you review again and let me know if this is what you had in mind? Thanks!

Copy link
Contributor

@Kingdutch Kingdutch left a comment

Choose a reason for hiding this comment

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

Yeah that’s awesome! Makes the minor release fully transparent (nothing happens) while allowing people to adopt the new behavior in preparation for the next major version. In the 5.0 release we can remove a bunch of code and the new behavior is the default.

Thanks! 🙌

@klausi klausi merged commit 79d09dd into drupal-graphql:8.x-4.x Jun 28, 2024
4 checks passed
@klausi klausi deleted the dataproducer-default-value branch June 28, 2024 10:03
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.

2 participants