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

ease upgrade path for programmatic default values #4296

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

yaacovCR
Copy link
Contributor

#3814 added default value validation and coercion, deprecating astFromValue() (which was unsafe, used serialize() to uncoerce input values provided in the internal format) and replacing it with a call to valueToLiteral() which safely operates on external values.

This PR makes that change backwards compatible by reintroducing it as a new config option of externalDefaultValue instead of replacing the existing option of defaultValue.

defaultValueLiteral stays as its open top-level config option, but is subsumed under externalDefaultValue within the GraphQLArgument/GraphQLInputField as it is always external.

@yaacovCR yaacovCR requested a review from a team as a code owner November 18, 2024 04:04
Copy link

netlify bot commented Nov 18, 2024

Deploy Preview for compassionate-pike-271cb3 ready!

Name Link
🔨 Latest commit b2889b9
🔍 Latest deploy log https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/673abdffbb06120008c012f4
😎 Deploy Preview https://deploy-preview-4296--compassionate-pike-271cb3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

Hi @yaacovCR, I'm @github-actions bot happy to help you with this PR 👋

Supported commands

Please post this commands in separate comments and only one per comment:

  • @github-actions run-benchmark - Run benchmark comparing base and merge commits for this PR
  • @github-actions publish-pr-on-npm - Build package from this PR and publish it on NPM

@yaacovCR
Copy link
Contributor Author

yaacovCR commented Nov 18, 2024

To summarize, cross-posting from #4288 (comment):

In v16, the available default value option on GraphQLArgumentConfig and GraphQLInputField was just defaultValue and that represented the internal value and so external values were never validated no matter how supplied. If the schema was built from SDL, we converted the default value AST into the internal value immediately by way of valueFromAST(), which just erased the default if it was invalid, if it was programmatically supplied, we just used whatever was suppllied.

In v17 currently, prior to the proposed #4296, we adopted @leebyron 's work and replaced that with two options, defaultValue and defaultValueLiteral where the former was the external value programmatically supplied, and the lateral was the ValueNode from the schema built from buildSchema() or extendSchema(), validating both of these within validateSchema(), and so we retain on GraphQLArgument and GraphQLInputField types a single defaultValue property of type DefaultValueUsage which either has a value or a literal property.

After #4296, we have both. On the config, to disambiguate, we use externalDefaultValue for the programmatically supplied option, so defaultValue as previously supplied stays the same. Within the objects, the new DefaultValueUsage object is stored under externalDefaultValue but the old internal default value if supplied will be under defaultValue. The new config options take precedence over the old.

Converting from the old to new and vice a versa

Coercing the external values for the defaults to the input values can be done easily using the coerceInputValue() or the coerceInputLiteral functions, (although it's not necessarily safe until the schema has been validated, in particular default values may contain themselves recursively which would be quite unsafe).

If you want to safely turn an internal value into something external, we have no function to do this in v16 or v17. But if you want, you can do what's done unsafely in v16 for introspection, you can use astFromValue() to move from an internal value to an AST, which is always external, and can now be supplied as the default via the defaultValueLiteral option.

@yaacovCR yaacovCR requested review from JoviDeCroock and removed request for a team November 18, 2024 12:07
@yaacovCR yaacovCR added PR: feature 🚀 requires increase of "minor" version number PR: breaking change 💥 implementation requires increase of "major" version number and removed PR: feature 🚀 requires increase of "minor" version number labels Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: breaking change 💥 implementation requires increase of "major" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant