UNPKG

8.37 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports", "apollo-utilities", "ts-invariant"], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require("apollo-utilities"), require("ts-invariant"));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.apolloUtilities, global.tsInvariant);
11 global.unknown = mod.exports;
12 }
13})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _apolloUtilities, _tsInvariant) {
14
15 _exports.__esModule = true;
16 _exports.check = check;
17 _exports.filter = filter;
18 _exports.propType = propType;
19 _exports.default = void 0;
20
21 function graphql(resolver, document, rootValue, contextValue, variableValues, execOptions) {
22 if (variableValues === void 0) {
23 variableValues = {};
24 }
25
26 if (execOptions === void 0) {
27 execOptions = {};
28 }
29
30 var mainDefinition = (0, _apolloUtilities.getMainDefinition)(document);
31 var fragments = (0, _apolloUtilities.getFragmentDefinitions)(document);
32 var fragmentMap = (0, _apolloUtilities.createFragmentMap)(fragments);
33 var resultMapper = execOptions.resultMapper;
34
35 var fragmentMatcher = execOptions.fragmentMatcher || function () {
36 return true;
37 };
38
39 var execContext = {
40 fragmentMap: fragmentMap,
41 contextValue: contextValue,
42 variableValues: variableValues,
43 resultMapper: resultMapper,
44 resolver: resolver,
45 fragmentMatcher: fragmentMatcher
46 };
47 return executeSelectionSet(mainDefinition.selectionSet, rootValue, execContext);
48 }
49
50 function executeSelectionSet(selectionSet, rootValue, execContext) {
51 var fragmentMap = execContext.fragmentMap,
52 contextValue = execContext.contextValue,
53 variables = execContext.variableValues;
54 var result = {};
55 selectionSet.selections.forEach(function (selection) {
56 if (variables && !(0, _apolloUtilities.shouldInclude)(selection, variables)) {
57 return;
58 }
59
60 if ((0, _apolloUtilities.isField)(selection)) {
61 var fieldResult = executeField(selection, rootValue, execContext);
62 var resultFieldKey = (0, _apolloUtilities.resultKeyNameFromField)(selection);
63
64 if (fieldResult !== undefined) {
65 if (result[resultFieldKey] === undefined) {
66 result[resultFieldKey] = fieldResult;
67 } else {
68 merge(result[resultFieldKey], fieldResult);
69 }
70 }
71 } else {
72 var fragment = void 0;
73
74 if ((0, _apolloUtilities.isInlineFragment)(selection)) {
75 fragment = selection;
76 } else {
77 fragment = fragmentMap[selection.name.value];
78
79 if (!fragment) {
80 throw new Error("No fragment named ".concat(selection.name.value));
81 }
82 }
83
84 var typeCondition = fragment.typeCondition.name.value;
85
86 if (execContext.fragmentMatcher(rootValue, typeCondition, contextValue)) {
87 var fragmentResult = executeSelectionSet(fragment.selectionSet, rootValue, execContext);
88 merge(result, fragmentResult);
89 }
90 }
91 });
92
93 if (execContext.resultMapper) {
94 return execContext.resultMapper(result, rootValue);
95 }
96
97 return result;
98 }
99
100 function executeField(field, rootValue, execContext) {
101 var variables = execContext.variableValues,
102 contextValue = execContext.contextValue,
103 resolver = execContext.resolver;
104 var fieldName = field.name.value;
105 var args = (0, _apolloUtilities.argumentsObjectFromField)(field, variables);
106 var info = {
107 isLeaf: !field.selectionSet,
108 resultKey: (0, _apolloUtilities.resultKeyNameFromField)(field),
109 directives: (0, _apolloUtilities.getDirectiveInfoFromField)(field, variables),
110 field: field
111 };
112 var result = resolver(fieldName, rootValue, args, contextValue, info);
113
114 if (!field.selectionSet) {
115 return result;
116 }
117
118 if (result == null) {
119 return result;
120 }
121
122 if (Array.isArray(result)) {
123 return executeSubSelectedArray(field, result, execContext);
124 }
125
126 return executeSelectionSet(field.selectionSet, result, execContext);
127 }
128
129 function executeSubSelectedArray(field, result, execContext) {
130 return result.map(function (item) {
131 if (item === null) {
132 return null;
133 }
134
135 if (Array.isArray(item)) {
136 return executeSubSelectedArray(field, item, execContext);
137 }
138
139 return executeSelectionSet(field.selectionSet, item, execContext);
140 });
141 }
142
143 var hasOwn = Object.prototype.hasOwnProperty;
144
145 function merge(dest, src) {
146 if (src !== null && typeof src === 'object') {
147 Object.keys(src).forEach(function (key) {
148 var srcVal = src[key];
149
150 if (!hasOwn.call(dest, key)) {
151 dest[key] = srcVal;
152 } else {
153 merge(dest[key], srcVal);
154 }
155 });
156 }
157 }
158
159 var hasOwnProperty = Object.prototype.hasOwnProperty;
160
161 function filter(doc, data, variableValues) {
162 if (variableValues === void 0) {
163 variableValues = {};
164 }
165
166 if (data === null) return data;
167
168 var resolver = function (fieldName, root, args, context, info) {
169 return root[info.resultKey];
170 };
171
172 return Array.isArray(data) ? data.map(function (dataObj) {
173 return graphql(resolver, doc, dataObj, null, variableValues);
174 }) : graphql(resolver, doc, data, null, variableValues);
175 }
176
177 function check(doc, data, variables) {
178 if (variables === void 0) {
179 variables = {};
180 }
181
182 var resolver = function (fieldName, root, args, context, info) {
183 process.env.NODE_ENV === "production" ? (0, _tsInvariant.invariant)(hasOwnProperty.call(root, info.resultKey) || !variables && hasVariableInclusions(info.field.directives), 1) : (0, _tsInvariant.invariant)(hasOwnProperty.call(root, info.resultKey) || !variables && hasVariableInclusions(info.field.directives), "".concat(info.resultKey, " missing on ").concat(JSON.stringify(root)));
184 return root[info.resultKey];
185 };
186
187 graphql(resolver, doc, data, {}, variables, {
188 fragmentMatcher: function () {
189 return false;
190 }
191 });
192 }
193
194 function hasVariableInclusions(directives) {
195 return (0, _apolloUtilities.getInclusionDirectives)(directives).some(function (_a) {
196 var ifArgument = _a.ifArgument;
197 return ifArgument.value && ifArgument.value.kind === 'Variable';
198 });
199 }
200
201 var ANONYMOUS = '<<anonymous>>';
202
203 function PropTypeError(message) {
204 this.message = message;
205 this.stack = '';
206 }
207
208 PropTypeError.prototype = Error.prototype;
209 var reactPropTypeLocationNames = {
210 prop: 'prop',
211 context: 'context',
212 childContext: 'child context'
213 };
214
215 function createChainableTypeChecker(validate) {
216 function checkType(isRequired, props, propName, componentName, location, propFullName) {
217 componentName = componentName || ANONYMOUS;
218 propFullName = propFullName || propName;
219
220 if (props[propName] == null) {
221 var locationName = reactPropTypeLocationNames[location];
222
223 if (isRequired) {
224 if (props[propName] === null) {
225 return new PropTypeError("The ".concat(locationName, " `").concat(propFullName, "` is marked as required ") + "in `".concat(componentName, "`, but its value is `null`."));
226 }
227
228 return new PropTypeError("The ".concat(locationName, " `").concat(propFullName, "` is marked as required in ") + "`".concat(componentName, "`, but its value is `undefined`."));
229 }
230
231 return null;
232 } else {
233 return validate(props, propName, componentName, location, propFullName);
234 }
235 }
236
237 var chainedCheckType = checkType.bind(null, false);
238 chainedCheckType.isRequired = checkType.bind(null, true);
239 return chainedCheckType;
240 }
241
242 function propType(doc, mapPropsToVariables) {
243 if (mapPropsToVariables === void 0) {
244 mapPropsToVariables = function (props) {
245 return null;
246 };
247 }
248
249 return createChainableTypeChecker(function (props, propName) {
250 var prop = props[propName];
251
252 try {
253 if (!prop.loading) {
254 check(doc, prop, mapPropsToVariables(props));
255 }
256
257 return null;
258 } catch (e) {
259 return e;
260 }
261 });
262 }
263
264 var _default = graphql;
265
266 _exports.default = _default;
267});