Skip to content

Commit 4ccc601

Browse files
leebyronyaacovCR
authored andcommitted
Preserve defaultValue literals
Fixes graphql#3051 This change solves the problem of default values defined via SDL not always resolving correctly through introspection by preserving the original GraphQL literal in the schema definition. This changes argument and input field definitions `defaultValue` field from just the "value" to a new `GraphQLDefaultValueUsage` type which contains either or both "value" and "literal" fields. Since either of these fields may be set, new functions for resolving a value or literal from either have been added - `getLiteralDefaultValue` and `getCoercedDefaultValue` - these replace uses that either assumed a set value or were manually converting a value back to a literal. Here is the flow for how a default value defined in an SDL would be converted into a functional schema and back to an SDL: **Before this change:** ``` (SDL) --parse-> (AST) --coerceInputLiteral--> (defaultValue config) --valueToAST--> (AST) --print --> (SDL) ``` `coerceInputLiteral` performs coercion which is a one-way function, and `valueToAST` is unsafe and set to be deprecated in graphql#3049. **After this change:** ``` (SDL) --parse-> (defaultValue literal config) --print --> (SDL) ```
1 parent d512a52 commit 4ccc601

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/utilities/__tests__/coerceInputValue-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,26 @@ describe('coerceDefaultValue', () => {
892892
expect(parseValueCalls).to.deep.equal(['hello']);
893893
});
894894
});
895+
896+
describe('coerceDefaultValue', () => {
897+
it('memoizes coercion', () => {
898+
const parseValueCalls: any = [];
899+
900+
const spyScalar = new GraphQLScalarType({
901+
name: 'SpyScalar',
902+
parseValue(value) {
903+
parseValueCalls.push(value);
904+
return value;
905+
},
906+
});
907+
908+
const defaultValueUsage = {
909+
literal: { kind: Kind.STRING, value: 'hello' },
910+
} as const;
911+
expect(coerceDefaultValue(defaultValueUsage, spyScalar)).to.equal('hello');
912+
913+
// Call a second time
914+
expect(coerceDefaultValue(defaultValueUsage, spyScalar)).to.equal('hello');
915+
expect(parseValueCalls).to.deep.equal(['hello']);
916+
});
917+
});

src/utilities/coerceInputValue.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,21 @@ export function coerceDefaultValue(
408408
}
409409
return coercedValue;
410410
}
411+
412+
/**
413+
* @internal
414+
*/
415+
export function coerceDefaultValue(
416+
defaultValue: GraphQLDefaultValueUsage,
417+
type: GraphQLInputType,
418+
): unknown {
419+
// Memoize the result of coercing the default value in a hidden field.
420+
let coercedValue = (defaultValue as any)._memoizedCoercedValue;
421+
if (coercedValue === undefined) {
422+
coercedValue = defaultValue.literal
423+
? coerceInputLiteral(defaultValue.literal, type)
424+
: defaultValue.value;
425+
(defaultValue as any)._memoizedCoercedValue = coercedValue;
426+
}
427+
return coercedValue;
428+
}

0 commit comments

Comments
 (0)