Skip to content

Commit 2be8214

Browse files
committed
simplify
1 parent 54d2f7d commit 2be8214

File tree

2 files changed

+25
-63
lines changed

2 files changed

+25
-63
lines changed

src/execution/values.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import type { GraphQLDirective } from '../type/directives.js';
2020
import type { GraphQLSchema } from '../type/schema.js';
2121

2222
import { coerceInputValue } from '../utilities/coerceInputValue.js';
23-
import {
24-
getVariableSignature,
25-
GraphQLVariableSignature,
26-
} from '../utilities/getVariableSignature.js';
23+
import type { GraphQLVariableSignature } from '../utilities/getVariableSignature.js';
24+
import { getVariableSignature } from '../utilities/getVariableSignature.js';
2725
import { valueFromAST } from '../utilities/valueFromAST.js';
2826

2927
import type { FragmentVariables } from './collectFields.js';
@@ -90,8 +88,8 @@ function coerceVariableValues(
9088

9189
const { name: varName, type: varType } = varSignature;
9290
if (!Object.hasOwn(inputs, varName)) {
93-
if (varSignature.hasDefaultValue) {
94-
coercedValues[varName] = varSignature.getDefaultValue();
91+
if (varDefNode.defaultValue) {
92+
coercedValues[varName] = varSignature.defaultValue;
9593
} else if (isNonNullType(varType)) {
9694
const varTypeStr = inspect(varType);
9795
onError(
@@ -173,9 +171,8 @@ export function experimentalGetArgumentValues(
173171
const argumentNode = argNodeMap.get(name);
174172

175173
if (argumentNode == null) {
176-
const defaultValue = getDefaultValue(argDef);
177-
if (defaultValue !== undefined) {
178-
coercedValues[name] = defaultValue;
174+
if (argDef.defaultValue !== undefined) {
175+
coercedValues[name] = argDef.defaultValue;
179176
} else if (isNonNullType(argType)) {
180177
throw new GraphQLError(
181178
`Argument "${name}" of required type "${inspect(argType)}" ` +
@@ -193,27 +190,25 @@ export function experimentalGetArgumentValues(
193190
const variableName = valueNode.name.value;
194191
if (fragmentVariables?.signatures[variableName]) {
195192
hasValue = fragmentVariables.values[variableName] != null;
196-
const defaultValue = getDefaultValue(argDef);
197-
if (!hasValue && defaultValue !== undefined) {
198-
coercedValues[name] = defaultValue;
193+
if (!hasValue && argDef.defaultValue !== undefined) {
194+
coercedValues[name] = argDef.defaultValue;
199195
continue;
200196
}
201197
} else if (
202198
variableValues != null &&
203199
Object.hasOwn(variableValues, variableName)
204200
) {
205201
hasValue = variableValues[variableName] != null;
202+
} else if (argDef.defaultValue !== undefined) {
203+
coercedValues[name] = argDef.defaultValue;
204+
continue;
205+
} else if (isNonNullType(argType)) {
206+
throw new GraphQLError(
207+
`Argument "${name}" of required type "${inspect(argType)}" ` +
208+
`was provided the variable "$${variableName}" which was not provided a runtime value.`,
209+
{ nodes: valueNode },
210+
);
206211
} else {
207-
const defaultValue = getDefaultValue(argDef);
208-
if (defaultValue !== undefined) {
209-
coercedValues[name] = defaultValue;
210-
} else if (isNonNullType(argType)) {
211-
throw new GraphQLError(
212-
`Argument "${name}" of required type "${inspect(argType)}" ` +
213-
`was provided the variable "$${variableName}" which was not provided a runtime value.`,
214-
{ nodes: valueNode },
215-
);
216-
}
217212
continue;
218213
}
219214
}
@@ -246,14 +241,6 @@ export function experimentalGetArgumentValues(
246241
return coercedValues;
247242
}
248243

249-
function getDefaultValue(
250-
arg: GraphQLArgument | GraphQLVariableSignature,
251-
): unknown {
252-
return arg instanceof GraphQLVariableSignature
253-
? arg.getDefaultValue()
254-
: arg.defaultValue;
255-
}
256-
257244
/**
258245
* Prepares an object map of argument values given a directive definition
259246
* and a AST node which may contain directives. Optionally also accepts a map

src/utilities/getVariableSignature.ts

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,18 @@ import { print } from '../language/printer.js';
55
import type { GraphQLInputType, GraphQLSchema } from '../type/index.js';
66
import { isInputType } from '../type/index.js';
77

8-
import type { ConstValueNode, VariableDefinitionNode } from '../index.js';
8+
import type { VariableDefinitionNode } from '../index.js';
99

1010
import { typeFromAST } from './typeFromAST.js';
1111
import { valueFromAST } from './valueFromAST.js';
1212

1313
/**
1414
* A GraphQLVariableSignature is required to coerce a variable value.
15-
*
16-
* @internal
1715
* */
18-
export class GraphQLVariableSignature {
16+
export interface GraphQLVariableSignature {
1917
name: string;
2018
type: GraphQLInputType;
21-
hasDefaultValue: boolean;
22-
_defaultValue: unknown;
23-
24-
constructor(
25-
name: string,
26-
type: GraphQLInputType,
27-
defaultValueNode: ConstValueNode | undefined,
28-
) {
29-
this.name = name;
30-
this.type = type;
31-
if (defaultValueNode) {
32-
this.hasDefaultValue = true;
33-
this._defaultValue = () => valueFromAST(defaultValueNode, type);
34-
} else {
35-
this.hasDefaultValue = false;
36-
}
37-
}
38-
39-
getDefaultValue(): unknown {
40-
if (typeof this._defaultValue === 'function') {
41-
this._defaultValue = this._defaultValue();
42-
}
43-
return this._defaultValue;
44-
}
19+
defaultValue: unknown;
4520
}
4621

4722
export function getVariableSignature(
@@ -61,9 +36,9 @@ export function getVariableSignature(
6136
);
6237
}
6338

64-
return new GraphQLVariableSignature(
65-
varName,
66-
varType,
67-
varDefNode.defaultValue,
68-
);
39+
return {
40+
name: varName,
41+
type: varType,
42+
defaultValue: valueFromAST(varDefNode.defaultValue, varType),
43+
};
6944
}

0 commit comments

Comments
 (0)