graphql-tag 2.12.4 → 2.12.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,10 +1,20 @@
1
1
  # Change log
2
2
 
3
+ ### v2.12.5
4
+
5
+ * Also publish `src/` directory to npm, enabling source maps. <br/>
6
+ [@maclockard](https://github.com/maclockard) in [#403](https://github.com/apollographql/graphql-tag/pull/403)
7
+
3
8
  ### v2.12.4 (2021-04-29)
4
9
 
5
10
  * Allow fragments to be imported by name when using the webpack loader. <br/>
6
11
  [@dobesv](https://github.com/dobesv) in [#257](https://github.com/apollographql/graphql-tag/pull/257)
7
12
 
13
+ ### v2.12.3
14
+
15
+ * Update `tslib` dependency to version 2.1.0. <br/>
16
+ [@benjamn](http://github.com/benjamn) in [#381](https://github.com/apollographql/graphql-tag/pull/381)
17
+
8
18
  ### v2.12.2
9
19
 
10
20
  * Avoid using `Object.assign` to attach extra properties to `gql`. <br/>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-tag",
3
- "version": "2.12.4",
3
+ "version": "2.12.5",
4
4
  "description": "A JavaScript template literal tag that parses GraphQL queries",
5
5
  "main": "./main.js",
6
6
  "module": "./lib/index.js",
@@ -19,6 +19,7 @@
19
19
  },
20
20
  "files": [
21
21
  "lib/",
22
+ "src/",
22
23
  "loader.js",
23
24
  "main.js"
24
25
  ],
@@ -41,13 +42,13 @@
41
42
  "@types/node": "^14.14.22",
42
43
  "chai": "^4.2.0",
43
44
  "graphql": "^15.4.0",
44
- "mocha": "^8.2.1",
45
+ "mocha": "^9.0.1",
45
46
  "rimraf": "^3.0.2",
46
47
  "rollup": "^2.33.1",
47
48
  "rollup-plugin-sourcemaps": "^0.6.3",
48
49
  "source-map-support": "^0.5.19",
49
50
  "test-all-versions": "^5.0.1",
50
- "typescript": "^4.2.4"
51
+ "typescript": "^4.3.4"
51
52
  },
52
53
  "peerDependencies": {
53
54
  "graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0"
@@ -0,0 +1,7 @@
1
+ // @flow
2
+
3
+ import type { DocumentNode } from 'graphql';
4
+
5
+ declare export default function gql(literals: any, ...placeholders: any[]): DocumentNode;
6
+ declare export function resetCaches(): void;
7
+ declare export function disableFragmentWarnings(): void;
package/src/index.ts ADDED
@@ -0,0 +1,173 @@
1
+ import { parse } from 'graphql';
2
+
3
+ import {
4
+ DocumentNode,
5
+ DefinitionNode,
6
+ Location,
7
+ } from 'graphql/language/ast';
8
+
9
+ // A map docString -> graphql document
10
+ const docCache = new Map<string, DocumentNode>();
11
+
12
+ // A map fragmentName -> [normalized source]
13
+ const fragmentSourceMap = new Map<string, Set<string>>();
14
+
15
+ let printFragmentWarnings = true;
16
+ let experimentalFragmentVariables = false;
17
+
18
+ // Strip insignificant whitespace
19
+ // Note that this could do a lot more, such as reorder fields etc.
20
+ function normalize(string: string) {
21
+ return string.replace(/[\s,]+/g, ' ').trim();
22
+ }
23
+
24
+ function cacheKeyFromLoc(loc: Location) {
25
+ return normalize(loc.source.body.substring(loc.start, loc.end));
26
+ }
27
+
28
+ // Take a unstripped parsed document (query/mutation or even fragment), and
29
+ // check all fragment definitions, checking for name->source uniqueness.
30
+ // We also want to make sure only unique fragments exist in the document.
31
+ function processFragments(ast: DocumentNode) {
32
+ const seenKeys = new Set<string>();
33
+ const definitions: DefinitionNode[] = [];
34
+
35
+ ast.definitions.forEach(fragmentDefinition => {
36
+ if (fragmentDefinition.kind === 'FragmentDefinition') {
37
+ var fragmentName = fragmentDefinition.name.value;
38
+ var sourceKey = cacheKeyFromLoc(fragmentDefinition.loc!);
39
+
40
+ // We know something about this fragment
41
+ let sourceKeySet = fragmentSourceMap.get(fragmentName)!;
42
+ if (sourceKeySet && !sourceKeySet.has(sourceKey)) {
43
+ // this is a problem because the app developer is trying to register another fragment with
44
+ // the same name as one previously registered. So, we tell them about it.
45
+ if (printFragmentWarnings) {
46
+ console.warn("Warning: fragment with name " + fragmentName + " already exists.\n"
47
+ + "graphql-tag enforces all fragment names across your application to be unique; read more about\n"
48
+ + "this in the docs: http://dev.apollodata.com/core/fragments.html#unique-names");
49
+ }
50
+ } else if (!sourceKeySet) {
51
+ fragmentSourceMap.set(fragmentName, sourceKeySet = new Set);
52
+ }
53
+
54
+ sourceKeySet.add(sourceKey);
55
+
56
+ if (!seenKeys.has(sourceKey)) {
57
+ seenKeys.add(sourceKey);
58
+ definitions.push(fragmentDefinition);
59
+ }
60
+ } else {
61
+ definitions.push(fragmentDefinition);
62
+ }
63
+ });
64
+
65
+ return {
66
+ ...ast,
67
+ definitions,
68
+ };
69
+ }
70
+
71
+ function stripLoc(doc: DocumentNode) {
72
+ const workSet = new Set<Record<string, any>>(doc.definitions);
73
+
74
+ workSet.forEach(node => {
75
+ if (node.loc) delete node.loc;
76
+ Object.keys(node).forEach(key => {
77
+ const value = node[key];
78
+ if (value && typeof value === 'object') {
79
+ workSet.add(value);
80
+ }
81
+ });
82
+ });
83
+
84
+ const loc = doc.loc as Record<string, any>;
85
+ if (loc) {
86
+ delete loc.startToken;
87
+ delete loc.endToken;
88
+ }
89
+
90
+ return doc;
91
+ }
92
+
93
+ function parseDocument(source: string) {
94
+ var cacheKey = normalize(source);
95
+ if (!docCache.has(cacheKey)) {
96
+ const parsed = parse(source, {
97
+ experimentalFragmentVariables,
98
+ });
99
+ if (!parsed || parsed.kind !== 'Document') {
100
+ throw new Error('Not a valid GraphQL document.');
101
+ }
102
+ docCache.set(
103
+ cacheKey,
104
+ // check that all "new" fragments inside the documents are consistent with
105
+ // existing fragments of the same name
106
+ stripLoc(processFragments(parsed)),
107
+ );
108
+ }
109
+ return docCache.get(cacheKey)!;
110
+ }
111
+
112
+ // XXX This should eventually disallow arbitrary string interpolation, like Relay does
113
+ export function gql(
114
+ literals: string | readonly string[],
115
+ ...args: any[]
116
+ ) {
117
+
118
+ if (typeof literals === 'string') {
119
+ literals = [literals];
120
+ }
121
+
122
+ let result = literals[0];
123
+
124
+ args.forEach((arg, i) => {
125
+ if (arg && arg.kind === 'Document') {
126
+ result += arg.loc.source.body;
127
+ } else {
128
+ result += arg;
129
+ }
130
+ result += literals[i + 1];
131
+ });
132
+
133
+ return parseDocument(result);
134
+ }
135
+
136
+ export function resetCaches() {
137
+ docCache.clear();
138
+ fragmentSourceMap.clear();
139
+ }
140
+
141
+ export function disableFragmentWarnings() {
142
+ printFragmentWarnings = false;
143
+ }
144
+
145
+ export function enableExperimentalFragmentVariables() {
146
+ experimentalFragmentVariables = true;
147
+ }
148
+
149
+ export function disableExperimentalFragmentVariables() {
150
+ experimentalFragmentVariables = false;
151
+ }
152
+
153
+ const extras = {
154
+ gql,
155
+ resetCaches,
156
+ disableFragmentWarnings,
157
+ enableExperimentalFragmentVariables,
158
+ disableExperimentalFragmentVariables,
159
+ };
160
+
161
+ export namespace gql {
162
+ export const {
163
+ gql,
164
+ resetCaches,
165
+ disableFragmentWarnings,
166
+ enableExperimentalFragmentVariables,
167
+ disableExperimentalFragmentVariables,
168
+ } = extras;
169
+ }
170
+
171
+ gql.default = gql;
172
+
173
+ export default gql;
package/src/tests.ts ADDED
@@ -0,0 +1,491 @@
1
+ import 'source-map-support/register';
2
+
3
+ import { assert } from 'chai';
4
+ import { DocumentNode, FragmentDefinitionNode } from 'graphql';
5
+
6
+ import gql from './index';
7
+ const loader = require('../loader');
8
+
9
+ describe('gql', () => {
10
+ it('parses queries', () => {
11
+ assert.equal(gql`{ testQuery }`.kind, 'Document');
12
+ });
13
+
14
+ it('parses queries when called as a function', () => {
15
+ assert.equal(gql('{ testQuery }').kind, 'Document');
16
+ });
17
+
18
+ it('parses queries with weird substitutions', () => {
19
+ const obj = Object.create(null);
20
+ assert.equal(gql`{ field(input: "${obj.missing}") }`.kind, 'Document');
21
+ assert.equal(gql`{ field(input: "${null}") }`.kind, 'Document');
22
+ assert.equal(gql`{ field(input: "${0}") }`.kind, 'Document');
23
+ });
24
+
25
+ it('allows interpolation of documents generated by the webpack loader', () => {
26
+ const sameFragment = "fragment SomeFragmentName on SomeType { someField }";
27
+
28
+ const jsSource = loader.call(
29
+ { cacheable() {} },
30
+ sameFragment,
31
+ );
32
+ const module = { exports: Object.create(null) };
33
+
34
+ Function("module", jsSource)(module);
35
+
36
+ const document = gql`query { ...SomeFragmentName } ${module.exports}`;
37
+ assert.equal(document.kind, 'Document');
38
+ assert.equal(document.definitions.length, 2);
39
+ assert.equal(document.definitions[0].kind, 'OperationDefinition');
40
+ assert.equal(document.definitions[1].kind, 'FragmentDefinition');
41
+ });
42
+
43
+ it('parses queries through webpack loader', () => {
44
+ const jsSource = loader.call({ cacheable() {} }, '{ testQuery }');
45
+ const module = { exports: Object.create(null) };
46
+ Function("module", jsSource)(module);
47
+ assert.equal(module.exports.kind, 'Document');
48
+ });
49
+
50
+ it('parses single query through webpack loader', () => {
51
+ const jsSource = loader.call({ cacheable() {} }, `
52
+ query Q1 { testQuery }
53
+ `);
54
+ const module = { exports: Object.create(null) };
55
+ Function("module", jsSource)(module);
56
+
57
+ assert.equal(module.exports.kind, 'Document');
58
+ assert.exists(module.exports.Q1);
59
+ assert.equal(module.exports.Q1.kind, 'Document');
60
+ assert.equal(module.exports.Q1.definitions.length, 1);
61
+ });
62
+
63
+ it('parses single query and exports as default', () => {
64
+ const jsSource = loader.call({ cacheable() {} }, `
65
+ query Q1 { testQuery }
66
+ `);
67
+ const module = { exports: Object.create(null) };
68
+ Function("module", jsSource)(module);
69
+ assert.deepEqual(module.exports.definitions, module.exports.Q1.definitions);
70
+ });
71
+
72
+ it('parses multiple queries through webpack loader', () => {
73
+ const jsSource = loader.call({ cacheable() {} }, `
74
+ query Q1 { testQuery }
75
+ query Q2 { testQuery2 }
76
+ `);
77
+ const module = { exports: Object.create(null) };
78
+ Function("module", jsSource)(module);
79
+
80
+ assert.exists(module.exports.Q1);
81
+ assert.exists(module.exports.Q2);
82
+ assert.equal(module.exports.Q1.kind, 'Document');
83
+ assert.equal(module.exports.Q2.kind, 'Document');
84
+ assert.equal(module.exports.Q1.definitions.length, 1);
85
+ assert.equal(module.exports.Q2.definitions.length, 1);
86
+ });
87
+
88
+ it('parses fragments with variable definitions', () => {
89
+ gql.enableExperimentalFragmentVariables();
90
+
91
+ const parsed: any = gql`fragment A ($arg: String!) on Type { testQuery }`;
92
+ assert.equal(parsed.kind, 'Document');
93
+ assert.exists(parsed.definitions[0].variableDefinitions);
94
+
95
+ gql.disableExperimentalFragmentVariables()
96
+ });
97
+
98
+ // see https://github.com/apollographql/graphql-tag/issues/168
99
+ it('does not nest queries needlessly in named exports', () => {
100
+ const jsSource = loader.call({ cacheable() {} }, `
101
+ query Q1 { testQuery }
102
+ query Q2 { testQuery2 }
103
+ query Q3 { test Query3 }
104
+ `);
105
+ const module = { exports: Object.create(null) };
106
+ Function("module", jsSource)(module);
107
+
108
+ assert.notExists(module.exports.Q2.Q1);
109
+ assert.notExists(module.exports.Q3.Q1);
110
+ assert.notExists(module.exports.Q3.Q2);
111
+ });
112
+
113
+ it('tracks fragment dependencies from multiple queries through webpack loader', () => {
114
+ const jsSource = loader.call({ cacheable() {} }, `
115
+ fragment F1 on F { testQuery }
116
+ fragment F2 on F { testQuery2 }
117
+ fragment F3 on F { testQuery3 }
118
+ query Q1 { ...F1 }
119
+ query Q2 { ...F2 }
120
+ query Q3 {
121
+ ...F1
122
+ ...F2
123
+ }
124
+ `);
125
+ const module = { exports: Object.create(null) };
126
+ Function("module", jsSource)(module);
127
+
128
+ assert.exists(module.exports.Q1);
129
+ assert.exists(module.exports.Q2);
130
+ assert.exists(module.exports.Q3);
131
+ const Q1 = module.exports.Q1.definitions;
132
+ const Q2 = module.exports.Q2.definitions;
133
+ const Q3 = module.exports.Q3.definitions;
134
+
135
+ assert.equal(Q1.length, 2);
136
+ assert.equal(Q1[0].name.value, 'Q1');
137
+ assert.equal(Q1[1].name.value, 'F1');
138
+
139
+ assert.equal(Q2.length, 2);
140
+ assert.equal(Q2[0].name.value, 'Q2');
141
+ assert.equal(Q2[1].name.value, 'F2');
142
+
143
+ assert.equal(Q3.length, 3);
144
+ assert.equal(Q3[0].name.value, 'Q3');
145
+ assert.equal(Q3[1].name.value, 'F1');
146
+ assert.equal(Q3[2].name.value, 'F2');
147
+
148
+ const F1 = module.exports.F1.definitions;
149
+ const F2 = module.exports.F2.definitions;
150
+ const F3 = module.exports.F3.definitions;
151
+
152
+ assert.equal(F1.length, 1);
153
+ assert.equal(F1[0].name.value, 'F1');
154
+ assert.equal(F2.length, 1);
155
+ assert.equal(F2[0].name.value, 'F2');
156
+ assert.equal(F3.length, 1);
157
+ assert.equal(F3[0].name.value, 'F3');
158
+
159
+ });
160
+
161
+ it('tracks fragment dependencies across nested fragments', () => {
162
+ const jsSource = loader.call({ cacheable() {} }, `
163
+ fragment F11 on F { testQuery }
164
+ fragment F22 on F {
165
+ ...F11
166
+ testQuery2
167
+ }
168
+ fragment F33 on F {
169
+ ...F22
170
+ testQuery3
171
+ }
172
+
173
+ query Q1 {
174
+ ...F33
175
+ }
176
+
177
+ query Q2 {
178
+ id
179
+ }
180
+ `);
181
+
182
+ const module = { exports: Object.create(null) };
183
+ Function("module", jsSource)(module);
184
+
185
+ assert.exists(module.exports.Q1);
186
+ assert.exists(module.exports.Q2);
187
+
188
+ const Q1 = module.exports.Q1.definitions;
189
+ const Q2 = module.exports.Q2.definitions;
190
+
191
+ assert.equal(Q1.length, 4);
192
+ assert.equal(Q1[0].name.value, 'Q1');
193
+ assert.equal(Q1[1].name.value, 'F33');
194
+ assert.equal(Q1[2].name.value, 'F22');
195
+ assert.equal(Q1[3].name.value, 'F11');
196
+
197
+ assert.equal(Q2.length, 1);
198
+
199
+ const F11 = module.exports.F11.definitions;
200
+ const F22 = module.exports.F22.definitions;
201
+ const F33 = module.exports.F33.definitions;
202
+
203
+ assert.equal(F11.length, 1);
204
+ assert.equal(F11[0].name.value, 'F11');
205
+ assert.equal(F22.length, 2);
206
+ assert.equal(F22[0].name.value, 'F22');
207
+ assert.equal(F22[1].name.value, 'F11');
208
+ assert.equal(F33.length, 3);
209
+ assert.equal(F33[0].name.value, 'F33');
210
+ assert.equal(F33[1].name.value, 'F22');
211
+ assert.equal(F33[2].name.value, 'F11');
212
+ });
213
+
214
+ it('correctly imports other files through the webpack loader', () => {
215
+ const query = `#import "./fragment_definition.graphql"
216
+ query {
217
+ author {
218
+ ...authorDetails
219
+ }
220
+ }`;
221
+ const jsSource = loader.call({ cacheable() {} }, query);
222
+ const module = { exports: Object.create(null) };
223
+ const require = (path: string) => {
224
+ assert.equal(path, './fragment_definition.graphql');
225
+ return gql`
226
+ fragment authorDetails on Author {
227
+ firstName
228
+ lastName
229
+ }`;
230
+ };
231
+ Function("module,require", jsSource)(module, require);
232
+ assert.equal(module.exports.kind, 'Document');
233
+ const definitions = module.exports.definitions;
234
+ assert.equal(definitions.length, 2);
235
+ assert.equal(definitions[0].kind, 'OperationDefinition');
236
+ assert.equal(definitions[1].kind, 'FragmentDefinition');
237
+ });
238
+
239
+ it('tracks fragment dependencies across fragments loaded via the webpack loader', () => {
240
+ const query = `#import "./fragment_definition.graphql"
241
+ fragment F111 on F {
242
+ ...F222
243
+ }
244
+
245
+ query Q1 {
246
+ ...F111
247
+ }
248
+
249
+ query Q2 {
250
+ a
251
+ }
252
+ `;
253
+ const jsSource = loader.call({ cacheable() {} }, query);
254
+ const module = { exports: Object.create(null) };
255
+ const require = (path: string) => {
256
+ assert.equal(path, './fragment_definition.graphql');
257
+ return gql`
258
+ fragment F222 on F {
259
+ f1
260
+ f2
261
+ }`;
262
+ };
263
+ Function("module,require", jsSource)(module, require);
264
+
265
+ assert.exists(module.exports.Q1);
266
+ assert.exists(module.exports.Q2);
267
+
268
+ const Q1 = module.exports.Q1.definitions;
269
+ const Q2 = module.exports.Q2.definitions;
270
+
271
+ assert.equal(Q1.length, 3);
272
+ assert.equal(Q1[0].name.value, 'Q1');
273
+ assert.equal(Q1[1].name.value, 'F111');
274
+ assert.equal(Q1[2].name.value, 'F222');
275
+
276
+ assert.equal(Q2.length, 1);
277
+ });
278
+
279
+ it('does not complain when presented with normal comments', (done) => {
280
+ assert.doesNotThrow(() => {
281
+ const query = `#normal comment
282
+ query {
283
+ author {
284
+ ...authorDetails
285
+ }
286
+ }`;
287
+ const jsSource = loader.call({ cacheable() {} }, query);
288
+ const module = { exports: Object.create(null) };
289
+ Function("module", jsSource)(module);
290
+ assert.equal(module.exports.kind, 'Document');
291
+ done();
292
+ });
293
+ });
294
+
295
+ it('returns the same object for the same query', () => {
296
+ assert.isTrue(gql`{ sameQuery }` === gql`{ sameQuery }`);
297
+ });
298
+
299
+ it('returns the same object for the same query, even with whitespace differences', () => {
300
+ assert.isTrue(gql`{ sameQuery }` === gql` { sameQuery, }`);
301
+ });
302
+
303
+ const fragmentAst = gql`
304
+ fragment UserFragment on User {
305
+ firstName
306
+ lastName
307
+ }
308
+ `;
309
+
310
+ it('returns the same object for the same fragment', () => {
311
+ assert.isTrue(gql`fragment same on Same { sameQuery }` ===
312
+ gql`fragment same on Same { sameQuery }`);
313
+ });
314
+
315
+ it('returns the same object for the same document with substitution', () => {
316
+ // We know that calling `gql` on a fragment string will always return
317
+ // the same document, so we can reuse `fragmentAst`
318
+ assert.isTrue(gql`{ ...UserFragment } ${fragmentAst}` ===
319
+ gql`{ ...UserFragment } ${fragmentAst}`);
320
+ });
321
+
322
+ it('can reference a fragment that references as fragment', () => {
323
+ const secondFragmentAst = gql`
324
+ fragment SecondUserFragment on User {
325
+ ...UserFragment
326
+ }
327
+ ${fragmentAst}
328
+ `;
329
+
330
+ const ast = gql`
331
+ {
332
+ user(id: 5) {
333
+ ...SecondUserFragment
334
+ }
335
+ }
336
+ ${secondFragmentAst}
337
+ `;
338
+
339
+ assert.deepEqual(ast, gql`
340
+ {
341
+ user(id: 5) {
342
+ ...SecondUserFragment
343
+ }
344
+ }
345
+ fragment SecondUserFragment on User {
346
+ ...UserFragment
347
+ }
348
+ fragment UserFragment on User {
349
+ firstName
350
+ lastName
351
+ }
352
+ `);
353
+ });
354
+
355
+ describe('fragment warnings', () => {
356
+ let warnings = [];
357
+ const oldConsoleWarn = console.warn;
358
+ beforeEach(() => {
359
+ gql.resetCaches();
360
+ warnings = [];
361
+ console.warn = (w: string) => warnings.push(w);
362
+ });
363
+ afterEach(() => {
364
+ console.warn = oldConsoleWarn;
365
+ });
366
+
367
+ it('warns if you use the same fragment name for different fragments', () => {
368
+ const frag1 = gql`fragment TestSame on Bar { fieldOne }`;
369
+ const frag2 = gql`fragment TestSame on Bar { fieldTwo }`;
370
+
371
+ assert.isFalse(frag1 === frag2);
372
+ assert.equal(warnings.length, 1);
373
+ });
374
+
375
+ it('does not warn if you use the same fragment name for the same fragment', () => {
376
+ const frag1 = gql`fragment TestDifferent on Bar { fieldOne }`;
377
+ const frag2 = gql`fragment TestDifferent on Bar { fieldOne }`;
378
+
379
+ assert.isTrue(frag1 === frag2);
380
+ assert.equal(warnings.length, 0);
381
+ });
382
+
383
+ it('does not warn if you use the same embedded fragment in two different queries', () => {
384
+ const frag1 = gql`fragment TestEmbedded on Bar { field }`;
385
+ const query1 = gql`{ bar { fieldOne ...TestEmbedded } } ${frag1}`;
386
+ const query2 = gql`{ bar { fieldTwo ...TestEmbedded } } ${frag1}`;
387
+
388
+ assert.isFalse(query1 === query2);
389
+ assert.equal(warnings.length, 0);
390
+ });
391
+
392
+ it('does not warn if you use the same fragment name for embedded and non-embedded fragments', () => {
393
+ const frag1 = gql`fragment TestEmbeddedTwo on Bar { field }`;
394
+ gql`{ bar { ...TestEmbedded } } ${frag1}`;
395
+ gql`{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }`;
396
+
397
+ assert.equal(warnings.length, 0);
398
+ });
399
+ });
400
+
401
+ describe('unique fragments', () => {
402
+ beforeEach(() => {
403
+ gql.resetCaches();
404
+ });
405
+
406
+ it('strips duplicate fragments from the document', () => {
407
+ const frag1 = gql`fragment TestDuplicate on Bar { field }`;
408
+ const query1 = gql`{ bar { fieldOne ...TestDuplicate } } ${frag1} ${frag1}`;
409
+ const query2 = gql`{ bar { fieldOne ...TestDuplicate } } ${frag1}`;
410
+
411
+ assert.equal(query1.definitions.length, 2);
412
+ assert.equal(query1.definitions[1].kind, 'FragmentDefinition');
413
+ // We don't test strict equality between the two queries because the source.body parsed from the
414
+ // document is not the same, but the set of definitions should be.
415
+ assert.deepEqual(query1.definitions, query2.definitions);
416
+ });
417
+
418
+ it('ignores duplicate fragments from second-level imports when using the webpack loader', () => {
419
+ // take a require function and a query string, use the webpack loader to process it
420
+ const load = (
421
+ require: (path: string) => DocumentNode | null,
422
+ query: string,
423
+ ): DocumentNode | null => {
424
+ const jsSource = loader.call({ cacheable() {} }, query);
425
+ const module = { exports: Object.create(null) };
426
+ Function("require,module", jsSource)(require, module);
427
+ return module.exports;
428
+ }
429
+
430
+ const test_require = (path: string) => {
431
+ switch (path) {
432
+ case './friends.graphql':
433
+ return load(test_require, [
434
+ '#import "./person.graphql"',
435
+ 'fragment friends on Hero { friends { ...person } }',
436
+ ].join('\n'));
437
+ case './enemies.graphql':
438
+ return load(test_require, [
439
+ '#import "./person.graphql"',
440
+ 'fragment enemies on Hero { enemies { ...person } }',
441
+ ].join('\n'));
442
+ case './person.graphql':
443
+ return load(test_require, 'fragment person on Person { name }\n');
444
+ default:
445
+ return null;
446
+ };
447
+ };
448
+
449
+ const result = load(test_require, [
450
+ '#import "./friends.graphql"',
451
+ '#import "./enemies.graphql"',
452
+ 'query { hero { ...friends ...enemies } }',
453
+ ].join('\n'))!;
454
+
455
+ assert.equal(result.kind, 'Document');
456
+ assert.equal(result.definitions.length, 4, 'after deduplication, only 4 fragments should remain');
457
+ assert.equal(result.definitions[0].kind, 'OperationDefinition');
458
+
459
+ // the rest of the definitions should be fragments and contain one of
460
+ // each: "friends", "enemies", "person". Order does not matter
461
+ const fragments = result.definitions.slice(1) as FragmentDefinitionNode[];
462
+ assert(fragments.every(fragment => fragment.kind === 'FragmentDefinition'))
463
+ assert(fragments.some(fragment => fragment.name.value === 'friends'))
464
+ assert(fragments.some(fragment => fragment.name.value === 'enemies'))
465
+ assert(fragments.some(fragment => fragment.name.value === 'person'))
466
+ });
467
+ });
468
+
469
+ // How to make this work?
470
+ // it.only('can reference a fragment passed as a document via shorthand', () => {
471
+ // const ast = gql`
472
+ // {
473
+ // user(id: 5) {
474
+ // ...${userFragmentDocument}
475
+ // }
476
+ // }
477
+ // `;
478
+ //
479
+ // assert.deepEqual(ast, gql`
480
+ // {
481
+ // user(id: 5) {
482
+ // ...UserFragment
483
+ // }
484
+ // }
485
+ // fragment UserFragment on User {
486
+ // firstName
487
+ // lastName
488
+ // }
489
+ // `);
490
+ // });
491
+ });