Skip to content

Commit fbb191a

Browse files
yaacovCRleebyron
andauthored
RFC: Default value validation & coercion (#3814)
[#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]>
1 parent 9dae904 commit fbb191a

13 files changed

+900
-37
lines changed

cspell.yml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ ignoreRegExpList:
3434

3535
words:
3636
- graphiql
37+
- uncoerce
38+
- uncoerced
3739

3840
# Different names used inside tests
3941
- Skywalker

src/execution/__tests__/variables-test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ const TestType = new GraphQLObjectType({
150150
}),
151151
fieldWithNestedInputObject: fieldWithInputArg({
152152
type: TestNestedInputObject,
153-
defaultValue: 'Hello World',
154153
}),
155154
list: fieldWithInputArg({ type: new GraphQLList(GraphQLString) }),
156155
nested: {

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export {
448448
// Create a JavaScript value from a GraphQL language AST without a Type.
449449
valueFromASTUntyped,
450450
// Create a GraphQL language AST from a JavaScript value.
451+
/** @deprecated use `valueToLiteral()` instead with care to operate on external values - `astFromValue()` will be removed in v18 */
451452
astFromValue,
452453
// A helper to use within recursive-descent visitors which need to be aware of the GraphQL type system.
453454
TypeInfo,

src/type/__tests__/enumType-test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ const QueryType = new GraphQLObjectType({
7171
args: {
7272
fromEnum: {
7373
type: ComplexEnum,
74-
// Note: defaultValue is provided an *internal* representation for
75-
// Enums, rather than the string name.
76-
defaultValue: Complex1,
74+
defaultValue: 'ONE',
7775
},
7876
provideGoodValue: { type: GraphQLBoolean },
7977
provideBadValue: { type: GraphQLBoolean },

0 commit comments

Comments
 (0)