UNPKG

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