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

Add valueToLiteral() #3812

Merged
merged 1 commit into from
Sep 29, 2024
Merged

Conversation

yaacovCR
Copy link
Contributor

@yaacovCR yaacovCR commented Jan 1, 2023

#3065 rebased on main.

Depends on #3811

@leebyron comments from original PR:

Provides the "Value to Literal" methods in this data flow chart.

  • Adds valueToLiteral() which takes an external input value and translates it to a literal, allowing for custom scalars to define this behavior.

This also adds important changes to Input Coercion, especially for custom scalars:

  • The value provided to parseLiteral is now ConstValueNode and the second variables argument has been removed. For all built-in scalars this has no effect, but any custom scalars which use complex literals no longer need to do variable reconciliation manually (in fact most do not -- this has been an easy subtle bug to miss).
    This behavior is possible with the addition of replaceVariables

Changes to the original:

  1. Instead of changing the signature of parseLiteral(), a new method parseConstLiteral() has been added with the simpler signature. parseLiteral() has been marked for deprecation.
  2. replaceVariables() has access to operation and fragment variables.

@yaacovCR yaacovCR added the PR: feature 🚀 requires increase of "minor" version number label Jan 1, 2023
@yaacovCR yaacovCR requested a review from leebyron January 1, 2023 14:00
@github-actions
Copy link

github-actions bot commented Jan 1, 2023

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

@netlify
Copy link

netlify bot commented Jan 1, 2023

Deploy Preview for compassionate-pike-271cb3 ready!

Name Link
🔨 Latest commit bc6af39
🔍 Latest deploy log https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/66f92956f623850008cdb013
😎 Deploy Preview https://deploy-preview-3812--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.

@yaacovCR yaacovCR force-pushed the value-to-literal-rebased branch 4 times, most recently from e740389 to f03f2c5 Compare January 1, 2023 20:06
@yaacovCR yaacovCR force-pushed the value-to-literal-rebased branch 7 times, most recently from 91cf2c1 to b3fbe95 Compare February 6, 2023 12:53
@yaacovCR yaacovCR force-pushed the value-to-literal-rebased branch 2 times, most recently from 7afbe9b to 3ea9f70 Compare May 31, 2023 11:51
@yaacovCR yaacovCR requested a review from a team as a code owner September 15, 2024 11:58
@yaacovCR yaacovCR force-pushed the value-to-literal-rebased branch 2 times, most recently from 15aa1e5 to 9a30b6f Compare September 15, 2024 14:18
@yaacovCR yaacovCR force-pushed the value-to-literal-rebased branch 3 times, most recently from 1dba99b to a042db7 Compare September 17, 2024 11:42
@yaacovCR yaacovCR force-pushed the value-to-literal-rebased branch 3 times, most recently from e593020 to 8e57231 Compare September 17, 2024 13:59
@yaacovCR yaacovCR mentioned this pull request Sep 27, 2024
17 tasks
* Adds `valueToLiteral()` which takes an external value and translates it to a literal, allowing for custom scalars to define this behavior.

This also adds important changes to Input Coercion, especially for custom scalars:

* Addition of `parseConstLiteral()` to leaf types which operates in parallel to `parseLiteral()` but take `ConstValueNode` instead of `ValueNode` -- the second `variables` argument has been removed. For all built-in scalars this has no effect, but any custom scalars which use complex literals no longer need to do variable reconciliation manually (in fact most do not -- this has been an easy subtle bug to miss).

  This behavior is possible with the addition of `replaceVariables()`.
  `parseLiteral()` is no longer used internally and has been marked for deprecation.
@yaacovCR yaacovCR merged commit 4dfae39 into graphql:main Sep 29, 2024
20 checks passed
@yaacovCR yaacovCR deleted the value-to-literal-rebased branch September 29, 2024 12:10
yaacovCR added a commit that referenced this pull request Oct 13, 2024
the new method was introduced in #3812 with upgrade path improved in
#4209.

This PR completes polishes this work a bit:

1. The method is renamed to `coerceInputLiteral()` following the naming
convention suggested in #2357, with the other methods to be renamed in
later PRs.
2. The type of the method `GraphQLScalarInputLiteralCoercer` is
exported, which was not done in the initial work. The old
`GraphQLScalarLiteralParser` type is deprecated.
yaacovCR added a commit that referenced this pull request Oct 18, 2024
[#3086 rebased on
main](#3086).

Depends on #3812 

@leebyron comments from original PR:

> Factors out input validation to reusable functions:
> 
> * Introduces `validateInputLiteral` by extracting this behavior from
`ValuesOfCorrectTypeRule`.
> * Introduces `validateInputValue` by extracting this behavior from
`coerceInputValue`
> * Simplifies `coerceInputValue` to return early on validation error
> * Unifies error reporting between `validateInputValue` and
`validateInputLiteral`, causing some error message strings to change,
but error data (eg locations) are preserved.
> 
> These two parallel functions will be used to validate default values
> 
> Potentially breaking if you rely on the existing behavior of
`coerceInputValue` to call a callback function, as the call signature
has changed. GraphQL behavior should not change, though error messages
are now slightly different.

Note: also breaking if you rely on the default callback function to
throw. Grossly similar behavior is available with
`validateInputValue()`.

Co-authored-by: Lee Byron <[email protected]>
yaacovCR added a commit that referenced this pull request Oct 27, 2024
[#3049 rebased on
main](#3049).

This is the last rebased PR from the original PR stack concluding with
#3049.

* Rebased: #3809 [Original: #3092]
* Rebased: #3810 [Original: #3074]
* Rebased: #3811 [Original: #3077]
* Rebased: #3812 [Original: #3065]
* Rebased: #3813 [Original: #3086]
* Rebased: #3814 (this PR) [Original: #3049]

Update: #3044 and #3145 have been separated from this stack.

Changes from original PR:
1. `astFromValue()` is deprecated instead of being removed.

@leebyron comments from #3049, the original PR:
> Implements
[graphql/graphql-spec#793](graphql/graphql-spec#793)
> 
> * BREAKING: Changes default values from being represented as an
assumed-coerced "internal input value" to a pre-coerced "external input
value" (See chart below).
> This allows programmatically provided default values to be represented
in the same domain as values sent to the service via variable values,
and allows it to have well defined methods for both transforming into a
printed GraphQL literal string for introspection / schema printing (via
`valueToLiteral()`) or coercing into an "internal input value" for use
at runtime (via `coerceInputValue()`)
> To support this change in value type, this PR adds two related
behavioral changes:
>   
> * Adds coercion of default values from external to internal at runtime
(within `coerceInputValue()`)
> * Removes `astFromValue()`, replacing it with `valueToLiteral()` for
use in introspection and schema printing. `astFromValue()` performed
unsafe "uncoercion" to convert an "Internal input value" directly to a
"GraphQL Literal AST", where `valueToLiteral()` performs a well defined
transform from "External input value" to "GraphQL Literal AST".
> * Adds validation of default values during schema validation.
> Since assumed-coerced "internal" values may not pass "external"
validation (for example, Enum values), an additional validation error
has been included which attempts to detect this case and report a
strategy for resolution.
> 
> Here's a broad overview of the intended end state:
> 
> ![GraphQL Value
Flow](https://user-images.githubusercontent.com/50130/118379946-51ac5300-b593-11eb-839f-c483ecfbc875.png)

---------

Co-authored-by: Lee Byron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: feature 🚀 requires increase of "minor" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants