1 | import { __makeTemplateObject } from "tslib";
|
2 | import 'source-map-support/register';
|
3 | import { assert } from 'chai';
|
4 | import gql from './index';
|
5 | var loader = require('../loader');
|
6 | describe('gql', function () {
|
7 | it('parses queries', function () {
|
8 | assert.equal(gql(templateObject_1 || (templateObject_1 = __makeTemplateObject(["{ testQuery }"], ["{ testQuery }"]))).kind, 'Document');
|
9 | });
|
10 | it('parses queries when called as a function', function () {
|
11 | assert.equal(gql('{ testQuery }').kind, 'Document');
|
12 | });
|
13 | it('parses queries with weird substitutions', function () {
|
14 | var obj = Object.create(null);
|
15 | assert.equal(gql(templateObject_2 || (templateObject_2 = __makeTemplateObject(["{ field(input: \"", "\") }"], ["{ field(input: \"", "\") }"])), obj.missing).kind, 'Document');
|
16 | assert.equal(gql(templateObject_3 || (templateObject_3 = __makeTemplateObject(["{ field(input: \"", "\") }"], ["{ field(input: \"", "\") }"])), null).kind, 'Document');
|
17 | assert.equal(gql(templateObject_4 || (templateObject_4 = __makeTemplateObject(["{ field(input: \"", "\") }"], ["{ field(input: \"", "\") }"])), 0).kind, 'Document');
|
18 | });
|
19 | it('allows interpolation of documents generated by the webpack loader', function () {
|
20 | var sameFragment = "fragment SomeFragmentName on SomeType { someField }";
|
21 | var jsSource = loader.call({ cacheable: function () { } }, sameFragment);
|
22 | var module = { exports: Object.create(null) };
|
23 | Function("module", jsSource)(module);
|
24 | var document = gql(templateObject_5 || (templateObject_5 = __makeTemplateObject(["query { ...SomeFragmentName } ", ""], ["query { ...SomeFragmentName } ", ""])), module.exports);
|
25 | assert.equal(document.kind, 'Document');
|
26 | assert.equal(document.definitions.length, 2);
|
27 | assert.equal(document.definitions[0].kind, 'OperationDefinition');
|
28 | assert.equal(document.definitions[1].kind, 'FragmentDefinition');
|
29 | });
|
30 | it('parses queries through webpack loader', function () {
|
31 | var jsSource = loader.call({ cacheable: function () { } }, '{ testQuery }');
|
32 | var module = { exports: Object.create(null) };
|
33 | Function("module", jsSource)(module);
|
34 | assert.equal(module.exports.kind, 'Document');
|
35 | });
|
36 | it('parses single query through webpack loader', function () {
|
37 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n ");
|
38 | var module = { exports: Object.create(null) };
|
39 | Function("module", jsSource)(module);
|
40 | assert.equal(module.exports.kind, 'Document');
|
41 | assert.exists(module.exports.Q1);
|
42 | assert.equal(module.exports.Q1.kind, 'Document');
|
43 | assert.equal(module.exports.Q1.definitions.length, 1);
|
44 | });
|
45 | it('parses single query and exports as default', function () {
|
46 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n ");
|
47 | var module = { exports: Object.create(null) };
|
48 | Function("module", jsSource)(module);
|
49 | assert.deepEqual(module.exports.definitions, module.exports.Q1.definitions);
|
50 | });
|
51 | it('parses multiple queries through webpack loader', function () {
|
52 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n query Q2 { testQuery2 }\n ");
|
53 | var module = { exports: Object.create(null) };
|
54 | Function("module", jsSource)(module);
|
55 | assert.exists(module.exports.Q1);
|
56 | assert.exists(module.exports.Q2);
|
57 | assert.equal(module.exports.Q1.kind, 'Document');
|
58 | assert.equal(module.exports.Q2.kind, 'Document');
|
59 | assert.equal(module.exports.Q1.definitions.length, 1);
|
60 | assert.equal(module.exports.Q2.definitions.length, 1);
|
61 | });
|
62 | it('parses fragments with variable definitions', function () {
|
63 | gql.enableExperimentalFragmentVariables();
|
64 | var parsed = gql(templateObject_6 || (templateObject_6 = __makeTemplateObject(["fragment A ($arg: String!) on Type { testQuery }"], ["fragment A ($arg: String!) on Type { testQuery }"])));
|
65 | assert.equal(parsed.kind, 'Document');
|
66 | assert.exists(parsed.definitions[0].variableDefinitions);
|
67 | gql.disableExperimentalFragmentVariables();
|
68 | });
|
69 | it('does not nest queries needlessly in named exports', function () {
|
70 | var jsSource = loader.call({ cacheable: function () { } }, "\n query Q1 { testQuery }\n query Q2 { testQuery2 }\n query Q3 { test Query3 }\n ");
|
71 | var module = { exports: Object.create(null) };
|
72 | Function("module", jsSource)(module);
|
73 | assert.notExists(module.exports.Q2.Q1);
|
74 | assert.notExists(module.exports.Q3.Q1);
|
75 | assert.notExists(module.exports.Q3.Q2);
|
76 | });
|
77 | it('tracks fragment dependencies from multiple queries through webpack loader', function () {
|
78 | 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 ");
|
79 | var module = { exports: Object.create(null) };
|
80 | Function("module", jsSource)(module);
|
81 | assert.exists(module.exports.Q1);
|
82 | assert.exists(module.exports.Q2);
|
83 | assert.exists(module.exports.Q3);
|
84 | var Q1 = module.exports.Q1.definitions;
|
85 | var Q2 = module.exports.Q2.definitions;
|
86 | var Q3 = module.exports.Q3.definitions;
|
87 | assert.equal(Q1.length, 2);
|
88 | assert.equal(Q1[0].name.value, 'Q1');
|
89 | assert.equal(Q1[1].name.value, 'F1');
|
90 | assert.equal(Q2.length, 2);
|
91 | assert.equal(Q2[0].name.value, 'Q2');
|
92 | assert.equal(Q2[1].name.value, 'F2');
|
93 | assert.equal(Q3.length, 3);
|
94 | assert.equal(Q3[0].name.value, 'Q3');
|
95 | assert.equal(Q3[1].name.value, 'F1');
|
96 | assert.equal(Q3[2].name.value, 'F2');
|
97 | var F1 = module.exports.F1.definitions;
|
98 | var F2 = module.exports.F2.definitions;
|
99 | var F3 = module.exports.F3.definitions;
|
100 | assert.equal(F1.length, 1);
|
101 | assert.equal(F1[0].name.value, 'F1');
|
102 | assert.equal(F2.length, 1);
|
103 | assert.equal(F2[0].name.value, 'F2');
|
104 | assert.equal(F3.length, 1);
|
105 | assert.equal(F3[0].name.value, 'F3');
|
106 | });
|
107 | it('tracks fragment dependencies across nested fragments', function () {
|
108 | 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 ");
|
109 | var module = { exports: Object.create(null) };
|
110 | Function("module", jsSource)(module);
|
111 | assert.exists(module.exports.Q1);
|
112 | assert.exists(module.exports.Q2);
|
113 | var Q1 = module.exports.Q1.definitions;
|
114 | var Q2 = module.exports.Q2.definitions;
|
115 | assert.equal(Q1.length, 4);
|
116 | assert.equal(Q1[0].name.value, 'Q1');
|
117 | assert.equal(Q1[1].name.value, 'F33');
|
118 | assert.equal(Q1[2].name.value, 'F22');
|
119 | assert.equal(Q1[3].name.value, 'F11');
|
120 | assert.equal(Q2.length, 1);
|
121 | var F11 = module.exports.F11.definitions;
|
122 | var F22 = module.exports.F22.definitions;
|
123 | var F33 = module.exports.F33.definitions;
|
124 | assert.equal(F11.length, 1);
|
125 | assert.equal(F11[0].name.value, 'F11');
|
126 | assert.equal(F22.length, 2);
|
127 | assert.equal(F22[0].name.value, 'F22');
|
128 | assert.equal(F22[1].name.value, 'F11');
|
129 | assert.equal(F33.length, 3);
|
130 | assert.equal(F33[0].name.value, 'F33');
|
131 | assert.equal(F33[1].name.value, 'F22');
|
132 | assert.equal(F33[2].name.value, 'F11');
|
133 | });
|
134 | it('correctly imports other files through the webpack loader', function () {
|
135 | var query = "#import \"./fragment_definition.graphql\"\n query {\n author {\n ...authorDetails\n }\n }";
|
136 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
137 | var module = { exports: Object.create(null) };
|
138 | var require = function (path) {
|
139 | assert.equal(path, './fragment_definition.graphql');
|
140 | return gql(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n fragment authorDetails on Author {\n firstName\n lastName\n }"], ["\n fragment authorDetails on Author {\n firstName\n lastName\n }"])));
|
141 | };
|
142 | Function("module,require", jsSource)(module, require);
|
143 | assert.equal(module.exports.kind, 'Document');
|
144 | var definitions = module.exports.definitions;
|
145 | assert.equal(definitions.length, 2);
|
146 | assert.equal(definitions[0].kind, 'OperationDefinition');
|
147 | assert.equal(definitions[1].kind, 'FragmentDefinition');
|
148 | });
|
149 | it('tracks fragment dependencies across fragments loaded via the webpack loader', function () {
|
150 | 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 ";
|
151 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
152 | var module = { exports: Object.create(null) };
|
153 | var require = function (path) {
|
154 | assert.equal(path, './fragment_definition.graphql');
|
155 | return gql(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n fragment F222 on F {\n f1\n f2\n }"], ["\n fragment F222 on F {\n f1\n f2\n }"])));
|
156 | };
|
157 | Function("module,require", jsSource)(module, require);
|
158 | assert.exists(module.exports.Q1);
|
159 | assert.exists(module.exports.Q2);
|
160 | var Q1 = module.exports.Q1.definitions;
|
161 | var Q2 = module.exports.Q2.definitions;
|
162 | assert.equal(Q1.length, 3);
|
163 | assert.equal(Q1[0].name.value, 'Q1');
|
164 | assert.equal(Q1[1].name.value, 'F111');
|
165 | assert.equal(Q1[2].name.value, 'F222');
|
166 | assert.equal(Q2.length, 1);
|
167 | });
|
168 | it('does not complain when presented with normal comments', function (done) {
|
169 | assert.doesNotThrow(function () {
|
170 | var query = "#normal comment\n query {\n author {\n ...authorDetails\n }\n }";
|
171 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
172 | var module = { exports: Object.create(null) };
|
173 | Function("module", jsSource)(module);
|
174 | assert.equal(module.exports.kind, 'Document');
|
175 | done();
|
176 | });
|
177 | });
|
178 | it('returns the same object for the same query', function () {
|
179 | assert.isTrue(gql(templateObject_9 || (templateObject_9 = __makeTemplateObject(["{ sameQuery }"], ["{ sameQuery }"]))) === gql(templateObject_10 || (templateObject_10 = __makeTemplateObject(["{ sameQuery }"], ["{ sameQuery }"]))));
|
180 | });
|
181 | it('returns the same object for the same query, even with whitespace differences', function () {
|
182 | assert.isTrue(gql(templateObject_11 || (templateObject_11 = __makeTemplateObject(["{ sameQuery }"], ["{ sameQuery }"]))) === gql(templateObject_12 || (templateObject_12 = __makeTemplateObject([" { sameQuery, }"], [" { sameQuery, }"]))));
|
183 | });
|
184 | var fragmentAst = gql(templateObject_13 || (templateObject_13 = __makeTemplateObject(["\n fragment UserFragment on User {\n firstName\n lastName\n }\n"], ["\n fragment UserFragment on User {\n firstName\n lastName\n }\n"])));
|
185 | it('returns the same object for the same fragment', function () {
|
186 | assert.isTrue(gql(templateObject_14 || (templateObject_14 = __makeTemplateObject(["fragment same on Same { sameQuery }"], ["fragment same on Same { sameQuery }"]))) === gql(templateObject_15 || (templateObject_15 = __makeTemplateObject(["fragment same on Same { sameQuery }"], ["fragment same on Same { sameQuery }"]))));
|
187 | });
|
188 | it('returns the same object for the same document with substitution', function () {
|
189 | assert.isTrue(gql(templateObject_16 || (templateObject_16 = __makeTemplateObject(["{ ...UserFragment } ", ""], ["{ ...UserFragment } ", ""])), fragmentAst) === gql(templateObject_17 || (templateObject_17 = __makeTemplateObject(["{ ...UserFragment } ", ""], ["{ ...UserFragment } ", ""])), fragmentAst));
|
190 | });
|
191 | it('can reference a fragment that references as fragment', function () {
|
192 | var secondFragmentAst = gql(templateObject_18 || (templateObject_18 = __makeTemplateObject(["\n fragment SecondUserFragment on User {\n ...UserFragment\n }\n ", "\n "], ["\n fragment SecondUserFragment on User {\n ...UserFragment\n }\n ", "\n "])), fragmentAst);
|
193 | var ast = gql(templateObject_19 || (templateObject_19 = __makeTemplateObject(["\n {\n user(id: 5) {\n ...SecondUserFragment\n }\n }\n ", "\n "], ["\n {\n user(id: 5) {\n ...SecondUserFragment\n }\n }\n ", "\n "])), secondFragmentAst);
|
194 | assert.deepEqual(ast, gql(templateObject_20 || (templateObject_20 = __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 "]))));
|
195 | });
|
196 | describe('fragment warnings', function () {
|
197 | var warnings = [];
|
198 | var oldConsoleWarn = console.warn;
|
199 | beforeEach(function () {
|
200 | gql.resetCaches();
|
201 | warnings = [];
|
202 | console.warn = function (w) { return warnings.push(w); };
|
203 | });
|
204 | afterEach(function () {
|
205 | console.warn = oldConsoleWarn;
|
206 | });
|
207 | it('warns if you use the same fragment name for different fragments', function () {
|
208 | var frag1 = gql(templateObject_21 || (templateObject_21 = __makeTemplateObject(["fragment TestSame on Bar { fieldOne }"], ["fragment TestSame on Bar { fieldOne }"])));
|
209 | var frag2 = gql(templateObject_22 || (templateObject_22 = __makeTemplateObject(["fragment TestSame on Bar { fieldTwo }"], ["fragment TestSame on Bar { fieldTwo }"])));
|
210 | assert.isFalse(frag1 === frag2);
|
211 | assert.equal(warnings.length, 1);
|
212 | });
|
213 | it('does not warn if you use the same fragment name for the same fragment', function () {
|
214 | var frag1 = gql(templateObject_23 || (templateObject_23 = __makeTemplateObject(["fragment TestDifferent on Bar { fieldOne }"], ["fragment TestDifferent on Bar { fieldOne }"])));
|
215 | var frag2 = gql(templateObject_24 || (templateObject_24 = __makeTemplateObject(["fragment TestDifferent on Bar { fieldOne }"], ["fragment TestDifferent on Bar { fieldOne }"])));
|
216 | assert.isTrue(frag1 === frag2);
|
217 | assert.equal(warnings.length, 0);
|
218 | });
|
219 | it('does not warn if you use the same embedded fragment in two different queries', function () {
|
220 | var frag1 = gql(templateObject_25 || (templateObject_25 = __makeTemplateObject(["fragment TestEmbedded on Bar { field }"], ["fragment TestEmbedded on Bar { field }"])));
|
221 | var query1 = gql(templateObject_26 || (templateObject_26 = __makeTemplateObject(["{ bar { fieldOne ...TestEmbedded } } ", ""], ["{ bar { fieldOne ...TestEmbedded } } ", ""])), frag1);
|
222 | var query2 = gql(templateObject_27 || (templateObject_27 = __makeTemplateObject(["{ bar { fieldTwo ...TestEmbedded } } ", ""], ["{ bar { fieldTwo ...TestEmbedded } } ", ""])), frag1);
|
223 | assert.isFalse(query1 === query2);
|
224 | assert.equal(warnings.length, 0);
|
225 | });
|
226 | it('does not warn if you use the same fragment name for embedded and non-embedded fragments', function () {
|
227 | var frag1 = gql(templateObject_28 || (templateObject_28 = __makeTemplateObject(["fragment TestEmbeddedTwo on Bar { field }"], ["fragment TestEmbeddedTwo on Bar { field }"])));
|
228 | gql(templateObject_29 || (templateObject_29 = __makeTemplateObject(["{ bar { ...TestEmbedded } } ", ""], ["{ bar { ...TestEmbedded } } ", ""])), frag1);
|
229 | gql(templateObject_30 || (templateObject_30 = __makeTemplateObject(["{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }"], ["{ bar { ...TestEmbedded } } fragment TestEmbeddedTwo on Bar { field }"])));
|
230 | assert.equal(warnings.length, 0);
|
231 | });
|
232 | });
|
233 | describe('unique fragments', function () {
|
234 | beforeEach(function () {
|
235 | gql.resetCaches();
|
236 | });
|
237 | it('strips duplicate fragments from the document', function () {
|
238 | var frag1 = gql(templateObject_31 || (templateObject_31 = __makeTemplateObject(["fragment TestDuplicate on Bar { field }"], ["fragment TestDuplicate on Bar { field }"])));
|
239 | var query1 = gql(templateObject_32 || (templateObject_32 = __makeTemplateObject(["{ bar { fieldOne ...TestDuplicate } } ", " ", ""], ["{ bar { fieldOne ...TestDuplicate } } ", " ", ""])), frag1, frag1);
|
240 | var query2 = gql(templateObject_33 || (templateObject_33 = __makeTemplateObject(["{ bar { fieldOne ...TestDuplicate } } ", ""], ["{ bar { fieldOne ...TestDuplicate } } ", ""])), frag1);
|
241 | assert.equal(query1.definitions.length, 2);
|
242 | assert.equal(query1.definitions[1].kind, 'FragmentDefinition');
|
243 | assert.deepEqual(query1.definitions, query2.definitions);
|
244 | });
|
245 | it('ignores duplicate fragments from second-level imports when using the webpack loader', function () {
|
246 | var load = function (require, query) {
|
247 | var jsSource = loader.call({ cacheable: function () { } }, query);
|
248 | var module = { exports: Object.create(null) };
|
249 | Function("require,module", jsSource)(require, module);
|
250 | return module.exports;
|
251 | };
|
252 | var test_require = function (path) {
|
253 | switch (path) {
|
254 | case './friends.graphql':
|
255 | return load(test_require, [
|
256 | '#import "./person.graphql"',
|
257 | 'fragment friends on Hero { friends { ...person } }',
|
258 | ].join('\n'));
|
259 | case './enemies.graphql':
|
260 | return load(test_require, [
|
261 | '#import "./person.graphql"',
|
262 | 'fragment enemies on Hero { enemies { ...person } }',
|
263 | ].join('\n'));
|
264 | case './person.graphql':
|
265 | return load(test_require, 'fragment person on Person { name }\n');
|
266 | default:
|
267 | return null;
|
268 | }
|
269 | ;
|
270 | };
|
271 | var result = load(test_require, [
|
272 | '#import "./friends.graphql"',
|
273 | '#import "./enemies.graphql"',
|
274 | 'query { hero { ...friends ...enemies } }',
|
275 | ].join('\n'));
|
276 | assert.equal(result.kind, 'Document');
|
277 | assert.equal(result.definitions.length, 4, 'after deduplication, only 4 fragments should remain');
|
278 | assert.equal(result.definitions[0].kind, 'OperationDefinition');
|
279 | var fragments = result.definitions.slice(1);
|
280 | assert(fragments.every(function (fragment) { return fragment.kind === 'FragmentDefinition'; }));
|
281 | assert(fragments.some(function (fragment) { return fragment.name.value === 'friends'; }));
|
282 | assert(fragments.some(function (fragment) { return fragment.name.value === 'enemies'; }));
|
283 | assert(fragments.some(function (fragment) { return fragment.name.value === 'person'; }));
|
284 | });
|
285 | });
|
286 | });
|
287 | 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;
|
288 |
|
\ | No newline at end of file |