UNPKG

4.69 kBJavaScriptView Raw
1import objectValues from "../../polyfills/objectValues.mjs";
2import keyMap from "../../jsutils/keyMap.mjs";
3import inspect from "../../jsutils/inspect.mjs";
4import didYouMean from "../../jsutils/didYouMean.mjs";
5import suggestionList from "../../jsutils/suggestionList.mjs";
6import { GraphQLError } from "../../error/GraphQLError.mjs";
7import { print } from "../../language/printer.mjs";
8import { isLeafType, isInputObjectType, isListType, isNonNullType, isRequiredInputField, getNullableType, getNamedType } from "../../type/definition.mjs";
9
10/**
11 * Value literals of correct type
12 *
13 * A GraphQL document is only valid if all value literals are of the type
14 * expected at their position.
15 */
16export function ValuesOfCorrectTypeRule(context) {
17 return {
18 ListValue: function ListValue(node) {
19 // Note: TypeInfo will traverse into a list's item type, so look to the
20 // parent input type to check if it is a list.
21 var type = getNullableType(context.getParentInputType());
22
23 if (!isListType(type)) {
24 isValidValueNode(context, node);
25 return false; // Don't traverse further.
26 }
27 },
28 ObjectValue: function ObjectValue(node) {
29 var type = getNamedType(context.getInputType());
30
31 if (!isInputObjectType(type)) {
32 isValidValueNode(context, node);
33 return false; // Don't traverse further.
34 } // Ensure every required field exists.
35
36
37 var fieldNodeMap = keyMap(node.fields, function (field) {
38 return field.name.value;
39 });
40
41 for (var _i2 = 0, _objectValues2 = objectValues(type.getFields()); _i2 < _objectValues2.length; _i2++) {
42 var fieldDef = _objectValues2[_i2];
43 var fieldNode = fieldNodeMap[fieldDef.name];
44
45 if (!fieldNode && isRequiredInputField(fieldDef)) {
46 var typeStr = inspect(fieldDef.type);
47 context.reportError(new GraphQLError("Field \"".concat(type.name, ".").concat(fieldDef.name, "\" of required type \"").concat(typeStr, "\" was not provided."), node));
48 }
49 }
50 },
51 ObjectField: function ObjectField(node) {
52 var parentType = getNamedType(context.getParentInputType());
53 var fieldType = context.getInputType();
54
55 if (!fieldType && isInputObjectType(parentType)) {
56 var suggestions = suggestionList(node.name.value, Object.keys(parentType.getFields()));
57 context.reportError(new GraphQLError("Field \"".concat(node.name.value, "\" is not defined by type \"").concat(parentType.name, "\".") + didYouMean(suggestions), node));
58 }
59 },
60 NullValue: function NullValue(node) {
61 var type = context.getInputType();
62
63 if (isNonNullType(type)) {
64 context.reportError(new GraphQLError("Expected value of type \"".concat(inspect(type), "\", found ").concat(print(node), "."), node));
65 }
66 },
67 EnumValue: function EnumValue(node) {
68 return isValidValueNode(context, node);
69 },
70 IntValue: function IntValue(node) {
71 return isValidValueNode(context, node);
72 },
73 FloatValue: function FloatValue(node) {
74 return isValidValueNode(context, node);
75 },
76 StringValue: function StringValue(node) {
77 return isValidValueNode(context, node);
78 },
79 BooleanValue: function BooleanValue(node) {
80 return isValidValueNode(context, node);
81 }
82 };
83}
84/**
85 * Any value literal may be a valid representation of a Scalar, depending on
86 * that scalar type.
87 */
88
89function isValidValueNode(context, node) {
90 // Report any error at the full type expected by the location.
91 var locationType = context.getInputType();
92
93 if (!locationType) {
94 return;
95 }
96
97 var type = getNamedType(locationType);
98
99 if (!isLeafType(type)) {
100 var typeStr = inspect(locationType);
101 context.reportError(new GraphQLError("Expected value of type \"".concat(typeStr, "\", found ").concat(print(node), "."), node));
102 return;
103 } // Scalars and Enums determine if a literal value is valid via parseLiteral(),
104 // which may throw or return an invalid value to indicate failure.
105
106
107 try {
108 var parseResult = type.parseLiteral(node, undefined
109 /* variables */
110 );
111
112 if (parseResult === undefined) {
113 var _typeStr = inspect(locationType);
114
115 context.reportError(new GraphQLError("Expected value of type \"".concat(_typeStr, "\", found ").concat(print(node), "."), node));
116 }
117 } catch (error) {
118 var _typeStr2 = inspect(locationType);
119
120 if (error instanceof GraphQLError) {
121 context.reportError(error);
122 } else {
123 context.reportError(new GraphQLError("Expected value of type \"".concat(_typeStr2, "\", found ").concat(print(node), "; ") + error.message, node, undefined, undefined, undefined, error));
124 }
125 }
126}