UNPKG

7.01 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __read = (this && this.__read) || function (o, n) {
26 var m = typeof Symbol === "function" && o[Symbol.iterator];
27 if (!m) return o;
28 var i = m.call(o), r, ar = [], e;
29 try {
30 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
31 }
32 catch (error) { e = { error: error }; }
33 finally {
34 try {
35 if (r && !r.done && (m = i["return"])) m.call(i);
36 }
37 finally { if (e) throw e.error; }
38 }
39 return ar;
40};
41var __importDefault = (this && this.__importDefault) || function (mod) {
42 return (mod && mod.__esModule) ? mod : { "default": mod };
43};
44Object.defineProperty(exports, "__esModule", { value: true });
45var codemirror_1 = __importDefault(require("codemirror"));
46var graphql_1 = require("graphql");
47var jsonParse_1 = __importStar(require("../utils/jsonParse"));
48codemirror_1.default.registerHelper('lint', 'graphql-variables', function (text, options, editor) {
49 if (!text) {
50 return [];
51 }
52 var ast;
53 try {
54 ast = (0, jsonParse_1.default)(text);
55 }
56 catch (error) {
57 if (error instanceof jsonParse_1.JSONSyntaxError) {
58 return [lintError(editor, error.position, error.message)];
59 }
60 throw error;
61 }
62 var variableToType = options.variableToType;
63 if (!variableToType) {
64 return [];
65 }
66 return validateVariables(editor, variableToType, ast);
67});
68function validateVariables(editor, variableToType, variablesAST) {
69 var errors = [];
70 variablesAST.members.forEach(function (member) {
71 var _a;
72 if (member) {
73 var variableName = (_a = member.key) === null || _a === void 0 ? void 0 : _a.value;
74 var type = variableToType[variableName];
75 if (!type) {
76 errors.push(lintError(editor, member.key, "Variable \"$".concat(variableName, "\" does not appear in any GraphQL query.")));
77 }
78 else {
79 validateValue(type, member.value).forEach(function (_a) {
80 var _b = __read(_a, 2), node = _b[0], message = _b[1];
81 errors.push(lintError(editor, node, message));
82 });
83 }
84 }
85 });
86 return errors;
87}
88function validateValue(type, valueAST) {
89 if (!type || !valueAST) {
90 return [];
91 }
92 if (type instanceof graphql_1.GraphQLNonNull) {
93 if (valueAST.kind === 'Null') {
94 return [[valueAST, "Type \"".concat(type, "\" is non-nullable and cannot be null.")]];
95 }
96 return validateValue(type.ofType, valueAST);
97 }
98 if (valueAST.kind === 'Null') {
99 return [];
100 }
101 if (type instanceof graphql_1.GraphQLList) {
102 var itemType_1 = type.ofType;
103 if (valueAST.kind === 'Array') {
104 var values = valueAST.values || [];
105 return mapCat(values, function (item) { return validateValue(itemType_1, item); });
106 }
107 return validateValue(itemType_1, valueAST);
108 }
109 if (type instanceof graphql_1.GraphQLInputObjectType) {
110 if (valueAST.kind !== 'Object') {
111 return [[valueAST, "Type \"".concat(type, "\" must be an Object.")]];
112 }
113 var providedFields_1 = Object.create(null);
114 var fieldErrors_1 = mapCat(valueAST.members, function (member) {
115 var _a;
116 var fieldName = (_a = member === null || member === void 0 ? void 0 : member.key) === null || _a === void 0 ? void 0 : _a.value;
117 providedFields_1[fieldName] = true;
118 var inputField = type.getFields()[fieldName];
119 if (!inputField) {
120 return [
121 [
122 member.key,
123 "Type \"".concat(type, "\" does not have a field \"").concat(fieldName, "\"."),
124 ],
125 ];
126 }
127 var fieldType = inputField ? inputField.type : undefined;
128 return validateValue(fieldType, member.value);
129 });
130 Object.keys(type.getFields()).forEach(function (fieldName) {
131 if (!providedFields_1[fieldName]) {
132 var fieldType = type.getFields()[fieldName].type;
133 if (fieldType instanceof graphql_1.GraphQLNonNull) {
134 fieldErrors_1.push([
135 valueAST,
136 "Object of type \"".concat(type, "\" is missing required field \"").concat(fieldName, "\"."),
137 ]);
138 }
139 }
140 });
141 return fieldErrors_1;
142 }
143 if ((type.name === 'Boolean' && valueAST.kind !== 'Boolean') ||
144 (type.name === 'String' && valueAST.kind !== 'String') ||
145 (type.name === 'ID' &&
146 valueAST.kind !== 'Number' &&
147 valueAST.kind !== 'String') ||
148 (type.name === 'Float' && valueAST.kind !== 'Number') ||
149 (type.name === 'Int' &&
150 (valueAST.kind !== 'Number' || (valueAST.value | 0) !== valueAST.value))) {
151 return [[valueAST, "Expected value of type \"".concat(type, "\".")]];
152 }
153 if (type instanceof graphql_1.GraphQLEnumType || type instanceof graphql_1.GraphQLScalarType) {
154 if ((valueAST.kind !== 'String' &&
155 valueAST.kind !== 'Number' &&
156 valueAST.kind !== 'Boolean' &&
157 valueAST.kind !== 'Null') ||
158 isNullish(type.parseValue(valueAST.value))) {
159 return [[valueAST, "Expected value of type \"".concat(type, "\".")]];
160 }
161 }
162 return [];
163}
164function lintError(editor, node, message) {
165 return {
166 message: message,
167 severity: 'error',
168 type: 'validation',
169 from: editor.posFromIndex(node.start),
170 to: editor.posFromIndex(node.end),
171 };
172}
173function isNullish(value) {
174 return value === null || value === undefined || value !== value;
175}
176function mapCat(array, mapper) {
177 return Array.prototype.concat.apply([], array.map(mapper));
178}
179//# sourceMappingURL=lint.js.map
\No newline at end of file