bundle.esm.js 34 KB

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