graphql-tag 2.10.3 → 2.12.1

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