UNPKG

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