Skip to content

Commit d623120

Browse files
committed
introduce internal getVariableSignature utility
now extracted also to graphql-js PR, see graphql#4175
1 parent 47ff0fb commit d623120

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/execution/values.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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 { getVariableSignature } from '../utilities/getVariableSignature.js';
2324
import { typeFromAST } from '../utilities/typeFromAST.js';
2425
import { valueFromAST } from '../utilities/valueFromAST.js';
2526
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped.js';
@@ -86,24 +87,16 @@ function coerceVariableValues(
8687
): { [variable: string]: unknown } {
8788
const coercedValues: { [variable: string]: unknown } = {};
8889
for (const varDefNode of varDefNodes) {
89-
const varName = varDefNode.variable.name.value;
90-
const varType = typeFromAST(schema, varDefNode.type);
91-
if (!isInputType(varType)) {
92-
// Must use input types for variables. This should be caught during
93-
// validation, however is checked again here for safety.
94-
const varTypeStr = print(varDefNode.type);
95-
onError(
96-
new GraphQLError(
97-
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`,
98-
{ nodes: varDefNode.type },
99-
),
100-
);
90+
const varSignature = getVariableSignature(schema, varDefNode);
91+
if (varSignature instanceof GraphQLError) {
92+
onError(varSignature);
10193
continue;
10294
}
10395

96+
const { name: varName, type: varType } = varSignature;
10497
if (!Object.hasOwn(inputs, varName)) {
10598
if (varDefNode.defaultValue) {
106-
coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
99+
coercedValues[varName] = varSignature.defaultValue;
107100
} else if (isNonNullType(varType)) {
108101
const varTypeStr = inspect(varType);
109102
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)