1 | 'use strict';
|
2 |
|
3 | var tslib = require('tslib');
|
4 | require('source-map-support/register');
|
5 | var chai = require('chai');
|
6 | var graphql = require('graphql');
|
7 |
|
8 | var docCache = new Map();
|
9 | var fragmentSourceMap = new Map();
|
10 | var printFragmentWarnings = true;
|
11 | var experimentalFragmentVariables = false;
|
12 | function normalize(string) {
|
13 | return string.replace(/[\s,]+/g, ' ').trim();
|
14 | }
|
15 | function cacheKeyFromLoc(loc) {
|
16 | return normalize(loc.source.body.substring(loc.start, loc.end));
|
17 | }
|
18 | function processFragments(ast) {
|
19 | var seenKeys = new Set();
|
20 | var definitions = [];
|
21 | ast.definitions.forEach(function (fragmentDefinition) {
|
22 | if (fragmentDefinition.kind === 'FragmentDefinition') {
|
23 | var fragmentName = fragmentDefinition.name.value;
|
24 | var sourceKey = cacheKeyFromLoc(fragmentDefinition.loc);
|
25 | var sourceKeySet = fragmentSourceMap.get(fragmentName);
|
26 | if (sourceKeySet && !sourceKeySet.has(sourceKey)) {
|
27 | if (printFragmentWarnings) {
|
28 | console.warn("Warning: fragment with name " + fragmentName + " already exists.\n"
|
29 | + "graphql-tag enforces all fragment names across your application to be unique; read more about\n"
|
30 | + "this in the docs: http://dev.apollodata.com/core/fragments.html#unique-names");
|
31 | }
|
32 | }
|
33 | else if (!sourceKeySet) {
|
34 | fragmentSourceMap.set(fragmentName, sourceKeySet = new Set);
|
35 | }
|
36 | sourceKeySet.add(sourceKey);
|
37 | if (!seenKeys.has(sourceKey)) {
|
38 | seenKeys.add(sourceKey);
|
39 | definitions.push(fragmentDefinition);
|
40 | }
|
41 | }
|
42 | else {
|
43 | definitions.push(fragmentDefinition);
|
44 | }
|
45 | });
|
46 | return tslib.__assign(tslib.__assign({}, ast), { definitions: definitions });
|
47 | }
|
48 | function stripLoc(doc) {
|
49 | var workSet = new Set(doc.definitions);
|
50 | workSet.forEach(function (node) {
|
51 | if (node.loc)
|
52 | delete node.loc;
|
53 | Object.keys(node).forEach(function (key) {
|
54 | var value = node[key];
|
55 | if (value && typeof value === 'object') {
|
56 | workSet.add(value);
|
57 | }
|
58 | });
|
59 | });
|
60 | var loc = doc.loc;
|
61 | if (loc) {
|
62 | delete loc.startToken;
|
63 | delete loc.endToken;
|
64 | }
|
65 | return doc;
|
66 | }
|
67 | function parseDocument(source) {
|
68 | var cacheKey = normalize(source);
|
69 | if (!docCache.has(cacheKey)) {
|
70 | var parsed = graphql.parse(source, {
|
71 | experimentalFragmentVariables: 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 | var loader = require('../loader');
|
127 | describe('gql', function () {
|
128 | it('parses queries', function () {
|
129 | chai.assert.equal(gql$1(templateObject_1 || (templateObject_1 = tslib.__makeTemplateObject(["{ testQuery }"], ["{ testQuery }"]))).kind, 'Document');
|
130 | });
|
131 | it('parses queries when called as a function', function () {
|
132 | chai.assert.equal(gql$1('{ testQuery }').kind, 'Document');
|
133 | });
|
134 | it('parses queries with weird substitutions', function () {
|
135 | var obj = Object.create(null);
|
136 | chai.assert.equal(gql$1(templateObject_2 || (templateObject_2 = tslib.__makeTemplateObject(["{ field(input: \"", "\") }"], ["{ field(input: \"", "\") }"])), obj.missing).kind, 'Document');
|
137 | chai.assert.equal(gql$1(templateObject_3 || (templateObject_3 = tslib.__makeTemplateObject(["{ field(input: \"", "\") }"], ["{ field(input: \"", "\") }"])), null).kind, 'Document');
|
138 | chai.assert.equal(gql$1(templateObject_4 || (templateObject_4 = tslib.__makeTemplateObject(["{ field(input: \"", "\") }"], ["{ field(input: \"", "\") }"])), 0).kind, 'Document');
|
139 | });
|
140 | it('allows interpolation of documents generated by the webpack loader', function () {
|
141 | var sameFragment = "fragment SomeFragmentName on SomeType { someField }";
|
142 | var jsSource = loader.call({ cacheable: function () { } }, sameFragment);
|
143 | var module = { exports: Object.create(null) };
|
144 | Function("module", jsSource)(module);
|
145 | var document = gql$1(templateObject_5 || (templateObject_5 = tslib.__makeTemplateObject(["query { ...SomeFragmentName } ", ""], ["query { ...SomeFragmentName } ", ""])), module.exports);
|
146 | chai.assert.equal(document.kind, 'Document');
|
147 | chai.assert.equal(document.definitions.length, 2);
|
148 | chai.assert.equal(document.definitions[0].kind, 'OperationDefinition');
|
149 | chai.assert.equal(document.definitions[1].kind, 'FragmentDefinition');
|
150 | });
|
151 | it('parses queries through webpack loader', function () {
|
152 | var jsSource = loader.call({ cacheable: function () { } }, '{ testQuery }');
|
153 | var module = { exports: Object.create(null) };
|
154 | Function("module", jsSource)(module);
|
155 | chai.assert.equal(module.exports.kind, 'Document');
|
156 | });
|
157 | it('parses single query through webpack loader', function () {
|
158 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n ");
|
159 | var module = { exports: Object.create(null) };
|
160 | Function("module", jsSource)(module);
|
161 | chai.assert.equal(module.exports.kind, 'Document');
|
162 | chai.assert.exists(module.exports.Q1);
|
163 | chai.assert.equal(module.exports.Q1.kind, 'Document');
|
164 | chai.assert.equal(module.exports.Q1.definitions.length, 1);
|
165 | });
|
166 | it('parses single query and exports as default', function () {
|
167 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n ");
|
168 | var module = { exports: Object.create(null) };
|
169 | Function("module", jsSource)(module);
|
170 | chai.assert.deepEqual(module.exports.definitions, module.exports.Q1.definitions);
|
171 | });
|
172 | it('parses multiple queries through webpack loader', function () {
|
173 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n query Q2 { testQuery2 }\n ");
|
174 | var module = { exports: Object.create(null) };
|
175 | Function("module", jsSource)(module);
|
176 | chai.assert.exists(module.exports.Q1);
|
177 | chai.assert.exists(module.exports.Q2);
|
178 | chai.assert.equal(module.exports.Q1.kind, 'Document');
|
179 | chai.assert.equal(module.exports.Q2.kind, 'Document');
|
180 | chai.assert.equal(module.exports.Q1.definitions.length, 1);
|
181 | chai.assert.equal(module.exports.Q2.definitions.length, 1);
|
182 | });
|
183 | it('parses fragments with variable definitions', function () {
|
184 | gql$1.enableExperimentalFragmentVariables();
|
185 | var parsed = gql$1(templateObject_6 || (templateObject_6 = tslib.__makeTemplateObject(["fragment A ($arg: String!) on Type { testQuery }"], ["fragment A ($arg: String!) on Type { testQuery }"])));
|
186 | chai.assert.equal(parsed.kind, 'Document');
|
187 | chai.assert.exists(parsed.definitions[0].variableDefinitions);
|
188 | gql$1.disableExperimentalFragmentVariables();
|
189 | });
|
190 | it('does not nest queries needlessly in named exports', function () {
|
191 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n query Q2 { testQuery2 }\n query Q3 { test Query3 }\n ");
|
192 | var module = { exports: Object.create(null) };
|
193 | Function("module", jsSource)(module);
|
194 | chai.assert.notExists(module.exports.Q2.Q1);
|
195 | chai.assert.notExists(module.exports.Q3.Q1);
|
196 | chai.assert.notExists(module.exports.Q3.Q2);
|
197 | });
|
198 | it('tracks fragment dependencies from multiple queries through webpack loader', function () {
|
199 | var jsSource = loader.call({ cacheable: function () { } }, "\n fragment F1 on F { testQuery }\n fragment F2 on F { testQuery2 }\n fragment F3 on F { testQuery3 }\n query Q1 { ...F1 }\n query Q2 { ...F2 }\n query Q3 {\n ...F1\n ...F2\n }\n ");
|
200 | var module = { exports: Object.create(null) };
|
201 | Function("module", jsSource)(module);
|
202 | chai.assert.exists(module.exports.Q1);
|
203 | chai.assert.exists(module.exports.Q2);
|
204 | chai.assert.exists(module.exports.Q3);
|
205 | var Q1 = module.exports.Q1.definitions;
|
206 | var Q2 = module.exports.Q2.definitions;
|
207 | var Q3 = module.exports.Q3.definitions;
|
208 | chai.assert.equal(Q1.length, 2);
|
209 | chai.assert.equal(Q1[0].name.value, 'Q1');
|
210 | chai.assert.equal(Q1[1].name.value, 'F1');
|
211 | chai.assert.equal(Q2.length, 2);
|
212 | chai.assert.equal(Q2[0].name.value, 'Q2');
|
213 | chai.assert.equal(Q2[1].name.value, 'F2');
|
214 | chai.assert.equal(Q3.length, 3);
|
215 | chai.assert.equal(Q3[0].name.value, 'Q3');
|
216 | chai.assert.equal(Q3[1].name.value, 'F1');
|
217 | chai.assert.equal(Q3[2].name.value, 'F2');
|
218 | var F1 = module.exports.F1.definitions;
|
219 | var F2 = module.exports.F2.definitions;
|
220 | var F3 = module.exports.F3.definitions;
|
221 | chai.assert.equal(F1.length, 1);
|
222 | chai.assert.equal(F1[0].name.value, 'F1');
|
223 | chai.assert.equal(F2.length, 1);
|
224 | chai.assert.equal(F2[0].name.value, 'F2');
|
225 | chai.assert.equal(F3.length, 1);
|
226 | chai.assert.equal(F3[0].name.value, 'F3');
|
227 | });
|
228 | it('tracks fragment dependencies across nested fragments', function () {
|
229 | var jsSource = loader.call({ cacheable: function () { } }, "\n fragment F11 on F { testQuery }\n fragment F22 on F {\n ...F11\n testQuery2\n }\n fragment F33 on F {\n ...F22\n testQuery3\n }\n\n query Q1 {\n ...F33\n }\n\n query Q2 {\n id\n }\n ");
|
230 | var module = { exports: Object.create(null) };
|
231 | Function("module", jsSource)(module);
|
232 | chai.assert.exists(module.exports.Q1);
|
233 | chai.assert.exists(module.exports.Q2);
|
234 | var Q1 = module.exports.Q1.definitions;
|
235 | var Q2 = module.exports.Q2.definitions;
|
236 | chai.assert.equal(Q1.length, 4);
|
237 | chai.assert.equal(Q1[0].name.value, 'Q1');
|
238 | chai.assert.equal(Q1[1].name.value, 'F33');
|
239 | chai.assert.equal(Q1[2].name.value, 'F22');
|
240 | chai.assert.equal(Q1[3].name.value, 'F11');
|
241 | chai.assert.equal(Q2.length, 1);
|
242 | var F11 = module.exports.F11.definitions;
|
243 | var F22 = module.exports.F22.definitions;
|
244 | var F33 = module.exports.F33.definitions;
|
245 | chai.assert.equal(F11.length, 1);
|
246 | chai.assert.equal(F11[0].name.value, 'F11');
|
247 | chai.assert.equal(F22.length, 2);
|
248 | chai.assert.equal(F22[0].name.value, 'F22');
|
249 | chai.assert.equal(F22[1].name.value, 'F11');
|
250 | chai.assert.equal(F33.length, 3);
|
251 | chai.assert.equal(F33[0].name.value, 'F33');
|
252 | chai.assert.equal(F33[1].name.value, 'F22');
|
253 | chai.assert.equal(F33[2].name.value, 'F11');
|
254 | });
|
255 | it('correctly imports other files through the webpack loader', function () {
|
256 | var query = "#import \"./fragment_definition.graphql\"\n query {\n author {\n ...authorDetails\n }\n }";
|
257 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
258 | var module = { exports: Object.create(null) };
|
259 | var require = function (path) {
|
260 | chai.assert.equal(path, './fragment_definition.graphql');
|
261 | return gql$1(templateObject_7 || (templateObject_7 = tslib.__makeTemplateObject(["\n fragment authorDetails on Author {\n firstName\n lastName\n }"], ["\n fragment authorDetails on Author {\n firstName\n lastName\n }"])));
|
262 | };
|
263 | Function("module,require", jsSource)(module, require);
|
264 | chai.assert.equal(module.exports.kind, 'Document');
|
265 | var definitions = module.exports.definitions;
|
266 | chai.assert.equal(definitions.length, 2);
|
267 | chai.assert.equal(definitions[0].kind, 'OperationDefinition');
|
268 | chai.assert.equal(definitions[1].kind, 'FragmentDefinition');
|
269 | });
|
270 | it('tracks fragment dependencies across fragments loaded via the webpack loader', function () {
|
271 | var query = "#import \"./fragment_definition.graphql\"\n fragment F111 on F {\n ...F222\n }\n\n query Q1 {\n ...F111\n }\n\n query Q2 {\n a\n }\n ";
|
272 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
273 | var module = { exports: Object.create(null) };
|
274 | var require = function (path) {
|
275 | chai.assert.equal(path, './fragment_definition.graphql');
|
276 | return gql$1(templateObject_8 || (templateObject_8 = tslib.__makeTemplateObject(["\n fragment F222 on F {\n f1\n f2\n }"], ["\n fragment F222 on F {\n f1\n f2\n }"])));
|
277 | };
|
278 | Function("module,require", jsSource)(module, require);
|
279 | chai.assert.exists(module.exports.Q1);
|
280 | chai.assert.exists(module.exports.Q2);
|
281 | var Q1 = module.exports.Q1.definitions;
|
282 | var Q2 = module.exports.Q2.definitions;
|
283 | chai.assert.equal(Q1.length, 3);
|
284 | chai.assert.equal(Q1[0].name.value, 'Q1');
|
285 | chai.assert.equal(Q1[1].name.value, 'F111');
|
286 | chai.assert.equal(Q1[2].name.value, 'F222');
|
287 | chai.assert.equal(Q2.length, 1);
|
288 | });
|
289 | it('does not complain when presented with normal comments', function (done) {
|
290 | chai.assert.doesNotThrow(function () {
|
291 | var query = "#normal comment\n query {\n author {\n ...authorDetails\n }\n }";
|
292 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
293 | var module = { exports: Object.create(null) };
|
294 | Function("module", jsSource)(module);
|
295 | chai.assert.equal(module.exports.kind, 'Document');
|
296 | done();
|
297 | });
|
298 | });
|
299 | it('returns the same object for the same query', function () {
|
300 | chai.assert.isTrue(gql$1(templateObject_9 || (templateObject_9 = tslib.__makeTemplateObject(["{ sameQuery }"], ["{ sameQuery }"]))) === gql$1(templateObject_10 || (templateObject_10 = tslib.__makeTemplateObject(["{ sameQuery }"], ["{ sameQuery }"]))));
|
301 | });
|
302 | it('returns the same object for the same query, even with whitespace differences', function () {
|
303 | chai.assert.isTrue(gql$1(templateObject_11 || (templateObject_11 = tslib.__makeTemplateObject(["{ sameQuery }"], ["{ sameQuery }"]))) === gql$1(templateObject_12 || (templateObject_12 = tslib.__makeTemplateObject([" { sameQuery, }"], [" { sameQuery, }"]))));
|
304 | });
|
305 | var fragmentAst = gql$1(templateObject_13 || (templateObject_13 = tslib.__makeTemplateObject(["\n fragment UserFragment on User {\n firstName\n lastName\n }\n"], ["\n fragment UserFragment on User {\n firstName\n lastName\n }\n"])));
|
306 | it('returns the same object for the same fragment', function () {
|
307 | chai.assert.isTrue(gql$1(templateObject_14 || (templateObject_14 = tslib.__makeTemplateObject(["fragment same on Same { sameQuery }"], ["fragment same on Same { sameQuery }"]))) === gql$1(templateObject_15 || (templateObject_15 = tslib.__makeTemplateObject(["fragment same on Same { sameQuery }"], ["fragment same on Same { sameQuery }"]))));
|
308 | });
|
309 | it('returns the same object for the same document with substitution', function () {
|
310 | chai.assert.isTrue(gql$1(templateObject_16 || (templateObject_16 = tslib.__makeTemplateObject(["{ ...UserFragment } ", ""], ["{ ...UserFragment } ", ""])), fragmentAst) === gql$1(templateObject_17 || (templateObject_17 = tslib.__makeTemplateObject(["{ ...UserFragment } ", ""], ["{ ...UserFragment } ", ""])), fragmentAst));
|
311 | });
|
312 | it('can reference a fragment that references as fragment', function () {
|
313 | var secondFragmentAst = gql$1(templateObject_18 || (templateObject_18 = tslib.__makeTemplateObject(["\n fragment SecondUserFragment on User {\n ...UserFragment\n }\n ", "\n "], ["\n fragment SecondUserFragment on User {\n ...UserFragment\n }\n ", "\n "])), fragmentAst);
|
314 | var ast = gql$1(templateObject_19 || (templateObject_19 = tslib.__makeTemplateObject(["\n {\n user(id: 5) {\n ...SecondUserFragment\n }\n }\n ", "\n "], ["\n {\n user(id: 5) {\n ...SecondUserFragment\n }\n }\n ", "\n "])), secondFragmentAst);
|
315 | chai.assert.deepEqual(ast, gql$1(templateObject_20 || (templateObject_20 = tslib.__makeTemplateObject(["\n {\n user(id: 5) {\n ...SecondUserFragment\n }\n }\n fragment SecondUserFragment on User {\n ...UserFragment\n }\n fragment UserFragment on User {\n firstName\n lastName\n }\n "], ["\n {\n user(id: 5) {\n ...SecondUserFragment\n }\n }\n fragment SecondUserFragment on User {\n ...UserFragment\n }\n fragment UserFragment on User {\n firstName\n lastName\n }\n "]))));
|
316 | });
|
317 | describe('fragment warnings', function () {
|
318 | var warnings = [];
|
319 | var oldConsoleWarn = console.warn;
|
320 | beforeEach(function () {
|
321 | gql$1.resetCaches();
|
322 | warnings = [];
|
323 | console.warn = function (w) { return warnings.push(w); };
|
324 | });
|
325 | afterEach(function () {
|
326 | console.warn = oldConsoleWarn;
|
327 | });
|
328 | it('warns if you use the same fragment name for different fragments', function () {
|
329 | var frag1 = gql$1(templateObject_21 || (templateObject_21 = tslib.__makeTemplateObject(["fragment TestSame on Bar { fieldOne }"], ["fragment TestSame on Bar { fieldOne }"])));
|
330 | var frag2 = gql$1(templateObject_22 || (templateObject_22 = tslib.__makeTemplateObject(["fragment TestSame on Bar { fieldTwo }"], ["fragment TestSame on Bar { fieldTwo }"])));
|
331 | chai.assert.isFalse(frag1 === frag2);
|
332 | chai.assert.equal(warnings.length, 1);
|
333 | });
|
334 | it('does not warn if you use the same fragment name for the same fragment', function () {
|
335 | var frag1 = gql$1(templateObject_23 || (templateObject_23 = tslib.__makeTemplateObject(["fragment TestDifferent on Bar { fieldOne }"], ["fragment TestDifferent on Bar { fieldOne }"])));
|
336 | var frag2 = gql$1(templateObject_24 || (templateObject_24 = tslib.__makeTemplateObject(["fragment TestDifferent on Bar { fieldOne }"], ["fragment TestDifferent on Bar { fieldOne }"])));
|
337 | chai.assert.isTrue(frag1 === frag2);
|
338 | chai.assert.equal(warnings.length, 0);
|
339 | });
|
340 | it('does not warn if you use the same embedded fragment in two different queries', function () {
|
341 | var frag1 = gql$1(templateObject_25 || (templateObject_25 = tslib.__makeTemplateObject(["fragment TestEmbedded on Bar { field }"], ["fragment TestEmbedded on Bar { field }"])));
|
342 | var query1 = gql$1(templateObject_26 || (templateObject_26 = tslib.__makeTemplateObject(["{ bar { fieldOne ...TestEmbedded } } ", ""], ["{ bar { fieldOne ...TestEmbedded } } ", ""])), frag1);
|
343 | var query2 = gql$1(templateObject_27 || (templateObject_27 = tslib.__makeTemplateObject(["{ bar { fieldTwo ...TestEmbedded } } ", ""], ["{ bar { fieldTwo ...TestEmbedded } } ", ""])), frag1);
|
344 | chai.assert.isFalse(query1 === query2);
|
345 | chai.assert.equal(warnings.length, 0);
|
346 | });
|
347 | it('does not warn if you use the same fragment name for embedded and non-embedded fragments', function () {
|
348 | var frag1 = gql$1(templateObject_28 || (templateObject_28 = tslib.__makeTemplateObject(["fragment TestEmbeddedTwo on Bar { field }"], ["fragment TestEmbeddedTwo on Bar { field }"])));
|
349 | gql$1(templateObject_29 || (templateObject_29 = tslib.__makeTemplateObject(["{ bar { ...TestEmbedded } } ", ""], ["{ bar { ...TestEmbedded } } ", ""])), frag1);
|
350 | gql$1(templateObject_30 || (templateObject_30 = tslib.__makeTemplateObject(["{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }"], ["{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }"])));
|
351 | chai.assert.equal(warnings.length, 0);
|
352 | });
|
353 | });
|
354 | describe('unique fragments', function () {
|
355 | beforeEach(function () {
|
356 | gql$1.resetCaches();
|
357 | });
|
358 | it('strips duplicate fragments from the document', function () {
|
359 | var frag1 = gql$1(templateObject_31 || (templateObject_31 = tslib.__makeTemplateObject(["fragment TestDuplicate on Bar { field }"], ["fragment TestDuplicate on Bar { field }"])));
|
360 | var query1 = gql$1(templateObject_32 || (templateObject_32 = tslib.__makeTemplateObject(["{ bar { fieldOne ...TestDuplicate } } ", " ", ""], ["{ bar { fieldOne ...TestDuplicate } } ", " ", ""])), frag1, frag1);
|
361 | var query2 = gql$1(templateObject_33 || (templateObject_33 = tslib.__makeTemplateObject(["{ bar { fieldOne ...TestDuplicate } } ", ""], ["{ bar { fieldOne ...TestDuplicate } } ", ""])), frag1);
|
362 | chai.assert.equal(query1.definitions.length, 2);
|
363 | chai.assert.equal(query1.definitions[1].kind, 'FragmentDefinition');
|
364 | chai.assert.deepEqual(query1.definitions, query2.definitions);
|
365 | });
|
366 | it('ignores duplicate fragments from second-level imports when using the webpack loader', function () {
|
367 | var load = function (require, query) {
|
368 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
369 | var module = { exports: Object.create(null) };
|
370 | Function("require,module", jsSource)(require, module);
|
371 | return module.exports;
|
372 | };
|
373 | var test_require = function (path) {
|
374 | switch (path) {
|
375 | case './friends.graphql':
|
376 | return load(test_require, [
|
377 | '#import "./person.graphql"',
|
378 | 'fragment friends on Hero { friends { ...person } }',
|
379 | ].join('\n'));
|
380 | case './enemies.graphql':
|
381 | return load(test_require, [
|
382 | '#import "./person.graphql"',
|
383 | 'fragment enemies on Hero { enemies { ...person } }',
|
384 | ].join('\n'));
|
385 | case './person.graphql':
|
386 | return load(test_require, 'fragment person on Person { name }\n');
|
387 | default:
|
388 | return null;
|
389 | }
|
390 | };
|
391 | var result = load(test_require, [
|
392 | '#import "./friends.graphql"',
|
393 | '#import "./enemies.graphql"',
|
394 | 'query { hero { ...friends ...enemies } }',
|
395 | ].join('\n'));
|
396 | chai.assert.equal(result.kind, 'Document');
|
397 | chai.assert.equal(result.definitions.length, 4, 'after deduplication, only 4 fragments should remain');
|
398 | chai.assert.equal(result.definitions[0].kind, 'OperationDefinition');
|
399 | var fragments = result.definitions.slice(1);
|
400 | chai.assert(fragments.every(function (fragment) { return fragment.kind === 'FragmentDefinition'; }));
|
401 | chai.assert(fragments.some(function (fragment) { return fragment.name.value === 'friends'; }));
|
402 | chai.assert(fragments.some(function (fragment) { return fragment.name.value === 'enemies'; }));
|
403 | chai.assert(fragments.some(function (fragment) { return fragment.name.value === 'person'; }));
|
404 | });
|
405 | });
|
406 | });
|
407 | var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14, templateObject_15, templateObject_16, templateObject_17, templateObject_18, templateObject_19, templateObject_20, templateObject_21, templateObject_22, templateObject_23, templateObject_24, templateObject_25, templateObject_26, templateObject_27, templateObject_28, templateObject_29, templateObject_30, templateObject_31, templateObject_32, templateObject_33;
|
408 |
|