/
ValuesOfCorrectTypeRule.ts
157 lines (148 loc) · 4.78 KB
/
ValuesOfCorrectTypeRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { keyMap } from '../../jsutils/keyMap';
import { inspect } from '../../jsutils/inspect';
import { didYouMean } from '../../jsutils/didYouMean';
import { suggestionList } from '../../jsutils/suggestionList';
import { GraphQLError } from '../../error/GraphQLError';
import type { ValueNode } from '../../language/ast';
import type { ASTVisitor } from '../../language/visitor';
import { print } from '../../language/printer';
import {
isLeafType,
isInputObjectType,
isListType,
isNonNullType,
isRequiredInputField,
getNullableType,
getNamedType,
} from '../../type/definition';
import type { ValidationContext } from '../ValidationContext';
/**
* Value literals of correct type
*
* A GraphQL document is only valid if all value literals are of the type
* expected at their position.
*
* See https://spec.graphql.org/draft/#sec-Values-of-Correct-Type
*/
export function ValuesOfCorrectTypeRule(
context: ValidationContext,
): ASTVisitor {
return {
ListValue(node) {
// Note: TypeInfo will traverse into a list's item type, so look to the
// parent input type to check if it is a list.
const type = getNullableType(context.getParentInputType());
if (!isListType(type)) {
isValidValueNode(context, node);
return false; // Don't traverse further.
}
},
ObjectValue(node) {
const type = getNamedType(context.getInputType());
if (!isInputObjectType(type)) {
isValidValueNode(context, node);
return false; // Don't traverse further.
}
// Ensure every required field exists.
const fieldNodeMap = keyMap(node.fields, (field) => field.name.value);
for (const fieldDef of Object.values(type.getFields())) {
const fieldNode = fieldNodeMap[fieldDef.name];
if (!fieldNode && isRequiredInputField(fieldDef)) {
const typeStr = inspect(fieldDef.type);
context.reportError(
new GraphQLError(
`Field "${type.name}.${fieldDef.name}" of required type "${typeStr}" was not provided.`,
node,
),
);
}
}
},
ObjectField(node) {
const parentType = getNamedType(context.getParentInputType());
const fieldType = context.getInputType();
if (!fieldType && isInputObjectType(parentType)) {
const suggestions = suggestionList(
node.name.value,
Object.keys(parentType.getFields()),
);
context.reportError(
new GraphQLError(
`Field "${node.name.value}" is not defined by type "${parentType.name}".` +
didYouMean(suggestions),
node,
),
);
}
},
NullValue(node) {
const type = context.getInputType();
if (isNonNullType(type)) {
context.reportError(
new GraphQLError(
`Expected value of type "${inspect(type)}", found ${print(node)}.`,
node,
),
);
}
},
EnumValue: (node) => isValidValueNode(context, node),
IntValue: (node) => isValidValueNode(context, node),
FloatValue: (node) => isValidValueNode(context, node),
StringValue: (node) => isValidValueNode(context, node),
BooleanValue: (node) => isValidValueNode(context, node),
};
}
/**
* Any value literal may be a valid representation of a Scalar, depending on
* that scalar type.
*/
function isValidValueNode(context: ValidationContext, node: ValueNode): void {
// Report any error at the full type expected by the location.
const locationType = context.getInputType();
if (!locationType) {
return;
}
const type = getNamedType(locationType);
if (!isLeafType(type)) {
const typeStr = inspect(locationType);
context.reportError(
new GraphQLError(
`Expected value of type "${typeStr}", found ${print(node)}.`,
node,
),
);
return;
}
// Scalars and Enums determine if a literal value is valid via parseLiteral(),
// which may throw or return an invalid value to indicate failure.
try {
const parseResult = type.parseLiteral(node, undefined /* variables */);
if (parseResult === undefined) {
const typeStr = inspect(locationType);
context.reportError(
new GraphQLError(
`Expected value of type "${typeStr}", found ${print(node)}.`,
node,
),
);
}
} catch (error) {
const typeStr = inspect(locationType);
if (error instanceof GraphQLError) {
context.reportError(error);
} else {
context.reportError(
new GraphQLError(
`Expected value of type "${typeStr}", found ${print(node)}; ` +
error.message,
node,
undefined,
undefined,
undefined,
error, // Ensure a reference to the original error is maintained.
),
);
}
}
}