Skip to content

Commit a4d9d80

Browse files
committed
ease upgrade path for programmatic default values
1 parent 79ad146 commit a4d9d80

28 files changed

+314
-216
lines changed

integrationTests/ts/basic-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const queryType: GraphQLObjectType = new GraphQLObjectType({
1111
args: {
1212
who: {
1313
type: GraphQLString,
14-
defaultValue: 'World',
14+
externalDefaultValue: 'World',
1515
},
1616
},
1717
resolve(_root, args: { who: string }) {

integrationTests/ts/esm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const queryType: GraphQLObjectType = new GraphQLObjectType({
1515
args: {
1616
who: {
1717
type: GraphQLString,
18-
defaultValue: 'World',
18+
externalDefaultValue: 'World',
1919
},
2020
},
2121
resolve(_root, args: { who: string }) {

src/execution/__tests__/executor-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ describe('Execute: Handles basic execution tasks', () => {
246246
signature: {
247247
name: 'var',
248248
type: GraphQLString,
249-
defaultValue: undefined,
249+
externalDefaultValue: undefined,
250250
},
251251
value: 'abc',
252252
},

src/execution/__tests__/variables-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ const TestType = new GraphQLObjectType({
142142
}),
143143
fieldWithDefaultArgumentValue: fieldWithInputArg({
144144
type: GraphQLString,
145-
defaultValue: 'Hello World',
145+
externalDefaultValue: 'Hello World',
146146
}),
147147
fieldWithNonNullableStringInputAndDefaultArgumentValue: fieldWithInputArg({
148148
type: new GraphQLNonNull(GraphQLString),
149-
defaultValue: 'Hello World',
149+
externalDefaultValue: 'Hello World',
150150
}),
151151
fieldWithNestedInputObject: fieldWithInputArg({
152152
type: TestNestedInputObject,
@@ -187,7 +187,7 @@ const schema = new GraphQLSchema({
187187
type: new GraphQLNonNull(GraphQLBoolean),
188188
description: 'Skipped when true.',
189189
// default values will override operation variables in the setting of defined fragment variables that are not provided
190-
defaultValue: true,
190+
externalDefaultValue: true,
191191
},
192192
},
193193
}),

src/execution/getVariableSignature.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { GraphQLError } from '../error/GraphQLError.js';
22

3-
import type { VariableDefinitionNode } from '../language/ast.js';
3+
import type {
4+
ConstValueNode,
5+
VariableDefinitionNode,
6+
} from '../language/ast.js';
47
import { print } from '../language/printer.js';
58

69
import { isInputType } from '../type/definition.js';
7-
import type {
8-
GraphQLDefaultValueUsage,
9-
GraphQLInputType,
10-
GraphQLSchema,
11-
} from '../type/index.js';
10+
import type { GraphQLInputType, GraphQLSchema } from '../type/index.js';
1211

1312
import { typeFromAST } from '../utilities/typeFromAST.js';
1413

@@ -21,7 +20,8 @@ import { typeFromAST } from '../utilities/typeFromAST.js';
2120
export interface GraphQLVariableSignature {
2221
name: string;
2322
type: GraphQLInputType;
24-
defaultValue: GraphQLDefaultValueUsage | undefined;
23+
defaultValue?: never;
24+
externalDefaultValue: { literal: ConstValueNode } | undefined;
2525
}
2626

2727
export function getVariableSignature(
@@ -46,6 +46,6 @@ export function getVariableSignature(
4646
return {
4747
name: varName,
4848
type: varType,
49-
defaultValue: defaultValue ? { literal: defaultValue } : undefined,
49+
externalDefaultValue: defaultValue ? { literal: defaultValue } : undefined,
5050
};
5151
}

src/execution/values.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,9 @@ export function experimentalGetArgumentValues(
231231
{ nodes: node },
232232
);
233233
}
234-
if (argDef.defaultValue) {
235-
coercedValues[name] = coerceDefaultValue(
236-
argDef.defaultValue,
237-
argDef.type,
238-
);
234+
const coercedDefaultValue = coerceDefaultValue(argDef);
235+
if (coercedDefaultValue !== undefined) {
236+
coercedValues[name] = coercedDefaultValue;
239237
}
240238
continue;
241239
}
@@ -254,11 +252,9 @@ export function experimentalGetArgumentValues(
254252
!Object.hasOwn(scopedVariableValues.coerced, variableName)) &&
255253
!isRequiredArgument(argDef)
256254
) {
257-
if (argDef.defaultValue) {
258-
coercedValues[name] = coerceDefaultValue(
259-
argDef.defaultValue,
260-
argDef.type,
261-
);
255+
const coercedDefaultValue = coerceDefaultValue(argDef);
256+
if (coercedDefaultValue !== undefined) {
257+
coercedValues[name] = coercedDefaultValue;
262258
}
263259
continue;
264260
}

src/type/__tests__/definition-test.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ describe('Type System: Objects', () => {
204204
input: {
205205
description: 'Argument description.',
206206
type: ScalarType,
207-
defaultValue: 'DefaultValue',
207+
defaultValue: undefined,
208+
externalDefaultValue: 'DefaultValue',
208209
defaultValueLiteral: undefined,
209210
deprecationReason: 'Argument deprecation reason.',
210211
extensions: { someExtension: 'extension' },
@@ -377,6 +378,7 @@ describe('Type System: Objects', () => {
377378
description: undefined,
378379
type: ScalarType,
379380
defaultValue: undefined,
381+
externalDefaultValue: undefined,
380382
deprecationReason: undefined,
381383
extensions: {},
382384
astNode: undefined,
@@ -493,6 +495,7 @@ describe('Type System: Interfaces', () => {
493495
description: 'Argument description.',
494496
type: ScalarType,
495497
defaultValue: undefined,
498+
externalDefaultValue: undefined,
496499
defaultValueLiteral: dummyAny,
497500
deprecationReason: 'Argument deprecation reason.',
498501
extensions: { someExtension: 'extension' },
@@ -830,7 +833,8 @@ describe('Type System: Input Objects', () => {
830833
input: {
831834
description: 'Argument description.',
832835
type: ScalarType,
833-
defaultValue: 'DefaultValue',
836+
defaultValue: undefined,
837+
externalDefaultValue: 'DefaultValue',
834838
defaultValueLiteral: undefined,
835839
deprecationReason: 'Argument deprecation reason.',
836840
extensions: { someExtension: 'extension' },
@@ -860,6 +864,7 @@ describe('Type System: Input Objects', () => {
860864
description: undefined,
861865
type: ScalarType,
862866
defaultValue: undefined,
867+
externalDefaultValue: undefined,
863868
deprecationReason: undefined,
864869
extensions: {},
865870
astNode: undefined,
@@ -879,6 +884,7 @@ describe('Type System: Input Objects', () => {
879884
description: undefined,
880885
type: ScalarType,
881886
defaultValue: undefined,
887+
externalDefaultValue: undefined,
882888
extensions: {},
883889
deprecationReason: undefined,
884890
astNode: undefined,
@@ -927,14 +933,15 @@ describe('Type System: Input Objects', () => {
927933
const inputObjType = new GraphQLInputObjectType({
928934
name: 'SomeInputObject',
929935
fields: {
930-
f: { type: ScalarType, defaultValue: 3 },
936+
f: { type: ScalarType, externalDefaultValue: 3 },
931937
},
932938
});
933939
expect(inputObjType.getFields().f).to.deep.include({
934940
name: 'f',
935941
description: undefined,
936942
type: ScalarType,
937-
defaultValue: { value: 3 },
943+
defaultValue: undefined,
944+
externalDefaultValue: { value: 3 },
938945
deprecationReason: undefined,
939946
extensions: {},
940947
astNode: undefined,
@@ -955,7 +962,8 @@ describe('Type System: Input Objects', () => {
955962
name: 'f',
956963
description: undefined,
957964
type: ScalarType,
958-
defaultValue: { literal: { kind: 'IntValue', value: '3' } },
965+
defaultValue: undefined,
966+
externalDefaultValue: { literal: { kind: 'IntValue', value: '3' } },
959967
deprecationReason: undefined,
960968
extensions: {},
961969
astNode: undefined,
@@ -968,13 +976,13 @@ describe('Type System: Input Objects', () => {
968976
fields: {
969977
f: {
970978
type: ScalarType,
971-
defaultValue: 3,
979+
externalDefaultValue: 3,
972980
defaultValueLiteral: { kind: Kind.INT, value: '3' },
973981
},
974982
},
975983
});
976984
expect(() => inputObjType.getFields()).to.throw(
977-
'Argument "f" has both a defaultValue and a defaultValueLiteral property, but only one must be provided.',
985+
'Argument "f" has both an externalDefaultValue and a defaultValueLiteral property, but only one must be provided.',
978986
);
979987
});
980988
});

src/type/__tests__/directive-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('Type System: Directive', () => {
4545
description: undefined,
4646
type: GraphQLString,
4747
defaultValue: undefined,
48+
externalDefaultValue: undefined,
4849
deprecationReason: undefined,
4950
extensions: {},
5051
astNode: undefined,
@@ -56,6 +57,7 @@ describe('Type System: Directive', () => {
5657
description: undefined,
5758
type: GraphQLInt,
5859
defaultValue: undefined,
60+
externalDefaultValue: undefined,
5961
deprecationReason: undefined,
6062
extensions: {},
6163
astNode: undefined,

src/type/__tests__/enumType-test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const QueryType = new GraphQLObjectType({
7171
args: {
7272
fromEnum: {
7373
type: ComplexEnum,
74-
defaultValue: 'ONE',
74+
externalDefaultValue: 'ONE',
7575
},
7676
provideGoodValue: { type: GraphQLBoolean },
7777
provideBadValue: { type: GraphQLBoolean },
@@ -90,6 +90,18 @@ const QueryType = new GraphQLObjectType({
9090
return fromEnum;
9191
},
9292
},
93+
complexEnumWithLegacyDefault: {
94+
type: ComplexEnum,
95+
args: {
96+
fromEnum: {
97+
type: ComplexEnum,
98+
defaultValue: Complex1,
99+
},
100+
},
101+
resolve(_source, { fromEnum }) {
102+
return fromEnum;
103+
},
104+
},
93105
thunkValuesString: {
94106
type: GraphQLString,
95107
args: {
@@ -442,6 +454,20 @@ describe('Type System: Enum Values', () => {
442454
});
443455
});
444456

457+
it('may be internally represented with complex values using legacy internal defaults', () => {
458+
const result = executeQuery(`
459+
{
460+
complexEnumWithLegacyDefault
461+
}
462+
`);
463+
464+
expectJSON(result).toDeepEqual({
465+
data: {
466+
complexEnumWithLegacyDefault: 'ONE',
467+
},
468+
});
469+
});
470+
445471
it('may have values specified via a callback', () => {
446472
const result = executeQuery('{ thunkValuesString(fromEnum: B) }');
447473

src/type/__tests__/predicate-test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ describe('Type predicates', () => {
634634
describe('isRequiredArgument', () => {
635635
function buildArg(config: {
636636
type: GraphQLInputType;
637-
defaultValue?: unknown;
637+
externalDefaultValue?: unknown;
638638
}): GraphQLArgument {
639639
const objectType = new GraphQLObjectType({
640640
name: 'SomeType',
@@ -660,7 +660,7 @@ describe('Type predicates', () => {
660660

661661
const optArg2 = buildArg({
662662
type: GraphQLString,
663-
defaultValue: null,
663+
externalDefaultValue: null,
664664
});
665665
expect(isRequiredArgument(optArg2)).to.equal(false);
666666

@@ -671,7 +671,7 @@ describe('Type predicates', () => {
671671

672672
const optArg4 = buildArg({
673673
type: new GraphQLNonNull(GraphQLString),
674-
defaultValue: 'default',
674+
externalDefaultValue: 'default',
675675
});
676676
expect(isRequiredArgument(optArg4)).to.equal(false);
677677
});
@@ -680,7 +680,7 @@ describe('Type predicates', () => {
680680
describe('isRequiredInputField', () => {
681681
function buildInputField(config: {
682682
type: GraphQLInputType;
683-
defaultValue?: unknown;
683+
externalDefaultValue?: unknown;
684684
}): GraphQLInputField {
685685
const inputObjectType = new GraphQLInputObjectType({
686686
name: 'SomeType',
@@ -706,7 +706,7 @@ describe('Type predicates', () => {
706706

707707
const optField2 = buildInputField({
708708
type: GraphQLString,
709-
defaultValue: null,
709+
externalDefaultValue: null,
710710
});
711711
expect(isRequiredInputField(optField2)).to.equal(false);
712712

@@ -717,7 +717,7 @@ describe('Type predicates', () => {
717717

718718
const optField4 = buildInputField({
719719
type: new GraphQLNonNull(GraphQLString),
720-
defaultValue: 'default',
720+
externalDefaultValue: 'default',
721721
});
722722
expect(isRequiredInputField(optField4)).to.equal(false);
723723
});

0 commit comments

Comments
 (0)