1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('tslib'), require('graphql')) :
|
3 | typeof define === 'function' && define.amd ? define(['exports', 'tslib', 'graphql'], factory) :
|
4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['graphql-tag'] = {}, global.tslib, global.graphql));
|
5 | }(this, (function (exports, tslib, graphql) { 'use strict';
|
6 |
|
7 | var docCache = new Map();
|
8 | var fragmentSourceMap = new Map();
|
9 | var printFragmentWarnings = true;
|
10 | var experimentalFragmentVariables = false;
|
11 | function normalize(string) {
|
12 | return string.replace(/[\s,]+/g, ' ').trim();
|
13 | }
|
14 | function cacheKeyFromLoc(loc) {
|
15 | return normalize(loc.source.body.substring(loc.start, loc.end));
|
16 | }
|
17 | function processFragments(ast) {
|
18 | var seenKeys = new Set();
|
19 | var definitions = [];
|
20 | ast.definitions.forEach(function (fragmentDefinition) {
|
21 | if (fragmentDefinition.kind === 'FragmentDefinition') {
|
22 | var fragmentName = fragmentDefinition.name.value;
|
23 | var sourceKey = cacheKeyFromLoc(fragmentDefinition.loc);
|
24 | var sourceKeySet = fragmentSourceMap.get(fragmentName);
|
25 | if (sourceKeySet && !sourceKeySet.has(sourceKey)) {
|
26 | if (printFragmentWarnings) {
|
27 | console.warn("Warning: fragment with name " + fragmentName + " already exists.\n"
|
28 | + "graphql-tag enforces all fragment names across your application to be unique; read more about\n"
|
29 | + "this in the docs: http://dev.apollodata.com/core/fragments.html#unique-names");
|
30 | }
|
31 | }
|
32 | else if (!sourceKeySet) {
|
33 | fragmentSourceMap.set(fragmentName, sourceKeySet = new Set);
|
34 | }
|
35 | sourceKeySet.add(sourceKey);
|
36 | if (!seenKeys.has(sourceKey)) {
|
37 | seenKeys.add(sourceKey);
|
38 | definitions.push(fragmentDefinition);
|
39 | }
|
40 | }
|
41 | else {
|
42 | definitions.push(fragmentDefinition);
|
43 | }
|
44 | });
|
45 | return tslib.__assign(tslib.__assign({}, ast), { definitions: definitions });
|
46 | }
|
47 | function stripLoc(doc) {
|
48 | var workSet = new Set(doc.definitions);
|
49 | workSet.forEach(function (node) {
|
50 | if (node.loc)
|
51 | delete node.loc;
|
52 | Object.keys(node).forEach(function (key) {
|
53 | var value = node[key];
|
54 | if (value && typeof value === 'object') {
|
55 | workSet.add(value);
|
56 | }
|
57 | });
|
58 | });
|
59 | var loc = doc.loc;
|
60 | if (loc) {
|
61 | delete loc.startToken;
|
62 | delete loc.endToken;
|
63 | }
|
64 | return doc;
|
65 | }
|
66 | function parseDocument(source) {
|
67 | var cacheKey = normalize(source);
|
68 | if (!docCache.has(cacheKey)) {
|
69 | var parsed = graphql.parse(source, {
|
70 | experimentalFragmentVariables: experimentalFragmentVariables,
|
71 | allowLegacyFragmentVariables: experimentalFragmentVariables
|
72 | });
|
73 | if (!parsed || parsed.kind !== 'Document') {
|
74 | throw new Error('Not a valid GraphQL document.');
|
75 | }
|
76 | docCache.set(cacheKey, stripLoc(processFragments(parsed)));
|
77 | }
|
78 | return docCache.get(cacheKey);
|
79 | }
|
80 | function gql(literals) {
|
81 | var args = [];
|
82 | for (var _i = 1; _i < arguments.length; _i++) {
|
83 | args[_i - 1] = arguments[_i];
|
84 | }
|
85 | if (typeof literals === 'string') {
|
86 | literals = [literals];
|
87 | }
|
88 | var result = literals[0];
|
89 | args.forEach(function (arg, i) {
|
90 | if (arg && arg.kind === 'Document') {
|
91 | result += arg.loc.source.body;
|
92 | }
|
93 | else {
|
94 | result += arg;
|
95 | }
|
96 | result += literals[i + 1];
|
97 | });
|
98 | return parseDocument(result);
|
99 | }
|
100 | function resetCaches() {
|
101 | docCache.clear();
|
102 | fragmentSourceMap.clear();
|
103 | }
|
104 | function disableFragmentWarnings() {
|
105 | printFragmentWarnings = false;
|
106 | }
|
107 | function enableExperimentalFragmentVariables() {
|
108 | experimentalFragmentVariables = true;
|
109 | }
|
110 | function disableExperimentalFragmentVariables() {
|
111 | experimentalFragmentVariables = false;
|
112 | }
|
113 | var extras = {
|
114 | gql: gql,
|
115 | resetCaches: resetCaches,
|
116 | disableFragmentWarnings: disableFragmentWarnings,
|
117 | enableExperimentalFragmentVariables: enableExperimentalFragmentVariables,
|
118 | disableExperimentalFragmentVariables: disableExperimentalFragmentVariables
|
119 | };
|
120 | (function (gql_1) {
|
121 | gql_1.gql = extras.gql, gql_1.resetCaches = extras.resetCaches, gql_1.disableFragmentWarnings = extras.disableFragmentWarnings, gql_1.enableExperimentalFragmentVariables = extras.enableExperimentalFragmentVariables, gql_1.disableExperimentalFragmentVariables = extras.disableExperimentalFragmentVariables;
|
122 | })(gql || (gql = {}));
|
123 | gql["default"] = gql;
|
124 | var gql$1 = gql;
|
125 |
|
126 | exports.default = gql$1;
|
127 | exports.disableExperimentalFragmentVariables = disableExperimentalFragmentVariables;
|
128 | exports.disableFragmentWarnings = disableFragmentWarnings;
|
129 | exports.enableExperimentalFragmentVariables = enableExperimentalFragmentVariables;
|
130 | exports.gql = gql;
|
131 | exports.resetCaches = resetCaches;
|
132 |
|
133 | Object.defineProperty(exports, '__esModule', { value: true });
|
134 |
|
135 | })));
|
136 |
|