graphql-tag 2.11.0 → 2.12.0

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.
@@ -1,6 +1,10 @@
1
- const gql = require('../src');
1
+ import 'source-map-support/register';
2
+
3
+ import { assert } from 'chai';
4
+ import type { DocumentNode, FragmentDefinitionNode } from 'graphql';
5
+
6
+ import gql from './index';
2
7
  const loader = require('../loader');
3
- const assert = require('chai').assert;
4
8
 
5
9
  describe('gql', () => {
6
10
  it('parses queries', () => {
@@ -12,7 +16,7 @@ describe('gql', () => {
12
16
  });
13
17
 
14
18
  it('parses queries with weird substitutions', () => {
15
- const obj = {};
19
+ const obj = Object.create(null);
16
20
  assert.equal(gql`{ field(input: "${obj.missing}") }`.kind, 'Document');
17
21
  assert.equal(gql`{ field(input: "${null}") }`.kind, 'Document');
18
22
  assert.equal(gql`{ field(input: "${0}") }`.kind, 'Document');
@@ -23,10 +27,11 @@ describe('gql', () => {
23
27
 
24
28
  const jsSource = loader.call(
25
29
  { cacheable() {} },
26
- "fragment SomeFragmentName on SomeType { someField }"
30
+ sameFragment,
27
31
  );
28
- const module = { exports: undefined };
29
- eval(jsSource);
32
+ const module = { exports: Object.create(null) };
33
+
34
+ Function("module", jsSource)(module);
30
35
 
31
36
  const document = gql`query { ...SomeFragmentName } ${module.exports}`;
32
37
  assert.equal(document.kind, 'Document');
@@ -37,8 +42,8 @@ describe('gql', () => {
37
42
 
38
43
  it('parses queries through webpack loader', () => {
39
44
  const jsSource = loader.call({ cacheable() {} }, '{ testQuery }');
40
- const module = { exports: undefined };
41
- eval(jsSource);
45
+ const module = { exports: Object.create(null) };
46
+ Function("module", jsSource)(module);
42
47
  assert.equal(module.exports.kind, 'Document');
43
48
  });
44
49
 
@@ -46,8 +51,8 @@ describe('gql', () => {
46
51
  const jsSource = loader.call({ cacheable() {} }, `
47
52
  query Q1 { testQuery }
48
53
  `);
49
- const module = { exports: undefined };
50
- eval(jsSource);
54
+ const module = { exports: Object.create(null) };
55
+ Function("module", jsSource)(module);
51
56
 
52
57
  assert.equal(module.exports.kind, 'Document');
53
58
  assert.exists(module.exports.Q1);
@@ -59,9 +64,8 @@ describe('gql', () => {
59
64
  const jsSource = loader.call({ cacheable() {} }, `
60
65
  query Q1 { testQuery }
61
66
  `);
62
- const module = { exports: undefined };
63
- eval(jsSource);
64
-
67
+ const module = { exports: Object.create(null) };
68
+ Function("module", jsSource)(module);
65
69
  assert.deepEqual(module.exports.definitions, module.exports.Q1.definitions);
66
70
  });
67
71
 
@@ -70,8 +74,8 @@ describe('gql', () => {
70
74
  query Q1 { testQuery }
71
75
  query Q2 { testQuery2 }
72
76
  `);
73
- const module = { exports: undefined };
74
- eval(jsSource);
77
+ const module = { exports: Object.create(null) };
78
+ Function("module", jsSource)(module);
75
79
 
76
80
  assert.exists(module.exports.Q1);
77
81
  assert.exists(module.exports.Q2);
@@ -84,7 +88,7 @@ describe('gql', () => {
84
88
  it('parses fragments with variable definitions', () => {
85
89
  gql.enableExperimentalFragmentVariables();
86
90
 
87
- const parsed = gql`fragment A ($arg: String!) on Type { testQuery }`;
91
+ const parsed: any = gql`fragment A ($arg: String!) on Type { testQuery }`;
88
92
  assert.equal(parsed.kind, 'Document');
89
93
  assert.exists(parsed.definitions[0].variableDefinitions);
90
94
 
@@ -98,8 +102,8 @@ describe('gql', () => {
98
102
  query Q2 { testQuery2 }
99
103
  query Q3 { test Query3 }
100
104
  `);
101
- const module = { exports: undefined };
102
- eval(jsSource);
105
+ const module = { exports: Object.create(null) };
106
+ Function("module", jsSource)(module);
103
107
 
104
108
  assert.notExists(module.exports.Q2.Q1);
105
109
  assert.notExists(module.exports.Q3.Q1);
@@ -118,8 +122,8 @@ describe('gql', () => {
118
122
  ...F2
119
123
  }
120
124
  `);
121
- const module = { exports: undefined };
122
- eval(jsSource);
125
+ const module = { exports: Object.create(null) };
126
+ Function("module", jsSource)(module);
123
127
 
124
128
  assert.exists(module.exports.Q1);
125
129
  assert.exists(module.exports.Q2);
@@ -164,8 +168,8 @@ describe('gql', () => {
164
168
  }
165
169
  `);
166
170
 
167
- const module = { exports: undefined };
168
- eval(jsSource);
171
+ const module = { exports: Object.create(null) };
172
+ Function("module", jsSource)(module);
169
173
 
170
174
  assert.exists(module.exports.Q1);
171
175
  assert.exists(module.exports.Q2);
@@ -190,9 +194,8 @@ describe('gql', () => {
190
194
  }
191
195
  }`;
192
196
  const jsSource = loader.call({ cacheable() {} }, query);
193
- const oldRequire = require;
194
- const module = { exports: undefined };
195
- const require = (path) => {
197
+ const module = { exports: Object.create(null) };
198
+ const require = (path: string) => {
196
199
  assert.equal(path, './fragment_definition.graphql');
197
200
  return gql`
198
201
  fragment authorDetails on Author {
@@ -200,7 +203,7 @@ describe('gql', () => {
200
203
  lastName
201
204
  }`;
202
205
  };
203
- eval(jsSource);
206
+ Function("module,require", jsSource)(module, require);
204
207
  assert.equal(module.exports.kind, 'Document');
205
208
  const definitions = module.exports.definitions;
206
209
  assert.equal(definitions.length, 2);
@@ -223,9 +226,8 @@ describe('gql', () => {
223
226
  }
224
227
  `;
225
228
  const jsSource = loader.call({ cacheable() {} }, query);
226
- const oldRequire = require;
227
- const module = { exports: undefined };
228
- const require = (path) => {
229
+ const module = { exports: Object.create(null) };
230
+ const require = (path: string) => {
229
231
  assert.equal(path, './fragment_definition.graphql');
230
232
  return gql`
231
233
  fragment F222 on F {
@@ -233,7 +235,7 @@ describe('gql', () => {
233
235
  f2
234
236
  }`;
235
237
  };
236
- eval(jsSource);
238
+ Function("module,require", jsSource)(module, require);
237
239
 
238
240
  assert.exists(module.exports.Q1);
239
241
  assert.exists(module.exports.Q2);
@@ -258,8 +260,8 @@ describe('gql', () => {
258
260
  }
259
261
  }`;
260
262
  const jsSource = loader.call({ cacheable() {} }, query);
261
- const module = { exports: undefined };
262
- eval(jsSource);
263
+ const module = { exports: Object.create(null) };
264
+ Function("module", jsSource)(module);
263
265
  assert.equal(module.exports.kind, 'Document');
264
266
  done();
265
267
  });
@@ -331,7 +333,7 @@ describe('gql', () => {
331
333
  beforeEach(() => {
332
334
  gql.resetCaches();
333
335
  warnings = [];
334
- console.warn = (w) => warnings.push(w);
336
+ console.warn = (w: string) => warnings.push(w);
335
337
  });
336
338
  afterEach(() => {
337
339
  console.warn = oldConsoleWarn;
@@ -364,8 +366,8 @@ describe('gql', () => {
364
366
 
365
367
  it('does not warn if you use the same fragment name for embedded and non-embedded fragments', () => {
366
368
  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
+ gql`{ bar { ...TestEmbedded } } ${frag1}`;
370
+ gql`{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }`;
369
371
 
370
372
  assert.equal(warnings.length, 0);
371
373
  });
@@ -390,14 +392,17 @@ describe('gql', () => {
390
392
 
391
393
  it('ignores duplicate fragments from second-level imports when using the webpack loader', () => {
392
394
  // take a require function and a query string, use the webpack loader to process it
393
- const load = (require, query) => {
395
+ const load = (
396
+ require: (path: string) => DocumentNode | null,
397
+ query: string,
398
+ ): DocumentNode | null => {
394
399
  const jsSource = loader.call({ cacheable() {} }, query);
395
- const module = { exports: undefined };
396
- eval(jsSource);
400
+ const module = { exports: Object.create(null) };
401
+ Function("require,module", jsSource)(require, module);
397
402
  return module.exports;
398
403
  }
399
404
 
400
- const test_require = (path) => {
405
+ const test_require = (path: string) => {
401
406
  switch (path) {
402
407
  case './friends.graphql':
403
408
  return load(test_require, [
@@ -420,7 +425,7 @@ describe('gql', () => {
420
425
  '#import "./friends.graphql"',
421
426
  '#import "./enemies.graphql"',
422
427
  'query { hero { ...friends ...enemies } }',
423
- ].join('\n'));
428
+ ].join('\n'))!;
424
429
 
425
430
  assert.equal(result.kind, 'Document');
426
431
  assert.equal(result.definitions.length, 4, 'after deduplication, only 4 fragments should remain');
@@ -428,7 +433,7 @@ describe('gql', () => {
428
433
 
429
434
  // the rest of the definitions should be fragments and contain one of
430
435
  // each: "friends", "enemies", "person". Order does not matter
431
- const fragments = result.definitions.slice(1)
436
+ const fragments = result.definitions.slice(1) as FragmentDefinitionNode[];
432
437
  assert(fragments.every(fragment => fragment.kind === 'FragmentDefinition'))
433
438
  assert(fragments.some(fragment => fragment.name.value === 'friends'))
434
439
  assert(fragments.some(fragment => fragment.name.value === 'enemies'))
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./lib",
4
+ "noImplicitAny": true,
5
+ "strictNullChecks": true,
6
+ "noUnusedParameters": false,
7
+ "noUnusedLocals": true,
8
+ "skipLibCheck": true,
9
+ "moduleResolution": "node",
10
+ "importHelpers": true,
11
+ "removeComments": true,
12
+ "sourceMap": true,
13
+ "declaration": true,
14
+ "declarationMap": true,
15
+ "module": "es2015",
16
+ "esModuleInterop": true,
17
+ "lib": ["es2015", "dom"],
18
+ "types": ["node", "chai", "mocha"]
19
+ },
20
+ "include": ["src/**/*.ts"]
21
+ }
package/index.d.ts DELETED
@@ -1,20 +0,0 @@
1
- declare module "graphql-tag" {
2
- function gql(
3
- literals: ReadonlyArray<string> | Readonly<string>,
4
- ...placeholders: any[]
5
- ): import("graphql").DocumentNode;
6
-
7
- namespace gql {
8
- function resetCaches(): void;
9
- function disableFragmentWarnings(): void;
10
- function enableExperimentalFragmentVariables(): void;
11
- function disableExperimentalFragmentVariables(): void;
12
- }
13
-
14
- export default gql;
15
-
16
- export function resetCaches(): void;
17
- export function disableFragmentWarnings(): void;
18
- export function enableExperimentalFragmentVariables(): void;
19
- export function disableExperimentalFragmentVariables(): void;
20
- }
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/test/mocha.opts DELETED
@@ -1 +0,0 @@
1
- --require babel-register