1 | import objectValues from "../../polyfills/objectValues.mjs";
|
2 | import keyMap from "../../jsutils/keyMap.mjs";
|
3 | import inspect from "../../jsutils/inspect.mjs";
|
4 | import didYouMean from "../../jsutils/didYouMean.mjs";
|
5 | import suggestionList from "../../jsutils/suggestionList.mjs";
|
6 | import { GraphQLError } from "../../error/GraphQLError.mjs";
|
7 | import { print } from "../../language/printer.mjs";
|
8 | import { isLeafType, isInputObjectType, isListType, isNonNullType, isRequiredInputField, getNullableType, getNamedType } from "../../type/definition.mjs";
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function ValuesOfCorrectTypeRule(context) {
|
17 | return {
|
18 | ListValue: function ListValue(node) {
|
19 |
|
20 |
|
21 | var type = getNullableType(context.getParentInputType());
|
22 |
|
23 | if (!isListType(type)) {
|
24 | isValidValueNode(context, node);
|
25 | return false;
|
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;
|
34 | }
|
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 |
|
86 |
|
87 |
|
88 |
|
89 | function isValidValueNode(context, node) {
|
90 |
|
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 | }
|
104 |
|
105 |
|
106 |
|
107 | try {
|
108 | var parseResult = type.parseLiteral(node, undefined
|
109 |
|
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 | }
|