Skip to content

Commit f6fa29e

Browse files
committed
introduce internal getVariableSignature utility
extracted from my fragment arguments scratch branch
1 parent 5c7d4d1 commit f6fa29e

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/execution/values.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { Kind } from '../language/kinds.js';
1414
import { print } from '../language/printer.js';
1515

1616
import type { GraphQLField } from '../type/definition.js';
17-
import { isInputType, isNonNullType } from '../type/definition.js';
17+
import { isNonNullType } from '../type/definition.js';
1818
import type { GraphQLDirective } from '../type/directives.js';
1919
import type { GraphQLSchema } from '../type/schema.js';
2020

2121
import { coerceInputValue } from '../utilities/coerceInputValue.js';
22-
import { typeFromAST } from '../utilities/typeFromAST.js';
22+
import { getVariableSignature } from '../utilities/getVariableSignature.js';
2323
import { valueFromAST } from '../utilities/valueFromAST.js';
2424

2525
type CoercedVariableValues =
@@ -76,24 +76,16 @@ function coerceVariableValues(
7676
): { [variable: string]: unknown } {
7777
const coercedValues: { [variable: string]: unknown } = {};
7878
for (const varDefNode of varDefNodes) {
79-
const varName = varDefNode.variable.name.value;
80-
const varType = typeFromAST(schema, varDefNode.type);
81-
if (!isInputType(varType)) {
82-
// Must use input types for variables. This should be caught during
83-
// validation, however is checked again here for safety.
84-
const varTypeStr = print(varDefNode.type);
85-
onError(
86-
new GraphQLError(
87-
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`,
88-
{ nodes: varDefNode.type },
89-
),
90-
);
79+
const varSignature = getVariableSignature(schema, varDefNode);
80+
if (varSignature instanceof GraphQLError) {
81+
onError(varSignature);
9182
continue;
9283
}
9384

85+
const { name: varName, type: varType } = varSignature;
9486
if (!Object.hasOwn(inputs, varName)) {
9587
if (varDefNode.defaultValue) {
96-
coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
88+
coercedValues[varName] = varSignature.defaultValue;
9789
} else if (isNonNullType(varType)) {
9890
const varTypeStr = inspect(varType);
9991
onError(

src/utilities/getVariableSignature.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { GraphQLError } from '../error/GraphQLError.js';
2+
3+
import { print } from '../language/printer.js';
4+
5+
import type { GraphQLInputType, GraphQLSchema } from '../type/index.js';
6+
import { isInputType } from '../type/index.js';
7+
8+
import type { VariableDefinitionNode } from '../index.js';
9+
10+
import { typeFromAST } from './typeFromAST.js';
11+
import { valueFromAST } from './valueFromAST.js';
12+
13+
/**
14+
* A GraphQLVariableSignature is required to coerce a variable value.
15+
* */
16+
export interface GraphQLVariableSignature {
17+
name: string;
18+
type: GraphQLInputType;
19+
defaultValue: unknown;
20+
}
21+
22+
export function getVariableSignature(
23+
schema: GraphQLSchema,
24+
varDefNode: VariableDefinitionNode,
25+
): GraphQLVariableSignature | GraphQLError {
26+
const varName = varDefNode.variable.name.value;
27+
const varType = typeFromAST(schema, varDefNode.type);
28+
29+
if (!isInputType(varType)) {
30+
// Must use input types for variables. This should be caught during
31+
// validation, however is checked again here for safety.
32+
const varTypeStr = print(varDefNode.type);
33+
return new GraphQLError(
34+
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`,
35+
{ nodes: varDefNode.type },
36+
);
37+
}
38+
39+
return {
40+
name: varName,
41+
type: varType,
42+
defaultValue: valueFromAST(varDefNode.defaultValue, varType),
43+
};
44+
}

0 commit comments

Comments
 (0)