UNPKG

6.99 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ValidationContext = exports.SDLValidationContext = exports.ASTValidationContext = void 0;
7
8var _kinds = require("../language/kinds");
9
10var _visitor = require("../language/visitor");
11
12var _TypeInfo = require("../utilities/TypeInfo");
13
14function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
15
16/**
17 * An instance of this class is passed as the "this" context to all validators,
18 * allowing access to commonly useful contextual information from within a
19 * validation rule.
20 */
21var ASTValidationContext =
22/*#__PURE__*/
23function () {
24 function ASTValidationContext(ast, onError) {
25 this._ast = ast;
26 this._errors = [];
27 this._fragments = undefined;
28 this._fragmentSpreads = new Map();
29 this._recursivelyReferencedFragments = new Map();
30 this._onError = onError;
31 }
32
33 var _proto = ASTValidationContext.prototype;
34
35 _proto.reportError = function reportError(error) {
36 this._errors.push(error);
37
38 if (this._onError) {
39 this._onError(error);
40 }
41 } // @deprecated: use onError callback instead - will be removed in v15.
42 ;
43
44 _proto.getErrors = function getErrors() {
45 return this._errors;
46 };
47
48 _proto.getDocument = function getDocument() {
49 return this._ast;
50 };
51
52 _proto.getFragment = function getFragment(name) {
53 var fragments = this._fragments;
54
55 if (!fragments) {
56 this._fragments = fragments = this.getDocument().definitions.reduce(function (frags, statement) {
57 if (statement.kind === _kinds.Kind.FRAGMENT_DEFINITION) {
58 frags[statement.name.value] = statement;
59 }
60
61 return frags;
62 }, Object.create(null));
63 }
64
65 return fragments[name];
66 };
67
68 _proto.getFragmentSpreads = function getFragmentSpreads(node) {
69 var spreads = this._fragmentSpreads.get(node);
70
71 if (!spreads) {
72 spreads = [];
73 var setsToVisit = [node];
74
75 while (setsToVisit.length !== 0) {
76 var set = setsToVisit.pop();
77
78 for (var _i2 = 0, _set$selections2 = set.selections; _i2 < _set$selections2.length; _i2++) {
79 var selection = _set$selections2[_i2];
80
81 if (selection.kind === _kinds.Kind.FRAGMENT_SPREAD) {
82 spreads.push(selection);
83 } else if (selection.selectionSet) {
84 setsToVisit.push(selection.selectionSet);
85 }
86 }
87 }
88
89 this._fragmentSpreads.set(node, spreads);
90 }
91
92 return spreads;
93 };
94
95 _proto.getRecursivelyReferencedFragments = function getRecursivelyReferencedFragments(operation) {
96 var fragments = this._recursivelyReferencedFragments.get(operation);
97
98 if (!fragments) {
99 fragments = [];
100 var collectedNames = Object.create(null);
101 var nodesToVisit = [operation.selectionSet];
102
103 while (nodesToVisit.length !== 0) {
104 var node = nodesToVisit.pop();
105
106 for (var _i4 = 0, _this$getFragmentSpre2 = this.getFragmentSpreads(node); _i4 < _this$getFragmentSpre2.length; _i4++) {
107 var spread = _this$getFragmentSpre2[_i4];
108 var fragName = spread.name.value;
109
110 if (collectedNames[fragName] !== true) {
111 collectedNames[fragName] = true;
112 var fragment = this.getFragment(fragName);
113
114 if (fragment) {
115 fragments.push(fragment);
116 nodesToVisit.push(fragment.selectionSet);
117 }
118 }
119 }
120 }
121
122 this._recursivelyReferencedFragments.set(operation, fragments);
123 }
124
125 return fragments;
126 };
127
128 return ASTValidationContext;
129}();
130
131exports.ASTValidationContext = ASTValidationContext;
132
133var SDLValidationContext =
134/*#__PURE__*/
135function (_ASTValidationContext) {
136 _inheritsLoose(SDLValidationContext, _ASTValidationContext);
137
138 function SDLValidationContext(ast, schema, onError) {
139 var _this;
140
141 _this = _ASTValidationContext.call(this, ast, onError) || this;
142 _this._schema = schema;
143 return _this;
144 }
145
146 var _proto2 = SDLValidationContext.prototype;
147
148 _proto2.getSchema = function getSchema() {
149 return this._schema;
150 };
151
152 return SDLValidationContext;
153}(ASTValidationContext);
154
155exports.SDLValidationContext = SDLValidationContext;
156
157var ValidationContext =
158/*#__PURE__*/
159function (_ASTValidationContext2) {
160 _inheritsLoose(ValidationContext, _ASTValidationContext2);
161
162 function ValidationContext(schema, ast, typeInfo, onError) {
163 var _this2;
164
165 _this2 = _ASTValidationContext2.call(this, ast, onError) || this;
166 _this2._schema = schema;
167 _this2._typeInfo = typeInfo;
168 _this2._variableUsages = new Map();
169 _this2._recursiveVariableUsages = new Map();
170 return _this2;
171 }
172
173 var _proto3 = ValidationContext.prototype;
174
175 _proto3.getSchema = function getSchema() {
176 return this._schema;
177 };
178
179 _proto3.getVariableUsages = function getVariableUsages(node) {
180 var usages = this._variableUsages.get(node);
181
182 if (!usages) {
183 var newUsages = [];
184 var typeInfo = new _TypeInfo.TypeInfo(this._schema);
185 (0, _visitor.visit)(node, (0, _visitor.visitWithTypeInfo)(typeInfo, {
186 VariableDefinition: function VariableDefinition() {
187 return false;
188 },
189 Variable: function Variable(variable) {
190 newUsages.push({
191 node: variable,
192 type: typeInfo.getInputType(),
193 defaultValue: typeInfo.getDefaultValue()
194 });
195 }
196 }));
197 usages = newUsages;
198
199 this._variableUsages.set(node, usages);
200 }
201
202 return usages;
203 };
204
205 _proto3.getRecursiveVariableUsages = function getRecursiveVariableUsages(operation) {
206 var usages = this._recursiveVariableUsages.get(operation);
207
208 if (!usages) {
209 usages = this.getVariableUsages(operation);
210
211 for (var _i6 = 0, _this$getRecursivelyR2 = this.getRecursivelyReferencedFragments(operation); _i6 < _this$getRecursivelyR2.length; _i6++) {
212 var frag = _this$getRecursivelyR2[_i6];
213 usages = usages.concat(this.getVariableUsages(frag));
214 }
215
216 this._recursiveVariableUsages.set(operation, usages);
217 }
218
219 return usages;
220 };
221
222 _proto3.getType = function getType() {
223 return this._typeInfo.getType();
224 };
225
226 _proto3.getParentType = function getParentType() {
227 return this._typeInfo.getParentType();
228 };
229
230 _proto3.getInputType = function getInputType() {
231 return this._typeInfo.getInputType();
232 };
233
234 _proto3.getParentInputType = function getParentInputType() {
235 return this._typeInfo.getParentInputType();
236 };
237
238 _proto3.getFieldDef = function getFieldDef() {
239 return this._typeInfo.getFieldDef();
240 };
241
242 _proto3.getDirective = function getDirective() {
243 return this._typeInfo.getDirective();
244 };
245
246 _proto3.getArgument = function getArgument() {
247 return this._typeInfo.getArgument();
248 };
249
250 return ValidationContext;
251}(ASTValidationContext);
252
253exports.ValidationContext = ValidationContext;