bundle.umd.js 36 KB

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