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