1 | import { visit } from 'graphql/language/visitor';
|
2 | import { InvariantError, invariant } from 'ts-invariant';
|
3 | import { __assign, __spreadArrays } from 'tslib';
|
4 | import stringify from 'fast-json-stable-stringify';
|
5 | export { equal as isEqual } from '@wry/equality';
|
6 |
|
7 | function isScalarValue(value) {
|
8 | return ['StringValue', 'BooleanValue', 'EnumValue'].indexOf(value.kind) > -1;
|
9 | }
|
10 | function isNumberValue(value) {
|
11 | return ['IntValue', 'FloatValue'].indexOf(value.kind) > -1;
|
12 | }
|
13 | function isStringValue(value) {
|
14 | return value.kind === 'StringValue';
|
15 | }
|
16 | function isBooleanValue(value) {
|
17 | return value.kind === 'BooleanValue';
|
18 | }
|
19 | function isIntValue(value) {
|
20 | return value.kind === 'IntValue';
|
21 | }
|
22 | function isFloatValue(value) {
|
23 | return value.kind === 'FloatValue';
|
24 | }
|
25 | function isVariable(value) {
|
26 | return value.kind === 'Variable';
|
27 | }
|
28 | function isObjectValue(value) {
|
29 | return value.kind === 'ObjectValue';
|
30 | }
|
31 | function isListValue(value) {
|
32 | return value.kind === 'ListValue';
|
33 | }
|
34 | function isEnumValue(value) {
|
35 | return value.kind === 'EnumValue';
|
36 | }
|
37 | function isNullValue(value) {
|
38 | return value.kind === 'NullValue';
|
39 | }
|
40 | function valueToObjectRepresentation(argObj, name, value, variables) {
|
41 | if (isIntValue(value) || isFloatValue(value)) {
|
42 | argObj[name.value] = Number(value.value);
|
43 | }
|
44 | else if (isBooleanValue(value) || isStringValue(value)) {
|
45 | argObj[name.value] = value.value;
|
46 | }
|
47 | else if (isObjectValue(value)) {
|
48 | var nestedArgObj_1 = {};
|
49 | value.fields.map(function (obj) {
|
50 | return valueToObjectRepresentation(nestedArgObj_1, obj.name, obj.value, variables);
|
51 | });
|
52 | argObj[name.value] = nestedArgObj_1;
|
53 | }
|
54 | else if (isVariable(value)) {
|
55 | var variableValue = (variables || {})[value.name.value];
|
56 | argObj[name.value] = variableValue;
|
57 | }
|
58 | else if (isListValue(value)) {
|
59 | argObj[name.value] = value.values.map(function (listValue) {
|
60 | var nestedArgArrayObj = {};
|
61 | valueToObjectRepresentation(nestedArgArrayObj, name, listValue, variables);
|
62 | return nestedArgArrayObj[name.value];
|
63 | });
|
64 | }
|
65 | else if (isEnumValue(value)) {
|
66 | argObj[name.value] = value.value;
|
67 | }
|
68 | else if (isNullValue(value)) {
|
69 | argObj[name.value] = null;
|
70 | }
|
71 | else {
|
72 | throw process.env.NODE_ENV === "production" ? new InvariantError(17) : new InvariantError("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\"" +
|
73 | 'is not supported. Use variables instead of inline arguments to ' +
|
74 | 'overcome this limitation.');
|
75 | }
|
76 | }
|
77 | function storeKeyNameFromField(field, variables) {
|
78 | var directivesObj = null;
|
79 | if (field.directives) {
|
80 | directivesObj = {};
|
81 | field.directives.forEach(function (directive) {
|
82 | directivesObj[directive.name.value] = {};
|
83 | if (directive.arguments) {
|
84 | directive.arguments.forEach(function (_a) {
|
85 | var name = _a.name, value = _a.value;
|
86 | return valueToObjectRepresentation(directivesObj[directive.name.value], name, value, variables);
|
87 | });
|
88 | }
|
89 | });
|
90 | }
|
91 | var argObj = null;
|
92 | if (field.arguments && field.arguments.length) {
|
93 | argObj = {};
|
94 | field.arguments.forEach(function (_a) {
|
95 | var name = _a.name, value = _a.value;
|
96 | return valueToObjectRepresentation(argObj, name, value, variables);
|
97 | });
|
98 | }
|
99 | return getStoreKeyName(field.name.value, argObj, directivesObj);
|
100 | }
|
101 | var KNOWN_DIRECTIVES = [
|
102 | 'connection',
|
103 | 'include',
|
104 | 'skip',
|
105 | 'client',
|
106 | 'rest',
|
107 | 'export',
|
108 | ];
|
109 | function getStoreKeyName(fieldName, args, directives) {
|
110 | if (directives &&
|
111 | directives['connection'] &&
|
112 | directives['connection']['key']) {
|
113 | if (directives['connection']['filter'] &&
|
114 | directives['connection']['filter'].length > 0) {
|
115 | var filterKeys = directives['connection']['filter']
|
116 | ? directives['connection']['filter']
|
117 | : [];
|
118 | filterKeys.sort();
|
119 | var queryArgs_1 = args;
|
120 | var filteredArgs_1 = {};
|
121 | filterKeys.forEach(function (key) {
|
122 | filteredArgs_1[key] = queryArgs_1[key];
|
123 | });
|
124 | return directives['connection']['key'] + "(" + JSON.stringify(filteredArgs_1) + ")";
|
125 | }
|
126 | else {
|
127 | return directives['connection']['key'];
|
128 | }
|
129 | }
|
130 | var completeFieldName = fieldName;
|
131 | if (args) {
|
132 | var stringifiedArgs = stringify(args);
|
133 | completeFieldName += "(" + stringifiedArgs + ")";
|
134 | }
|
135 | if (directives) {
|
136 | Object.keys(directives).forEach(function (key) {
|
137 | if (KNOWN_DIRECTIVES.indexOf(key) !== -1)
|
138 | return;
|
139 | if (directives[key] && Object.keys(directives[key]).length) {
|
140 | completeFieldName += "@" + key + "(" + JSON.stringify(directives[key]) + ")";
|
141 | }
|
142 | else {
|
143 | completeFieldName += "@" + key;
|
144 | }
|
145 | });
|
146 | }
|
147 | return completeFieldName;
|
148 | }
|
149 | function argumentsObjectFromField(field, variables) {
|
150 | if (field.arguments && field.arguments.length) {
|
151 | var argObj_1 = {};
|
152 | field.arguments.forEach(function (_a) {
|
153 | var name = _a.name, value = _a.value;
|
154 | return valueToObjectRepresentation(argObj_1, name, value, variables);
|
155 | });
|
156 | return argObj_1;
|
157 | }
|
158 | return null;
|
159 | }
|
160 | function resultKeyNameFromField(field) {
|
161 | return field.alias ? field.alias.value : field.name.value;
|
162 | }
|
163 | function isField(selection) {
|
164 | return selection.kind === 'Field';
|
165 | }
|
166 | function isInlineFragment(selection) {
|
167 | return selection.kind === 'InlineFragment';
|
168 | }
|
169 | function isIdValue(idObject) {
|
170 | return idObject &&
|
171 | idObject.type === 'id' &&
|
172 | typeof idObject.generated === 'boolean';
|
173 | }
|
174 | function toIdValue(idConfig, generated) {
|
175 | if (generated === void 0) { generated = false; }
|
176 | return __assign({ type: 'id', generated: generated }, (typeof idConfig === 'string'
|
177 | ? { id: idConfig, typename: undefined }
|
178 | : idConfig));
|
179 | }
|
180 | function isJsonValue(jsonObject) {
|
181 | return (jsonObject != null &&
|
182 | typeof jsonObject === 'object' &&
|
183 | jsonObject.type === 'json');
|
184 | }
|
185 | function defaultValueFromVariable(node) {
|
186 | throw process.env.NODE_ENV === "production" ? new InvariantError(18) : new InvariantError("Variable nodes are not supported by valueFromNode");
|
187 | }
|
188 | function valueFromNode(node, onVariable) {
|
189 | if (onVariable === void 0) { onVariable = defaultValueFromVariable; }
|
190 | switch (node.kind) {
|
191 | case 'Variable':
|
192 | return onVariable(node);
|
193 | case 'NullValue':
|
194 | return null;
|
195 | case 'IntValue':
|
196 | return parseInt(node.value, 10);
|
197 | case 'FloatValue':
|
198 | return parseFloat(node.value);
|
199 | case 'ListValue':
|
200 | return node.values.map(function (v) { return valueFromNode(v, onVariable); });
|
201 | case 'ObjectValue': {
|
202 | var value = {};
|
203 | for (var _i = 0, _a = node.fields; _i < _a.length; _i++) {
|
204 | var field = _a[_i];
|
205 | value[field.name.value] = valueFromNode(field.value, onVariable);
|
206 | }
|
207 | return value;
|
208 | }
|
209 | default:
|
210 | return node.value;
|
211 | }
|
212 | }
|
213 |
|
214 | function getDirectiveInfoFromField(field, variables) {
|
215 | if (field.directives && field.directives.length) {
|
216 | var directiveObj_1 = {};
|
217 | field.directives.forEach(function (directive) {
|
218 | directiveObj_1[directive.name.value] = argumentsObjectFromField(directive, variables);
|
219 | });
|
220 | return directiveObj_1;
|
221 | }
|
222 | return null;
|
223 | }
|
224 | function shouldInclude(selection, variables) {
|
225 | if (variables === void 0) { variables = {}; }
|
226 | return getInclusionDirectives(selection.directives).every(function (_a) {
|
227 | var directive = _a.directive, ifArgument = _a.ifArgument;
|
228 | var evaledValue = false;
|
229 | if (ifArgument.value.kind === 'Variable') {
|
230 | evaledValue = variables[ifArgument.value.name.value];
|
231 | process.env.NODE_ENV === "production" ? invariant(evaledValue !== void 0, 13) : invariant(evaledValue !== void 0, "Invalid variable referenced in @" + directive.name.value + " directive.");
|
232 | }
|
233 | else {
|
234 | evaledValue = ifArgument.value.value;
|
235 | }
|
236 | return directive.name.value === 'skip' ? !evaledValue : evaledValue;
|
237 | });
|
238 | }
|
239 | function getDirectiveNames(doc) {
|
240 | var names = [];
|
241 | visit(doc, {
|
242 | Directive: function (node) {
|
243 | names.push(node.name.value);
|
244 | },
|
245 | });
|
246 | return names;
|
247 | }
|
248 | function hasDirectives(names, doc) {
|
249 | return getDirectiveNames(doc).some(function (name) { return names.indexOf(name) > -1; });
|
250 | }
|
251 | function hasClientExports(document) {
|
252 | return (document &&
|
253 | hasDirectives(['client'], document) &&
|
254 | hasDirectives(['export'], document));
|
255 | }
|
256 | function isInclusionDirective(_a) {
|
257 | var value = _a.name.value;
|
258 | return value === 'skip' || value === 'include';
|
259 | }
|
260 | function getInclusionDirectives(directives) {
|
261 | return directives ? directives.filter(isInclusionDirective).map(function (directive) {
|
262 | var directiveArguments = directive.arguments;
|
263 | var directiveName = directive.name.value;
|
264 | process.env.NODE_ENV === "production" ? invariant(directiveArguments && directiveArguments.length === 1, 14) : invariant(directiveArguments && directiveArguments.length === 1, "Incorrect number of arguments for the @" + directiveName + " directive.");
|
265 | var ifArgument = directiveArguments[0];
|
266 | process.env.NODE_ENV === "production" ? invariant(ifArgument.name && ifArgument.name.value === 'if', 15) : invariant(ifArgument.name && ifArgument.name.value === 'if', "Invalid argument for the @" + directiveName + " directive.");
|
267 | var ifValue = ifArgument.value;
|
268 | process.env.NODE_ENV === "production" ? invariant(ifValue &&
|
269 | (ifValue.kind === 'Variable' || ifValue.kind === 'BooleanValue'), 16) : invariant(ifValue &&
|
270 | (ifValue.kind === 'Variable' || ifValue.kind === 'BooleanValue'), "Argument for the @" + directiveName + " directive must be a variable or a boolean value.");
|
271 | return { directive: directive, ifArgument: ifArgument };
|
272 | }) : [];
|
273 | }
|
274 |
|
275 | function getFragmentQueryDocument(document, fragmentName) {
|
276 | var actualFragmentName = fragmentName;
|
277 | var fragments = [];
|
278 | document.definitions.forEach(function (definition) {
|
279 | if (definition.kind === 'OperationDefinition') {
|
280 | throw process.env.NODE_ENV === "production" ? new InvariantError(11) : new InvariantError("Found a " + definition.operation + " operation" + (definition.name ? " named '" + definition.name.value + "'" : '') + ". " +
|
281 | 'No operations are allowed when using a fragment as a query. Only fragments are allowed.');
|
282 | }
|
283 | if (definition.kind === 'FragmentDefinition') {
|
284 | fragments.push(definition);
|
285 | }
|
286 | });
|
287 | if (typeof actualFragmentName === 'undefined') {
|
288 | process.env.NODE_ENV === "production" ? invariant(fragments.length === 1, 12) : invariant(fragments.length === 1, "Found " + fragments.length + " fragments. `fragmentName` must be provided when there is not exactly 1 fragment.");
|
289 | actualFragmentName = fragments[0].name.value;
|
290 | }
|
291 | var query = __assign(__assign({}, document), { definitions: __spreadArrays([
|
292 | {
|
293 | kind: 'OperationDefinition',
|
294 | operation: 'query',
|
295 | selectionSet: {
|
296 | kind: 'SelectionSet',
|
297 | selections: [
|
298 | {
|
299 | kind: 'FragmentSpread',
|
300 | name: {
|
301 | kind: 'Name',
|
302 | value: actualFragmentName,
|
303 | },
|
304 | },
|
305 | ],
|
306 | },
|
307 | }
|
308 | ], document.definitions) });
|
309 | return query;
|
310 | }
|
311 |
|
312 | function assign(target) {
|
313 | var sources = [];
|
314 | for (var _i = 1; _i < arguments.length; _i++) {
|
315 | sources[_i - 1] = arguments[_i];
|
316 | }
|
317 | sources.forEach(function (source) {
|
318 | if (typeof source === 'undefined' || source === null) {
|
319 | return;
|
320 | }
|
321 | Object.keys(source).forEach(function (key) {
|
322 | target[key] = source[key];
|
323 | });
|
324 | });
|
325 | return target;
|
326 | }
|
327 |
|
328 | function getMutationDefinition(doc) {
|
329 | checkDocument(doc);
|
330 | var mutationDef = doc.definitions.filter(function (definition) {
|
331 | return definition.kind === 'OperationDefinition' &&
|
332 | definition.operation === 'mutation';
|
333 | })[0];
|
334 | process.env.NODE_ENV === "production" ? invariant(mutationDef, 1) : invariant(mutationDef, 'Must contain a mutation definition.');
|
335 | return mutationDef;
|
336 | }
|
337 | function checkDocument(doc) {
|
338 | process.env.NODE_ENV === "production" ? invariant(doc && doc.kind === 'Document', 2) : invariant(doc && doc.kind === 'Document', "Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql");
|
339 | var operations = doc.definitions
|
340 | .filter(function (d) { return d.kind !== 'FragmentDefinition'; })
|
341 | .map(function (definition) {
|
342 | if (definition.kind !== 'OperationDefinition') {
|
343 | throw process.env.NODE_ENV === "production" ? new InvariantError(3) : new InvariantError("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\"");
|
344 | }
|
345 | return definition;
|
346 | });
|
347 | process.env.NODE_ENV === "production" ? invariant(operations.length <= 1, 4) : invariant(operations.length <= 1, "Ambiguous GraphQL document: contains " + operations.length + " operations");
|
348 | return doc;
|
349 | }
|
350 | function getOperationDefinition(doc) {
|
351 | checkDocument(doc);
|
352 | return doc.definitions.filter(function (definition) { return definition.kind === 'OperationDefinition'; })[0];
|
353 | }
|
354 | function getOperationDefinitionOrDie(document) {
|
355 | var def = getOperationDefinition(document);
|
356 | process.env.NODE_ENV === "production" ? invariant(def, 5) : invariant(def, "GraphQL document is missing an operation");
|
357 | return def;
|
358 | }
|
359 | function getOperationName(doc) {
|
360 | return (doc.definitions
|
361 | .filter(function (definition) {
|
362 | return definition.kind === 'OperationDefinition' && definition.name;
|
363 | })
|
364 | .map(function (x) { return x.name.value; })[0] || null);
|
365 | }
|
366 | function getFragmentDefinitions(doc) {
|
367 | return doc.definitions.filter(function (definition) { return definition.kind === 'FragmentDefinition'; });
|
368 | }
|
369 | function getQueryDefinition(doc) {
|
370 | var queryDef = getOperationDefinition(doc);
|
371 | process.env.NODE_ENV === "production" ? invariant(queryDef && queryDef.operation === 'query', 6) : invariant(queryDef && queryDef.operation === 'query', 'Must contain a query definition.');
|
372 | return queryDef;
|
373 | }
|
374 | function getFragmentDefinition(doc) {
|
375 | process.env.NODE_ENV === "production" ? invariant(doc.kind === 'Document', 7) : invariant(doc.kind === 'Document', "Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql");
|
376 | process.env.NODE_ENV === "production" ? invariant(doc.definitions.length <= 1, 8) : invariant(doc.definitions.length <= 1, 'Fragment must have exactly one definition.');
|
377 | var fragmentDef = doc.definitions[0];
|
378 | process.env.NODE_ENV === "production" ? invariant(fragmentDef.kind === 'FragmentDefinition', 9) : invariant(fragmentDef.kind === 'FragmentDefinition', 'Must be a fragment definition.');
|
379 | return fragmentDef;
|
380 | }
|
381 | function getMainDefinition(queryDoc) {
|
382 | checkDocument(queryDoc);
|
383 | var fragmentDefinition;
|
384 | for (var _i = 0, _a = queryDoc.definitions; _i < _a.length; _i++) {
|
385 | var definition = _a[_i];
|
386 | if (definition.kind === 'OperationDefinition') {
|
387 | var operation = definition.operation;
|
388 | if (operation === 'query' ||
|
389 | operation === 'mutation' ||
|
390 | operation === 'subscription') {
|
391 | return definition;
|
392 | }
|
393 | }
|
394 | if (definition.kind === 'FragmentDefinition' && !fragmentDefinition) {
|
395 | fragmentDefinition = definition;
|
396 | }
|
397 | }
|
398 | if (fragmentDefinition) {
|
399 | return fragmentDefinition;
|
400 | }
|
401 | throw process.env.NODE_ENV === "production" ? new InvariantError(10) : new InvariantError('Expected a parsed GraphQL query with a query, mutation, subscription, or a fragment.');
|
402 | }
|
403 | function createFragmentMap(fragments) {
|
404 | if (fragments === void 0) { fragments = []; }
|
405 | var symTable = {};
|
406 | fragments.forEach(function (fragment) {
|
407 | symTable[fragment.name.value] = fragment;
|
408 | });
|
409 | return symTable;
|
410 | }
|
411 | function getDefaultValues(definition) {
|
412 | if (definition &&
|
413 | definition.variableDefinitions &&
|
414 | definition.variableDefinitions.length) {
|
415 | var defaultValues = definition.variableDefinitions
|
416 | .filter(function (_a) {
|
417 | var defaultValue = _a.defaultValue;
|
418 | return defaultValue;
|
419 | })
|
420 | .map(function (_a) {
|
421 | var variable = _a.variable, defaultValue = _a.defaultValue;
|
422 | var defaultValueObj = {};
|
423 | valueToObjectRepresentation(defaultValueObj, variable.name, defaultValue);
|
424 | return defaultValueObj;
|
425 | });
|
426 | return assign.apply(void 0, __spreadArrays([{}], defaultValues));
|
427 | }
|
428 | return {};
|
429 | }
|
430 | function variablesInOperation(operation) {
|
431 | var names = new Set();
|
432 | if (operation.variableDefinitions) {
|
433 | for (var _i = 0, _a = operation.variableDefinitions; _i < _a.length; _i++) {
|
434 | var definition = _a[_i];
|
435 | names.add(definition.variable.name.value);
|
436 | }
|
437 | }
|
438 | return names;
|
439 | }
|
440 |
|
441 | function filterInPlace(array, test, context) {
|
442 | var target = 0;
|
443 | array.forEach(function (elem, i) {
|
444 | if (test.call(this, elem, i, array)) {
|
445 | array[target++] = elem;
|
446 | }
|
447 | }, context);
|
448 | array.length = target;
|
449 | return array;
|
450 | }
|
451 |
|
452 | var TYPENAME_FIELD = {
|
453 | kind: 'Field',
|
454 | name: {
|
455 | kind: 'Name',
|
456 | value: '__typename',
|
457 | },
|
458 | };
|
459 | function isEmpty(op, fragments) {
|
460 | return op.selectionSet.selections.every(function (selection) {
|
461 | return selection.kind === 'FragmentSpread' &&
|
462 | isEmpty(fragments[selection.name.value], fragments);
|
463 | });
|
464 | }
|
465 | function nullIfDocIsEmpty(doc) {
|
466 | return isEmpty(getOperationDefinition(doc) || getFragmentDefinition(doc), createFragmentMap(getFragmentDefinitions(doc)))
|
467 | ? null
|
468 | : doc;
|
469 | }
|
470 | function getDirectiveMatcher(directives) {
|
471 | return function directiveMatcher(directive) {
|
472 | return directives.some(function (dir) {
|
473 | return (dir.name && dir.name === directive.name.value) ||
|
474 | (dir.test && dir.test(directive));
|
475 | });
|
476 | };
|
477 | }
|
478 | function removeDirectivesFromDocument(directives, doc) {
|
479 | var variablesInUse = Object.create(null);
|
480 | var variablesToRemove = [];
|
481 | var fragmentSpreadsInUse = Object.create(null);
|
482 | var fragmentSpreadsToRemove = [];
|
483 | var modifiedDoc = nullIfDocIsEmpty(visit(doc, {
|
484 | Variable: {
|
485 | enter: function (node, _key, parent) {
|
486 | if (parent.kind !== 'VariableDefinition') {
|
487 | variablesInUse[node.name.value] = true;
|
488 | }
|
489 | },
|
490 | },
|
491 | Field: {
|
492 | enter: function (node) {
|
493 | if (directives && node.directives) {
|
494 | var shouldRemoveField = directives.some(function (directive) { return directive.remove; });
|
495 | if (shouldRemoveField &&
|
496 | node.directives &&
|
497 | node.directives.some(getDirectiveMatcher(directives))) {
|
498 | if (node.arguments) {
|
499 | node.arguments.forEach(function (arg) {
|
500 | if (arg.value.kind === 'Variable') {
|
501 | variablesToRemove.push({
|
502 | name: arg.value.name.value,
|
503 | });
|
504 | }
|
505 | });
|
506 | }
|
507 | if (node.selectionSet) {
|
508 | getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(function (frag) {
|
509 | fragmentSpreadsToRemove.push({
|
510 | name: frag.name.value,
|
511 | });
|
512 | });
|
513 | }
|
514 | return null;
|
515 | }
|
516 | }
|
517 | },
|
518 | },
|
519 | FragmentSpread: {
|
520 | enter: function (node) {
|
521 | fragmentSpreadsInUse[node.name.value] = true;
|
522 | },
|
523 | },
|
524 | Directive: {
|
525 | enter: function (node) {
|
526 | if (getDirectiveMatcher(directives)(node)) {
|
527 | return null;
|
528 | }
|
529 | },
|
530 | },
|
531 | }));
|
532 | if (modifiedDoc &&
|
533 | filterInPlace(variablesToRemove, function (v) { return !variablesInUse[v.name]; }).length) {
|
534 | modifiedDoc = removeArgumentsFromDocument(variablesToRemove, modifiedDoc);
|
535 | }
|
536 | if (modifiedDoc &&
|
537 | filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; })
|
538 | .length) {
|
539 | modifiedDoc = removeFragmentSpreadFromDocument(fragmentSpreadsToRemove, modifiedDoc);
|
540 | }
|
541 | return modifiedDoc;
|
542 | }
|
543 | function addTypenameToDocument(doc) {
|
544 | return visit(checkDocument(doc), {
|
545 | SelectionSet: {
|
546 | enter: function (node, _key, parent) {
|
547 | if (parent &&
|
548 | parent.kind === 'OperationDefinition') {
|
549 | return;
|
550 | }
|
551 | var selections = node.selections;
|
552 | if (!selections) {
|
553 | return;
|
554 | }
|
555 | var skip = selections.some(function (selection) {
|
556 | return (isField(selection) &&
|
557 | (selection.name.value === '__typename' ||
|
558 | selection.name.value.lastIndexOf('__', 0) === 0));
|
559 | });
|
560 | if (skip) {
|
561 | return;
|
562 | }
|
563 | var field = parent;
|
564 | if (isField(field) &&
|
565 | field.directives &&
|
566 | field.directives.some(function (d) { return d.name.value === 'export'; })) {
|
567 | return;
|
568 | }
|
569 | return __assign(__assign({}, node), { selections: __spreadArrays(selections, [TYPENAME_FIELD]) });
|
570 | },
|
571 | },
|
572 | });
|
573 | }
|
574 | var connectionRemoveConfig = {
|
575 | test: function (directive) {
|
576 | var willRemove = directive.name.value === 'connection';
|
577 | if (willRemove) {
|
578 | if (!directive.arguments ||
|
579 | !directive.arguments.some(function (arg) { return arg.name.value === 'key'; })) {
|
580 | process.env.NODE_ENV === "production" || invariant.warn('Removing an @connection directive even though it does not have a key. ' +
|
581 | 'You may want to use the key parameter to specify a store key.');
|
582 | }
|
583 | }
|
584 | return willRemove;
|
585 | },
|
586 | };
|
587 | function removeConnectionDirectiveFromDocument(doc) {
|
588 | return removeDirectivesFromDocument([connectionRemoveConfig], checkDocument(doc));
|
589 | }
|
590 | function hasDirectivesInSelectionSet(directives, selectionSet, nestedCheck) {
|
591 | if (nestedCheck === void 0) { nestedCheck = true; }
|
592 | return (selectionSet &&
|
593 | selectionSet.selections &&
|
594 | selectionSet.selections.some(function (selection) {
|
595 | return hasDirectivesInSelection(directives, selection, nestedCheck);
|
596 | }));
|
597 | }
|
598 | function hasDirectivesInSelection(directives, selection, nestedCheck) {
|
599 | if (nestedCheck === void 0) { nestedCheck = true; }
|
600 | if (!isField(selection)) {
|
601 | return true;
|
602 | }
|
603 | if (!selection.directives) {
|
604 | return false;
|
605 | }
|
606 | return (selection.directives.some(getDirectiveMatcher(directives)) ||
|
607 | (nestedCheck &&
|
608 | hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck)));
|
609 | }
|
610 | function getDirectivesFromDocument(directives, doc) {
|
611 | checkDocument(doc);
|
612 | var parentPath;
|
613 | return nullIfDocIsEmpty(visit(doc, {
|
614 | SelectionSet: {
|
615 | enter: function (node, _key, _parent, path) {
|
616 | var currentPath = path.join('-');
|
617 | if (!parentPath ||
|
618 | currentPath === parentPath ||
|
619 | !currentPath.startsWith(parentPath)) {
|
620 | if (node.selections) {
|
621 | var selectionsWithDirectives = node.selections.filter(function (selection) { return hasDirectivesInSelection(directives, selection); });
|
622 | if (hasDirectivesInSelectionSet(directives, node, false)) {
|
623 | parentPath = currentPath;
|
624 | }
|
625 | return __assign(__assign({}, node), { selections: selectionsWithDirectives });
|
626 | }
|
627 | else {
|
628 | return null;
|
629 | }
|
630 | }
|
631 | },
|
632 | },
|
633 | }));
|
634 | }
|
635 | function getArgumentMatcher(config) {
|
636 | return function argumentMatcher(argument) {
|
637 | return config.some(function (aConfig) {
|
638 | return argument.value &&
|
639 | argument.value.kind === 'Variable' &&
|
640 | argument.value.name &&
|
641 | (aConfig.name === argument.value.name.value ||
|
642 | (aConfig.test && aConfig.test(argument)));
|
643 | });
|
644 | };
|
645 | }
|
646 | function removeArgumentsFromDocument(config, doc) {
|
647 | var argMatcher = getArgumentMatcher(config);
|
648 | return nullIfDocIsEmpty(visit(doc, {
|
649 | OperationDefinition: {
|
650 | enter: function (node) {
|
651 | return __assign(__assign({}, node), { variableDefinitions: node.variableDefinitions.filter(function (varDef) {
|
652 | return !config.some(function (arg) { return arg.name === varDef.variable.name.value; });
|
653 | }) });
|
654 | },
|
655 | },
|
656 | Field: {
|
657 | enter: function (node) {
|
658 | var shouldRemoveField = config.some(function (argConfig) { return argConfig.remove; });
|
659 | if (shouldRemoveField) {
|
660 | var argMatchCount_1 = 0;
|
661 | node.arguments.forEach(function (arg) {
|
662 | if (argMatcher(arg)) {
|
663 | argMatchCount_1 += 1;
|
664 | }
|
665 | });
|
666 | if (argMatchCount_1 === 1) {
|
667 | return null;
|
668 | }
|
669 | }
|
670 | },
|
671 | },
|
672 | Argument: {
|
673 | enter: function (node) {
|
674 | if (argMatcher(node)) {
|
675 | return null;
|
676 | }
|
677 | },
|
678 | },
|
679 | }));
|
680 | }
|
681 | function removeFragmentSpreadFromDocument(config, doc) {
|
682 | function enter(node) {
|
683 | if (config.some(function (def) { return def.name === node.name.value; })) {
|
684 | return null;
|
685 | }
|
686 | }
|
687 | return nullIfDocIsEmpty(visit(doc, {
|
688 | FragmentSpread: { enter: enter },
|
689 | FragmentDefinition: { enter: enter },
|
690 | }));
|
691 | }
|
692 | function getAllFragmentSpreadsFromSelectionSet(selectionSet) {
|
693 | var allFragments = [];
|
694 | selectionSet.selections.forEach(function (selection) {
|
695 | if ((isField(selection) || isInlineFragment(selection)) &&
|
696 | selection.selectionSet) {
|
697 | getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(function (frag) { return allFragments.push(frag); });
|
698 | }
|
699 | else if (selection.kind === 'FragmentSpread') {
|
700 | allFragments.push(selection);
|
701 | }
|
702 | });
|
703 | return allFragments;
|
704 | }
|
705 | function buildQueryFromSelectionSet(document) {
|
706 | var definition = getMainDefinition(document);
|
707 | var definitionOperation = definition.operation;
|
708 | if (definitionOperation === 'query') {
|
709 | return document;
|
710 | }
|
711 | var modifiedDoc = visit(document, {
|
712 | OperationDefinition: {
|
713 | enter: function (node) {
|
714 | return __assign(__assign({}, node), { operation: 'query' });
|
715 | },
|
716 | },
|
717 | });
|
718 | return modifiedDoc;
|
719 | }
|
720 | function removeClientSetsFromDocument(document) {
|
721 | checkDocument(document);
|
722 | var modifiedDoc = removeDirectivesFromDocument([
|
723 | {
|
724 | test: function (directive) { return directive.name.value === 'client'; },
|
725 | remove: true,
|
726 | },
|
727 | ], document);
|
728 | if (modifiedDoc) {
|
729 | modifiedDoc = visit(modifiedDoc, {
|
730 | FragmentDefinition: {
|
731 | enter: function (node) {
|
732 | if (node.selectionSet) {
|
733 | var isTypenameOnly = node.selectionSet.selections.every(function (selection) {
|
734 | return isField(selection) && selection.name.value === '__typename';
|
735 | });
|
736 | if (isTypenameOnly) {
|
737 | return null;
|
738 | }
|
739 | }
|
740 | },
|
741 | },
|
742 | });
|
743 | }
|
744 | return modifiedDoc;
|
745 | }
|
746 |
|
747 | var canUseWeakMap = typeof WeakMap === 'function' && !(typeof navigator === 'object' &&
|
748 | navigator.product === 'ReactNative');
|
749 |
|
750 | var toString = Object.prototype.toString;
|
751 | function cloneDeep(value) {
|
752 | return cloneDeepHelper(value, new Map());
|
753 | }
|
754 | function cloneDeepHelper(val, seen) {
|
755 | switch (toString.call(val)) {
|
756 | case "[object Array]": {
|
757 | if (seen.has(val))
|
758 | return seen.get(val);
|
759 | var copy_1 = val.slice(0);
|
760 | seen.set(val, copy_1);
|
761 | copy_1.forEach(function (child, i) {
|
762 | copy_1[i] = cloneDeepHelper(child, seen);
|
763 | });
|
764 | return copy_1;
|
765 | }
|
766 | case "[object Object]": {
|
767 | if (seen.has(val))
|
768 | return seen.get(val);
|
769 | var copy_2 = Object.create(Object.getPrototypeOf(val));
|
770 | seen.set(val, copy_2);
|
771 | Object.keys(val).forEach(function (key) {
|
772 | copy_2[key] = cloneDeepHelper(val[key], seen);
|
773 | });
|
774 | return copy_2;
|
775 | }
|
776 | default:
|
777 | return val;
|
778 | }
|
779 | }
|
780 |
|
781 | function getEnv() {
|
782 | if (typeof process !== 'undefined' && process.env.NODE_ENV) {
|
783 | return process.env.NODE_ENV;
|
784 | }
|
785 | return 'development';
|
786 | }
|
787 | function isEnv(env) {
|
788 | return getEnv() === env;
|
789 | }
|
790 | function isProduction() {
|
791 | return isEnv('production') === true;
|
792 | }
|
793 | function isDevelopment() {
|
794 | return isEnv('development') === true;
|
795 | }
|
796 | function isTest() {
|
797 | return isEnv('test') === true;
|
798 | }
|
799 |
|
800 | function tryFunctionOrLogError(f) {
|
801 | try {
|
802 | return f();
|
803 | }
|
804 | catch (e) {
|
805 | if (console.error) {
|
806 | console.error(e);
|
807 | }
|
808 | }
|
809 | }
|
810 | function graphQLResultHasError(result) {
|
811 | return result.errors && result.errors.length;
|
812 | }
|
813 |
|
814 | function deepFreeze(o) {
|
815 | Object.freeze(o);
|
816 | Object.getOwnPropertyNames(o).forEach(function (prop) {
|
817 | if (o[prop] !== null &&
|
818 | (typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
819 | !Object.isFrozen(o[prop])) {
|
820 | deepFreeze(o[prop]);
|
821 | }
|
822 | });
|
823 | return o;
|
824 | }
|
825 | function maybeDeepFreeze(obj) {
|
826 | if (isDevelopment() || isTest()) {
|
827 | var symbolIsPolyfilled = typeof Symbol === 'function' && typeof Symbol('') === 'string';
|
828 | if (!symbolIsPolyfilled) {
|
829 | return deepFreeze(obj);
|
830 | }
|
831 | }
|
832 | return obj;
|
833 | }
|
834 |
|
835 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
836 | function mergeDeep() {
|
837 | var sources = [];
|
838 | for (var _i = 0; _i < arguments.length; _i++) {
|
839 | sources[_i] = arguments[_i];
|
840 | }
|
841 | return mergeDeepArray(sources);
|
842 | }
|
843 | function mergeDeepArray(sources) {
|
844 | var target = sources[0] || {};
|
845 | var count = sources.length;
|
846 | if (count > 1) {
|
847 | var pastCopies = [];
|
848 | target = shallowCopyForMerge(target, pastCopies);
|
849 | for (var i = 1; i < count; ++i) {
|
850 | target = mergeHelper(target, sources[i], pastCopies);
|
851 | }
|
852 | }
|
853 | return target;
|
854 | }
|
855 | function isObject(obj) {
|
856 | return obj !== null && typeof obj === 'object';
|
857 | }
|
858 | function mergeHelper(target, source, pastCopies) {
|
859 | if (isObject(source) && isObject(target)) {
|
860 | if (Object.isExtensible && !Object.isExtensible(target)) {
|
861 | target = shallowCopyForMerge(target, pastCopies);
|
862 | }
|
863 | Object.keys(source).forEach(function (sourceKey) {
|
864 | var sourceValue = source[sourceKey];
|
865 | if (hasOwnProperty.call(target, sourceKey)) {
|
866 | var targetValue = target[sourceKey];
|
867 | if (sourceValue !== targetValue) {
|
868 | target[sourceKey] = mergeHelper(shallowCopyForMerge(targetValue, pastCopies), sourceValue, pastCopies);
|
869 | }
|
870 | }
|
871 | else {
|
872 | target[sourceKey] = sourceValue;
|
873 | }
|
874 | });
|
875 | return target;
|
876 | }
|
877 | return source;
|
878 | }
|
879 | function shallowCopyForMerge(value, pastCopies) {
|
880 | if (value !== null &&
|
881 | typeof value === 'object' &&
|
882 | pastCopies.indexOf(value) < 0) {
|
883 | if (Array.isArray(value)) {
|
884 | value = value.slice(0);
|
885 | }
|
886 | else {
|
887 | value = __assign({ __proto__: Object.getPrototypeOf(value) }, value);
|
888 | }
|
889 | pastCopies.push(value);
|
890 | }
|
891 | return value;
|
892 | }
|
893 |
|
894 | var haveWarned = Object.create({});
|
895 | function warnOnceInDevelopment(msg, type) {
|
896 | if (type === void 0) { type = 'warn'; }
|
897 | if (!isProduction() && !haveWarned[msg]) {
|
898 | if (!isTest()) {
|
899 | haveWarned[msg] = true;
|
900 | }
|
901 | if (type === 'error') {
|
902 | console.error(msg);
|
903 | }
|
904 | else {
|
905 | console.warn(msg);
|
906 | }
|
907 | }
|
908 | }
|
909 |
|
910 | function stripSymbols(data) {
|
911 | return JSON.parse(JSON.stringify(data));
|
912 | }
|
913 |
|
914 | export { addTypenameToDocument, argumentsObjectFromField, assign, buildQueryFromSelectionSet, canUseWeakMap, checkDocument, cloneDeep, createFragmentMap, getDefaultValues, getDirectiveInfoFromField, getDirectiveNames, getDirectivesFromDocument, getEnv, getFragmentDefinition, getFragmentDefinitions, getFragmentQueryDocument, getInclusionDirectives, getMainDefinition, getMutationDefinition, getOperationDefinition, getOperationDefinitionOrDie, getOperationName, getQueryDefinition, getStoreKeyName, graphQLResultHasError, hasClientExports, hasDirectives, isDevelopment, isEnv, isField, isIdValue, isInlineFragment, isJsonValue, isNumberValue, isProduction, isScalarValue, isTest, maybeDeepFreeze, mergeDeep, mergeDeepArray, removeArgumentsFromDocument, removeClientSetsFromDocument, removeConnectionDirectiveFromDocument, removeDirectivesFromDocument, removeFragmentSpreadFromDocument, resultKeyNameFromField, shouldInclude, storeKeyNameFromField, stripSymbols, toIdValue, tryFunctionOrLogError, valueFromNode, valueToObjectRepresentation, variablesInOperation, warnOnceInDevelopment };
|
915 |
|