Skip to content

Commit ede32de

Browse files
yaacovCRleebyron
andcommitted
RFC: Default value validation & coercion (graphql#3814)
[graphql#3049 rebased on main](graphql#3049). This is the last rebased PR from the original PR stack concluding with graphql#3049. * Rebased: graphql#3809 [Original: graphql#3092] * Rebased: graphql#3810 [Original: graphql#3074] * Rebased: graphql#3811 [Original: graphql#3077] * Rebased: graphql#3812 [Original: graphql#3065] * Rebased: graphql#3813 [Original: graphql#3086] * Rebased: graphql#3814 (this PR) [Original: graphql#3049] Update: graphql#3044 and graphql#3145 have been separated from this stack. Changes from original PR: 1. `astFromValue()` is deprecated instead of being removed. @leebyron comments from graphql#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 <lee@leebyron.com>
1 parent be88b57 commit ede32de

File tree

13 files changed

+900
-37
lines changed

13 files changed

+900
-37
lines changed

cspell.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ words:
3535
- Coodinate
3636
- metafield
3737
- graphiql
38+
- uncoerce
39+
- uncoerced
3840

3941
# Different names used inside tests
4042
- Skywalker

src/execution/__tests__/variables-test.ts

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ export {
443443
// Create a JavaScript value from a GraphQL language AST without a Type.
444444
valueFromASTUntyped,
445445
// Create a GraphQL language AST from a JavaScript value.
446+
/** @deprecated use `valueToLiteral()` instead with care to operate on external values - `astFromValue()` will be removed in v18 */
446447
astFromValue,
447448
// A helper to use within recursive-descent visitors which need to be aware of the GraphQL type system.
448449
TypeInfo,

src/type/__tests__/enumType-test.ts

Lines changed: 1 addition & 3 deletions
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)