UNPKG

71.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var lodash_1 = require("lodash");
5var graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag"));
6var apollo_utilities_1 = require("apollo-utilities");
7var writeToStore_1 = require("../writeToStore");
8var objectCache_1 = require("../objectCache");
9var __1 = require("../");
10function withWarning(func, regex) {
11 var message = null;
12 var oldWarn = console.warn;
13 console.warn = function (m) { return (message = m); };
14 return Promise.resolve(func()).then(function (val) {
15 expect(message).toMatch(regex);
16 console.warn = oldWarn;
17 return val;
18 });
19}
20exports.withWarning = withWarning;
21var getIdField = function (_a) {
22 var id = _a.id;
23 return id;
24};
25describe('writing to the store', function () {
26 var writer = new writeToStore_1.StoreWriter();
27 it('properly normalizes a trivial item', function () {
28 var query = graphql_tag_1.default(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n }\n "])));
29 var result = {
30 id: 'abcd',
31 stringField: 'This is a string!',
32 numberField: 5,
33 nullField: null,
34 };
35 expect(writer
36 .writeQueryToStore({
37 query: query,
38 result: lodash_1.cloneDeep(result),
39 })
40 .toObject()).toEqual({
41 ROOT_QUERY: result,
42 });
43 });
44 it('properly normalizes an aliased field', function () {
45 var query = graphql_tag_1.default(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n {\n id\n aliasedField: stringField\n numberField\n nullField\n }\n "], ["\n {\n id\n aliasedField: stringField\n numberField\n nullField\n }\n "])));
46 var result = {
47 id: 'abcd',
48 aliasedField: 'This is a string!',
49 numberField: 5,
50 nullField: null,
51 };
52 var normalized = writer.writeQueryToStore({
53 result: result,
54 query: query,
55 });
56 expect(normalized.toObject()).toEqual({
57 ROOT_QUERY: {
58 id: 'abcd',
59 stringField: 'This is a string!',
60 numberField: 5,
61 nullField: null,
62 },
63 });
64 });
65 it('properly normalizes a aliased fields with arguments', function () {
66 var query = graphql_tag_1.default(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n {\n id\n aliasedField1: stringField(arg: 1)\n aliasedField2: stringField(arg: 2)\n numberField\n nullField\n }\n "], ["\n {\n id\n aliasedField1: stringField(arg: 1)\n aliasedField2: stringField(arg: 2)\n numberField\n nullField\n }\n "])));
67 var result = {
68 id: 'abcd',
69 aliasedField1: 'The arg was 1!',
70 aliasedField2: 'The arg was 2!',
71 numberField: 5,
72 nullField: null,
73 };
74 var normalized = writer.writeQueryToStore({
75 result: result,
76 query: query,
77 });
78 expect(normalized.toObject()).toEqual({
79 ROOT_QUERY: {
80 id: 'abcd',
81 'stringField({"arg":1})': 'The arg was 1!',
82 'stringField({"arg":2})': 'The arg was 2!',
83 numberField: 5,
84 nullField: null,
85 },
86 });
87 });
88 it('properly normalizes a query with variables', function () {
89 var query = graphql_tag_1.default(templateObject_4 || (templateObject_4 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField(arg: $stringArg)\n numberField(intArg: $intArg, floatArg: $floatArg)\n nullField\n }\n "], ["\n {\n id\n stringField(arg: $stringArg)\n numberField(intArg: $intArg, floatArg: $floatArg)\n nullField\n }\n "])));
90 var variables = {
91 intArg: 5,
92 floatArg: 3.14,
93 stringArg: 'This is a string!',
94 };
95 var result = {
96 id: 'abcd',
97 stringField: 'Heyo',
98 numberField: 5,
99 nullField: null,
100 };
101 var normalized = writer.writeQueryToStore({
102 result: result,
103 query: query,
104 variables: variables,
105 });
106 expect(normalized.toObject()).toEqual({
107 ROOT_QUERY: {
108 id: 'abcd',
109 nullField: null,
110 'numberField({"floatArg":3.14,"intArg":5})': 5,
111 'stringField({"arg":"This is a string!"})': 'Heyo',
112 },
113 });
114 });
115 it('properly normalizes a query with default values', function () {
116 var query = graphql_tag_1.default(templateObject_5 || (templateObject_5 = tslib_1.__makeTemplateObject(["\n query someBigQuery(\n $stringArg: String = \"This is a default string!\"\n $intArg: Int\n $floatArg: Float\n ) {\n id\n stringField(arg: $stringArg)\n numberField(intArg: $intArg, floatArg: $floatArg)\n nullField\n }\n "], ["\n query someBigQuery(\n $stringArg: String = \"This is a default string!\"\n $intArg: Int\n $floatArg: Float\n ) {\n id\n stringField(arg: $stringArg)\n numberField(intArg: $intArg, floatArg: $floatArg)\n nullField\n }\n "])));
117 var variables = {
118 intArg: 5,
119 floatArg: 3.14,
120 };
121 var result = {
122 id: 'abcd',
123 stringField: 'Heyo',
124 numberField: 5,
125 nullField: null,
126 };
127 var normalized = writer.writeQueryToStore({
128 result: result,
129 query: query,
130 variables: variables,
131 });
132 expect(normalized.toObject()).toEqual({
133 ROOT_QUERY: {
134 id: 'abcd',
135 nullField: null,
136 'numberField({"floatArg":3.14,"intArg":5})': 5,
137 'stringField({"arg":"This is a default string!"})': 'Heyo',
138 },
139 });
140 });
141 it('properly normalizes a query with custom directives', function () {
142 var query = graphql_tag_1.default(templateObject_6 || (templateObject_6 = tslib_1.__makeTemplateObject(["\n query {\n id\n firstName @include(if: true)\n lastName @upperCase\n birthDate @dateFormat(format: \"DD-MM-YYYY\")\n }\n "], ["\n query {\n id\n firstName @include(if: true)\n lastName @upperCase\n birthDate @dateFormat(format: \"DD-MM-YYYY\")\n }\n "])));
143 var result = {
144 id: 'abcd',
145 firstName: 'James',
146 lastName: 'BOND',
147 birthDate: '20-05-1940',
148 };
149 var normalized = writer.writeQueryToStore({
150 result: result,
151 query: query,
152 });
153 expect(normalized.toObject()).toEqual({
154 ROOT_QUERY: {
155 id: 'abcd',
156 firstName: 'James',
157 'lastName@upperCase': 'BOND',
158 'birthDate@dateFormat({"format":"DD-MM-YYYY"})': '20-05-1940',
159 },
160 });
161 });
162 it('properly normalizes a nested object with an ID', function () {
163 var _a;
164 var query = graphql_tag_1.default(templateObject_7 || (templateObject_7 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedObj {\n id\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedObj {\n id\n stringField\n numberField\n nullField\n }\n }\n "])));
165 var result = {
166 id: 'abcd',
167 stringField: 'This is a string!',
168 numberField: 5,
169 nullField: null,
170 nestedObj: {
171 id: 'abcde',
172 stringField: 'This is a string too!',
173 numberField: 6,
174 nullField: null,
175 },
176 };
177 expect(writer
178 .writeQueryToStore({
179 query: query,
180 result: lodash_1.cloneDeep(result),
181 dataIdFromObject: getIdField,
182 })
183 .toObject()).toEqual((_a = {
184 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedObj')), {
185 nestedObj: {
186 type: 'id',
187 id: result.nestedObj.id,
188 generated: false,
189 },
190 })
191 },
192 _a[result.nestedObj.id] = result.nestedObj,
193 _a));
194 });
195 it('properly normalizes a nested object without an ID', function () {
196 var _a;
197 var query = graphql_tag_1.default(templateObject_8 || (templateObject_8 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedObj {\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedObj {\n stringField\n numberField\n nullField\n }\n }\n "])));
198 var result = {
199 id: 'abcd',
200 stringField: 'This is a string!',
201 numberField: 5,
202 nullField: null,
203 nestedObj: {
204 stringField: 'This is a string too!',
205 numberField: 6,
206 nullField: null,
207 },
208 };
209 expect(writer
210 .writeQueryToStore({
211 query: query,
212 result: lodash_1.cloneDeep(result),
213 })
214 .toObject()).toEqual((_a = {
215 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedObj')), {
216 nestedObj: {
217 type: 'id',
218 id: "$ROOT_QUERY.nestedObj",
219 generated: true,
220 },
221 })
222 },
223 _a["$ROOT_QUERY.nestedObj"] = result.nestedObj,
224 _a));
225 });
226 it('properly normalizes a nested object with arguments but without an ID', function () {
227 var _a;
228 var query = graphql_tag_1.default(templateObject_9 || (templateObject_9 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedObj(arg: \"val\") {\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedObj(arg: \"val\") {\n stringField\n numberField\n nullField\n }\n }\n "])));
229 var result = {
230 id: 'abcd',
231 stringField: 'This is a string!',
232 numberField: 5,
233 nullField: null,
234 nestedObj: {
235 stringField: 'This is a string too!',
236 numberField: 6,
237 nullField: null,
238 },
239 };
240 expect(writer
241 .writeQueryToStore({
242 query: query,
243 result: lodash_1.cloneDeep(result),
244 })
245 .toObject()).toEqual((_a = {
246 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedObj')), {
247 'nestedObj({"arg":"val"})': {
248 type: 'id',
249 id: "$ROOT_QUERY.nestedObj({\"arg\":\"val\"})",
250 generated: true,
251 },
252 })
253 },
254 _a["$ROOT_QUERY.nestedObj({\"arg\":\"val\"})"] = result.nestedObj,
255 _a));
256 });
257 it('properly normalizes a nested array with IDs', function () {
258 var _a;
259 var query = graphql_tag_1.default(templateObject_10 || (templateObject_10 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n id\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n id\n stringField\n numberField\n nullField\n }\n }\n "])));
260 var result = {
261 id: 'abcd',
262 stringField: 'This is a string!',
263 numberField: 5,
264 nullField: null,
265 nestedArray: [
266 {
267 id: 'abcde',
268 stringField: 'This is a string too!',
269 numberField: 6,
270 nullField: null,
271 },
272 {
273 id: 'abcdef',
274 stringField: 'This is a string also!',
275 numberField: 7,
276 nullField: null,
277 },
278 ],
279 };
280 expect(writer
281 .writeQueryToStore({
282 query: query,
283 result: lodash_1.cloneDeep(result),
284 dataIdFromObject: getIdField,
285 })
286 .toObject()).toEqual((_a = {
287 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedArray')), {
288 nestedArray: result.nestedArray.map(function (obj) { return ({
289 type: 'id',
290 id: obj.id,
291 generated: false,
292 }); }),
293 })
294 },
295 _a[result.nestedArray[0].id] = result.nestedArray[0],
296 _a[result.nestedArray[1].id] = result.nestedArray[1],
297 _a));
298 });
299 it('properly normalizes a nested array with IDs and a null', function () {
300 var _a;
301 var query = graphql_tag_1.default(templateObject_11 || (templateObject_11 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n id\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n id\n stringField\n numberField\n nullField\n }\n }\n "])));
302 var result = {
303 id: 'abcd',
304 stringField: 'This is a string!',
305 numberField: 5,
306 nullField: null,
307 nestedArray: [
308 {
309 id: 'abcde',
310 stringField: 'This is a string too!',
311 numberField: 6,
312 nullField: null,
313 },
314 null,
315 ],
316 };
317 expect(writer
318 .writeQueryToStore({
319 query: query,
320 result: lodash_1.cloneDeep(result),
321 dataIdFromObject: getIdField,
322 })
323 .toObject()).toEqual((_a = {
324 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedArray')), {
325 nestedArray: [
326 { type: 'id', id: result.nestedArray[0].id, generated: false },
327 null,
328 ],
329 })
330 },
331 _a[result.nestedArray[0].id] = result.nestedArray[0],
332 _a));
333 });
334 it('properly normalizes a nested array without IDs', function () {
335 var _a;
336 var query = graphql_tag_1.default(templateObject_12 || (templateObject_12 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n stringField\n numberField\n nullField\n }\n }\n "])));
337 var result = {
338 id: 'abcd',
339 stringField: 'This is a string!',
340 numberField: 5,
341 nullField: null,
342 nestedArray: [
343 {
344 stringField: 'This is a string too!',
345 numberField: 6,
346 nullField: null,
347 },
348 {
349 stringField: 'This is a string also!',
350 numberField: 7,
351 nullField: null,
352 },
353 ],
354 };
355 var normalized = writer.writeQueryToStore({
356 query: query,
357 result: lodash_1.cloneDeep(result),
358 });
359 expect(normalized.toObject()).toEqual((_a = {
360 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedArray')), {
361 nestedArray: [
362 { type: 'id', generated: true, id: "ROOT_QUERY.nestedArray.0" },
363 { type: 'id', generated: true, id: "ROOT_QUERY.nestedArray.1" },
364 ],
365 })
366 },
367 _a["ROOT_QUERY.nestedArray.0"] = result.nestedArray[0],
368 _a["ROOT_QUERY.nestedArray.1"] = result.nestedArray[1],
369 _a));
370 });
371 it('properly normalizes a nested array without IDs and a null item', function () {
372 var _a;
373 var query = graphql_tag_1.default(templateObject_13 || (templateObject_13 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedArray {\n stringField\n numberField\n nullField\n }\n }\n "])));
374 var result = {
375 id: 'abcd',
376 stringField: 'This is a string!',
377 numberField: 5,
378 nullField: null,
379 nestedArray: [
380 null,
381 {
382 stringField: 'This is a string also!',
383 numberField: 7,
384 nullField: null,
385 },
386 ],
387 };
388 var normalized = writer.writeQueryToStore({
389 query: query,
390 result: lodash_1.cloneDeep(result),
391 });
392 expect(normalized.toObject()).toEqual((_a = {
393 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedArray')), {
394 nestedArray: [
395 null,
396 { type: 'id', generated: true, id: "ROOT_QUERY.nestedArray.1" },
397 ],
398 })
399 },
400 _a["ROOT_QUERY.nestedArray.1"] = result.nestedArray[1],
401 _a));
402 });
403 it('properly normalizes an array of non-objects', function () {
404 var query = graphql_tag_1.default(templateObject_14 || (templateObject_14 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n simpleArray\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n simpleArray\n }\n "])));
405 var result = {
406 id: 'abcd',
407 stringField: 'This is a string!',
408 numberField: 5,
409 nullField: null,
410 simpleArray: ['one', 'two', 'three'],
411 };
412 var normalized = writer.writeQueryToStore({
413 query: query,
414 result: lodash_1.cloneDeep(result),
415 dataIdFromObject: getIdField,
416 });
417 expect(normalized.toObject()).toEqual({
418 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'simpleArray')), {
419 simpleArray: {
420 type: 'json',
421 json: [
422 result.simpleArray[0],
423 result.simpleArray[1],
424 result.simpleArray[2],
425 ],
426 },
427 }),
428 });
429 });
430 it('properly normalizes an array of non-objects with null', function () {
431 var query = graphql_tag_1.default(templateObject_15 || (templateObject_15 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n simpleArray\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n simpleArray\n }\n "])));
432 var result = {
433 id: 'abcd',
434 stringField: 'This is a string!',
435 numberField: 5,
436 nullField: null,
437 simpleArray: [null, 'two', 'three'],
438 };
439 var normalized = writer.writeQueryToStore({
440 query: query,
441 result: lodash_1.cloneDeep(result),
442 });
443 expect(normalized.toObject()).toEqual({
444 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'simpleArray')), {
445 simpleArray: {
446 type: 'json',
447 json: [
448 result.simpleArray[0],
449 result.simpleArray[1],
450 result.simpleArray[2],
451 ],
452 },
453 }),
454 });
455 });
456 it('properly normalizes an object occurring in different graphql paths twice', function () {
457 var query = graphql_tag_1.default(templateObject_16 || (templateObject_16 = tslib_1.__makeTemplateObject(["\n {\n id\n object1 {\n id\n stringField\n }\n object2 {\n id\n numberField\n }\n }\n "], ["\n {\n id\n object1 {\n id\n stringField\n }\n object2 {\n id\n numberField\n }\n }\n "])));
458 var result = {
459 id: 'a',
460 object1: {
461 id: 'aa',
462 stringField: 'string',
463 },
464 object2: {
465 id: 'aa',
466 numberField: 1,
467 },
468 };
469 var normalized = writer.writeQueryToStore({
470 query: query,
471 result: lodash_1.cloneDeep(result),
472 dataIdFromObject: getIdField,
473 });
474 expect(normalized.toObject()).toEqual({
475 ROOT_QUERY: {
476 id: 'a',
477 object1: {
478 type: 'id',
479 id: 'aa',
480 generated: false,
481 },
482 object2: {
483 type: 'id',
484 id: 'aa',
485 generated: false,
486 },
487 },
488 aa: {
489 id: 'aa',
490 stringField: 'string',
491 numberField: 1,
492 },
493 });
494 });
495 it('properly normalizes an object occurring in different graphql array paths twice', function () {
496 var query = graphql_tag_1.default(templateObject_17 || (templateObject_17 = tslib_1.__makeTemplateObject(["\n {\n id\n array1 {\n id\n stringField\n obj {\n id\n stringField\n }\n }\n array2 {\n id\n stringField\n obj {\n id\n numberField\n }\n }\n }\n "], ["\n {\n id\n array1 {\n id\n stringField\n obj {\n id\n stringField\n }\n }\n array2 {\n id\n stringField\n obj {\n id\n numberField\n }\n }\n }\n "])));
497 var result = {
498 id: 'a',
499 array1: [
500 {
501 id: 'aa',
502 stringField: 'string',
503 obj: {
504 id: 'aaa',
505 stringField: 'string',
506 },
507 },
508 ],
509 array2: [
510 {
511 id: 'ab',
512 stringField: 'string2',
513 obj: {
514 id: 'aaa',
515 numberField: 1,
516 },
517 },
518 ],
519 };
520 var normalized = writer.writeQueryToStore({
521 query: query,
522 result: lodash_1.cloneDeep(result),
523 dataIdFromObject: getIdField,
524 });
525 expect(normalized.toObject()).toEqual({
526 ROOT_QUERY: {
527 id: 'a',
528 array1: [
529 {
530 type: 'id',
531 id: 'aa',
532 generated: false,
533 },
534 ],
535 array2: [
536 {
537 type: 'id',
538 id: 'ab',
539 generated: false,
540 },
541 ],
542 },
543 aa: {
544 id: 'aa',
545 stringField: 'string',
546 obj: {
547 type: 'id',
548 id: 'aaa',
549 generated: false,
550 },
551 },
552 ab: {
553 id: 'ab',
554 stringField: 'string2',
555 obj: {
556 type: 'id',
557 id: 'aaa',
558 generated: false,
559 },
560 },
561 aaa: {
562 id: 'aaa',
563 stringField: 'string',
564 numberField: 1,
565 },
566 });
567 });
568 it('properly normalizes an object occurring in the same graphql array path twice', function () {
569 var query = graphql_tag_1.default(templateObject_18 || (templateObject_18 = tslib_1.__makeTemplateObject(["\n {\n id\n array1 {\n id\n stringField\n obj {\n id\n stringField\n numberField\n }\n }\n }\n "], ["\n {\n id\n array1 {\n id\n stringField\n obj {\n id\n stringField\n numberField\n }\n }\n }\n "])));
570 var result = {
571 id: 'a',
572 array1: [
573 {
574 id: 'aa',
575 stringField: 'string',
576 obj: {
577 id: 'aaa',
578 stringField: 'string',
579 numberField: 1,
580 },
581 },
582 {
583 id: 'ab',
584 stringField: 'string2',
585 obj: {
586 id: 'aaa',
587 stringField: 'should not be written',
588 numberField: 2,
589 },
590 },
591 ],
592 };
593 var normalized = writer.writeQueryToStore({
594 query: query,
595 result: lodash_1.cloneDeep(result),
596 dataIdFromObject: getIdField,
597 });
598 expect(normalized.toObject()).toEqual({
599 ROOT_QUERY: {
600 id: 'a',
601 array1: [
602 {
603 type: 'id',
604 id: 'aa',
605 generated: false,
606 },
607 {
608 type: 'id',
609 id: 'ab',
610 generated: false,
611 },
612 ],
613 },
614 aa: {
615 id: 'aa',
616 stringField: 'string',
617 obj: {
618 type: 'id',
619 id: 'aaa',
620 generated: false,
621 },
622 },
623 ab: {
624 id: 'ab',
625 stringField: 'string2',
626 obj: {
627 type: 'id',
628 id: 'aaa',
629 generated: false,
630 },
631 },
632 aaa: {
633 id: 'aaa',
634 stringField: 'string',
635 numberField: 1,
636 },
637 });
638 });
639 it('merges nodes', function () {
640 var query = graphql_tag_1.default(templateObject_19 || (templateObject_19 = tslib_1.__makeTemplateObject(["\n {\n id\n numberField\n nullField\n }\n "], ["\n {\n id\n numberField\n nullField\n }\n "])));
641 var result = {
642 id: 'abcd',
643 numberField: 5,
644 nullField: null,
645 };
646 var store = writer.writeQueryToStore({
647 query: query,
648 result: lodash_1.cloneDeep(result),
649 dataIdFromObject: getIdField,
650 });
651 var query2 = graphql_tag_1.default(templateObject_20 || (templateObject_20 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n nullField\n }\n "], ["\n {\n id\n stringField\n nullField\n }\n "])));
652 var result2 = {
653 id: 'abcd',
654 stringField: 'This is a string!',
655 nullField: null,
656 };
657 var store2 = writer.writeQueryToStore({
658 store: store,
659 query: query2,
660 result: result2,
661 dataIdFromObject: getIdField,
662 });
663 expect(store2.toObject()).toEqual({
664 ROOT_QUERY: lodash_1.assign({}, result, result2),
665 });
666 });
667 it('properly normalizes a nested object that returns null', function () {
668 var query = graphql_tag_1.default(templateObject_21 || (templateObject_21 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n nestedObj {\n id\n stringField\n numberField\n nullField\n }\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n nestedObj {\n id\n stringField\n numberField\n nullField\n }\n }\n "])));
669 var result = {
670 id: 'abcd',
671 stringField: 'This is a string!',
672 numberField: 5,
673 nullField: null,
674 nestedObj: null,
675 };
676 expect(writer
677 .writeQueryToStore({
678 query: query,
679 result: lodash_1.cloneDeep(result),
680 })
681 .toObject()).toEqual({
682 ROOT_QUERY: lodash_1.assign({}, lodash_1.assign({}, lodash_1.omit(result, 'nestedObj')), {
683 nestedObj: null,
684 }),
685 });
686 });
687 it('properly normalizes an object with an ID when no extension is passed', function () {
688 var query = graphql_tag_1.default(templateObject_22 || (templateObject_22 = tslib_1.__makeTemplateObject(["\n {\n people_one(id: \"5\") {\n id\n stringField\n }\n }\n "], ["\n {\n people_one(id: \"5\") {\n id\n stringField\n }\n }\n "])));
689 var result = {
690 people_one: {
691 id: 'abcd',
692 stringField: 'This is a string!',
693 },
694 };
695 expect(writer
696 .writeQueryToStore({
697 query: query,
698 result: lodash_1.cloneDeep(result),
699 })
700 .toObject()).toEqual({
701 ROOT_QUERY: {
702 'people_one({"id":"5"})': {
703 type: 'id',
704 id: '$ROOT_QUERY.people_one({"id":"5"})',
705 generated: true,
706 },
707 },
708 '$ROOT_QUERY.people_one({"id":"5"})': {
709 id: 'abcd',
710 stringField: 'This is a string!',
711 },
712 });
713 });
714 it('consistently serialize different types of input when passed inlined or as variable', function () {
715 var testData = [
716 {
717 mutation: graphql_tag_1.default(templateObject_23 || (templateObject_23 = tslib_1.__makeTemplateObject(["\n mutation mut($in: Int!) {\n mut(inline: 5, variable: $in) {\n id\n }\n }\n "], ["\n mutation mut($in: Int!) {\n mut(inline: 5, variable: $in) {\n id\n }\n }\n "]))),
718 variables: { in: 5 },
719 expected: 'mut({"inline":5,"variable":5})',
720 },
721 {
722 mutation: graphql_tag_1.default(templateObject_24 || (templateObject_24 = tslib_1.__makeTemplateObject(["\n mutation mut($in: Float!) {\n mut(inline: 5.5, variable: $in) {\n id\n }\n }\n "], ["\n mutation mut($in: Float!) {\n mut(inline: 5.5, variable: $in) {\n id\n }\n }\n "]))),
723 variables: { in: 5.5 },
724 expected: 'mut({"inline":5.5,"variable":5.5})',
725 },
726 {
727 mutation: graphql_tag_1.default(templateObject_25 || (templateObject_25 = tslib_1.__makeTemplateObject(["\n mutation mut($in: String!) {\n mut(inline: \"abc\", variable: $in) {\n id\n }\n }\n "], ["\n mutation mut($in: String!) {\n mut(inline: \"abc\", variable: $in) {\n id\n }\n }\n "]))),
728 variables: { in: 'abc' },
729 expected: 'mut({"inline":"abc","variable":"abc"})',
730 },
731 {
732 mutation: graphql_tag_1.default(templateObject_26 || (templateObject_26 = tslib_1.__makeTemplateObject(["\n mutation mut($in: Array!) {\n mut(inline: [1, 2], variable: $in) {\n id\n }\n }\n "], ["\n mutation mut($in: Array!) {\n mut(inline: [1, 2], variable: $in) {\n id\n }\n }\n "]))),
733 variables: { in: [1, 2] },
734 expected: 'mut({"inline":[1,2],"variable":[1,2]})',
735 },
736 {
737 mutation: graphql_tag_1.default(templateObject_27 || (templateObject_27 = tslib_1.__makeTemplateObject(["\n mutation mut($in: Object!) {\n mut(inline: { a: 1 }, variable: $in) {\n id\n }\n }\n "], ["\n mutation mut($in: Object!) {\n mut(inline: { a: 1 }, variable: $in) {\n id\n }\n }\n "]))),
738 variables: { in: { a: 1 } },
739 expected: 'mut({"inline":{"a":1},"variable":{"a":1}})',
740 },
741 {
742 mutation: graphql_tag_1.default(templateObject_28 || (templateObject_28 = tslib_1.__makeTemplateObject(["\n mutation mut($in: Boolean!) {\n mut(inline: true, variable: $in) {\n id\n }\n }\n "], ["\n mutation mut($in: Boolean!) {\n mut(inline: true, variable: $in) {\n id\n }\n }\n "]))),
743 variables: { in: true },
744 expected: 'mut({"inline":true,"variable":true})',
745 },
746 ];
747 function isOperationDefinition(definition) {
748 return definition.kind === 'OperationDefinition';
749 }
750 function isField(selection) {
751 return selection.kind === 'Field';
752 }
753 testData.forEach(function (data) {
754 data.mutation.definitions.forEach(function (definition) {
755 if (isOperationDefinition(definition)) {
756 definition.selectionSet.selections.forEach(function (selection) {
757 if (isField(selection)) {
758 expect(apollo_utilities_1.storeKeyNameFromField(selection, data.variables)).toEqual(data.expected);
759 }
760 });
761 }
762 });
763 });
764 });
765 it('properly normalizes a mutation with object or array parameters and variables', function () {
766 var mutation = graphql_tag_1.default(templateObject_29 || (templateObject_29 = tslib_1.__makeTemplateObject(["\n mutation some_mutation($nil: ID, $in: Object) {\n some_mutation(\n input: {\n id: \"5\"\n arr: [1, { a: \"b\" }]\n obj: { a: \"b\" }\n num: 5.5\n nil: $nil\n bo: true\n }\n ) {\n id\n }\n some_mutation_with_variables(input: $in) {\n id\n }\n }\n "], ["\n mutation some_mutation($nil: ID, $in: Object) {\n some_mutation(\n input: {\n id: \"5\"\n arr: [1, { a: \"b\" }]\n obj: { a: \"b\" }\n num: 5.5\n nil: $nil\n bo: true\n }\n ) {\n id\n }\n some_mutation_with_variables(input: $in) {\n id\n }\n }\n "])));
767 var result = {
768 some_mutation: {
769 id: 'id',
770 },
771 some_mutation_with_variables: {
772 id: 'id',
773 },
774 };
775 var variables = {
776 nil: null,
777 in: {
778 id: '5',
779 arr: [1, { a: 'b' }],
780 obj: { a: 'b' },
781 num: 5.5,
782 nil: null,
783 bo: true,
784 },
785 };
786 function isOperationDefinition(value) {
787 return value.kind === 'OperationDefinition';
788 }
789 mutation.definitions.map(function (def) {
790 if (isOperationDefinition(def)) {
791 expect(writer
792 .writeSelectionSetToStore({
793 dataId: '5',
794 selectionSet: def.selectionSet,
795 result: lodash_1.cloneDeep(result),
796 context: {
797 store: objectCache_1.defaultNormalizedCacheFactory(),
798 variables: variables,
799 dataIdFromObject: function () { return '5'; },
800 },
801 })
802 .toObject()).toEqual({
803 '5': {
804 id: 'id',
805 'some_mutation({"input":{"arr":[1,{"a":"b"}],"bo":true,"id":"5","nil":null,"num":5.5,"obj":{"a":"b"}}})': {
806 generated: false,
807 id: '5',
808 type: 'id',
809 },
810 'some_mutation_with_variables({"input":{"arr":[1,{"a":"b"}],"bo":true,"id":"5","nil":null,"num":5.5,"obj":{"a":"b"}}})': {
811 generated: false,
812 id: '5',
813 type: 'id',
814 },
815 },
816 });
817 }
818 else {
819 throw 'No operation definition found';
820 }
821 });
822 });
823 it('should write to store if `dataIdFromObject` returns an ID of 0', function () {
824 var query = graphql_tag_1.default(templateObject_30 || (templateObject_30 = tslib_1.__makeTemplateObject(["\n query {\n author {\n firstName\n id\n __typename\n }\n }\n "], ["\n query {\n author {\n firstName\n id\n __typename\n }\n }\n "])));
825 var data = {
826 author: {
827 id: 0,
828 __typename: 'Author',
829 firstName: 'John',
830 },
831 };
832 var expStore = objectCache_1.defaultNormalizedCacheFactory({
833 ROOT_QUERY: {
834 author: {
835 id: 0,
836 typename: 'Author',
837 type: 'id',
838 generated: false,
839 },
840 },
841 0: {
842 id: data.author.id,
843 __typename: data.author.__typename,
844 firstName: data.author.firstName,
845 },
846 });
847 expect(writer
848 .writeQueryToStore({
849 result: data,
850 query: query,
851 dataIdFromObject: function () { return 0; },
852 })
853 .toObject()).toEqual(expStore.toObject());
854 });
855 describe('type escaping', function () {
856 var dataIdFromObject = function (object) {
857 if (object.__typename && object.id) {
858 return object.__typename + '__' + object.id;
859 }
860 return undefined;
861 };
862 it('should correctly escape generated ids', function () {
863 var query = graphql_tag_1.default(templateObject_31 || (templateObject_31 = tslib_1.__makeTemplateObject(["\n query {\n author {\n firstName\n lastName\n }\n }\n "], ["\n query {\n author {\n firstName\n lastName\n }\n }\n "])));
864 var data = {
865 author: {
866 firstName: 'John',
867 lastName: 'Smith',
868 },
869 };
870 var expStore = objectCache_1.defaultNormalizedCacheFactory({
871 ROOT_QUERY: {
872 author: {
873 type: 'id',
874 id: '$ROOT_QUERY.author',
875 generated: true,
876 },
877 },
878 '$ROOT_QUERY.author': data.author,
879 });
880 expect(writer
881 .writeQueryToStore({
882 result: data,
883 query: query,
884 })
885 .toObject()).toEqual(expStore.toObject());
886 });
887 it('should correctly escape real ids', function () {
888 var _a;
889 var query = graphql_tag_1.default(templateObject_32 || (templateObject_32 = tslib_1.__makeTemplateObject(["\n query {\n author {\n firstName\n id\n __typename\n }\n }\n "], ["\n query {\n author {\n firstName\n id\n __typename\n }\n }\n "])));
890 var data = {
891 author: {
892 firstName: 'John',
893 id: '129',
894 __typename: 'Author',
895 },
896 };
897 var expStore = objectCache_1.defaultNormalizedCacheFactory((_a = {
898 ROOT_QUERY: {
899 author: {
900 type: 'id',
901 id: dataIdFromObject(data.author),
902 generated: false,
903 typename: 'Author',
904 },
905 }
906 },
907 _a[dataIdFromObject(data.author)] = {
908 firstName: data.author.firstName,
909 id: data.author.id,
910 __typename: data.author.__typename,
911 },
912 _a));
913 expect(writer
914 .writeQueryToStore({
915 result: data,
916 query: query,
917 dataIdFromObject: dataIdFromObject,
918 })
919 .toObject()).toEqual(expStore.toObject());
920 });
921 it('should correctly escape json blobs', function () {
922 var _a;
923 var query = graphql_tag_1.default(templateObject_33 || (templateObject_33 = tslib_1.__makeTemplateObject(["\n query {\n author {\n info\n id\n __typename\n }\n }\n "], ["\n query {\n author {\n info\n id\n __typename\n }\n }\n "])));
924 var data = {
925 author: {
926 info: {
927 name: 'John',
928 },
929 id: '129',
930 __typename: 'Author',
931 },
932 };
933 var expStore = objectCache_1.defaultNormalizedCacheFactory((_a = {
934 ROOT_QUERY: {
935 author: {
936 type: 'id',
937 id: dataIdFromObject(data.author),
938 generated: false,
939 typename: 'Author',
940 },
941 }
942 },
943 _a[dataIdFromObject(data.author)] = {
944 __typename: data.author.__typename,
945 id: data.author.id,
946 info: {
947 type: 'json',
948 json: data.author.info,
949 },
950 },
951 _a));
952 expect(writer
953 .writeQueryToStore({
954 result: data,
955 query: query,
956 dataIdFromObject: dataIdFromObject,
957 })
958 .toObject()).toEqual(expStore.toObject());
959 });
960 });
961 it('should merge objects when overwriting a generated id with a real id', function () {
962 var dataWithoutId = {
963 author: {
964 firstName: 'John',
965 lastName: 'Smith',
966 __typename: 'Author',
967 },
968 };
969 var dataWithId = {
970 author: {
971 firstName: 'John',
972 id: '129',
973 __typename: 'Author',
974 },
975 };
976 var dataIdFromObject = function (object) {
977 if (object.__typename && object.id) {
978 return object.__typename + '__' + object.id;
979 }
980 return undefined;
981 };
982 var queryWithoutId = graphql_tag_1.default(templateObject_34 || (templateObject_34 = tslib_1.__makeTemplateObject(["\n query {\n author {\n firstName\n lastName\n __typename\n }\n }\n "], ["\n query {\n author {\n firstName\n lastName\n __typename\n }\n }\n "])));
983 var queryWithId = graphql_tag_1.default(templateObject_35 || (templateObject_35 = tslib_1.__makeTemplateObject(["\n query {\n author {\n firstName\n id\n __typename\n }\n }\n "], ["\n query {\n author {\n firstName\n id\n __typename\n }\n }\n "])));
984 var expStoreWithoutId = objectCache_1.defaultNormalizedCacheFactory({
985 '$ROOT_QUERY.author': {
986 firstName: 'John',
987 lastName: 'Smith',
988 __typename: 'Author',
989 },
990 ROOT_QUERY: {
991 author: {
992 type: 'id',
993 id: '$ROOT_QUERY.author',
994 generated: true,
995 typename: 'Author',
996 },
997 },
998 });
999 var expStoreWithId = objectCache_1.defaultNormalizedCacheFactory({
1000 Author__129: {
1001 firstName: 'John',
1002 lastName: 'Smith',
1003 id: '129',
1004 __typename: 'Author',
1005 },
1006 ROOT_QUERY: {
1007 author: {
1008 type: 'id',
1009 id: 'Author__129',
1010 generated: false,
1011 typename: 'Author',
1012 },
1013 },
1014 });
1015 var storeWithoutId = writer.writeQueryToStore({
1016 result: dataWithoutId,
1017 query: queryWithoutId,
1018 dataIdFromObject: dataIdFromObject,
1019 });
1020 expect(storeWithoutId.toObject()).toEqual(expStoreWithoutId.toObject());
1021 var storeWithId = writer.writeQueryToStore({
1022 result: dataWithId,
1023 query: queryWithId,
1024 store: storeWithoutId,
1025 dataIdFromObject: dataIdFromObject,
1026 });
1027 expect(storeWithId.toObject()).toEqual(expStoreWithId.toObject());
1028 });
1029 it('should allow a union of objects of a different type, when overwriting a generated id with a real id', function () {
1030 var dataWithPlaceholder = {
1031 author: {
1032 hello: 'Foo',
1033 __typename: 'Placeholder',
1034 },
1035 };
1036 var dataWithAuthor = {
1037 author: {
1038 firstName: 'John',
1039 lastName: 'Smith',
1040 id: '129',
1041 __typename: 'Author',
1042 },
1043 };
1044 var dataIdFromObject = function (object) {
1045 if (object.__typename && object.id) {
1046 return object.__typename + '__' + object.id;
1047 }
1048 return undefined;
1049 };
1050 var query = graphql_tag_1.default(templateObject_36 || (templateObject_36 = tslib_1.__makeTemplateObject(["\n query {\n author {\n ... on Author {\n firstName\n lastName\n id\n __typename\n }\n ... on Placeholder {\n hello\n __typename\n }\n }\n }\n "], ["\n query {\n author {\n ... on Author {\n firstName\n lastName\n id\n __typename\n }\n ... on Placeholder {\n hello\n __typename\n }\n }\n }\n "])));
1051 var expStoreWithPlaceholder = objectCache_1.defaultNormalizedCacheFactory({
1052 '$ROOT_QUERY.author': {
1053 hello: 'Foo',
1054 __typename: 'Placeholder',
1055 },
1056 ROOT_QUERY: {
1057 author: {
1058 type: 'id',
1059 id: '$ROOT_QUERY.author',
1060 generated: true,
1061 typename: 'Placeholder',
1062 },
1063 },
1064 });
1065 var expStoreWithAuthor = objectCache_1.defaultNormalizedCacheFactory({
1066 Author__129: {
1067 firstName: 'John',
1068 lastName: 'Smith',
1069 id: '129',
1070 __typename: 'Author',
1071 },
1072 ROOT_QUERY: {
1073 author: {
1074 type: 'id',
1075 id: 'Author__129',
1076 generated: false,
1077 typename: 'Author',
1078 },
1079 },
1080 });
1081 var store = writer.writeQueryToStore({
1082 result: dataWithPlaceholder,
1083 query: query,
1084 dataIdFromObject: dataIdFromObject,
1085 });
1086 expect(store.toObject()).toEqual(expStoreWithPlaceholder.toObject());
1087 writer.writeQueryToStore({
1088 result: dataWithAuthor,
1089 query: query,
1090 store: store,
1091 dataIdFromObject: dataIdFromObject,
1092 });
1093 expect(store.toObject()).toEqual(expStoreWithAuthor.toObject());
1094 writer.writeQueryToStore({
1095 result: dataWithPlaceholder,
1096 query: query,
1097 store: store,
1098 dataIdFromObject: dataIdFromObject,
1099 });
1100 expect(store.toObject()).toEqual(tslib_1.__assign(tslib_1.__assign({}, expStoreWithAuthor.toObject()), expStoreWithPlaceholder.toObject()));
1101 });
1102 it('does not swallow errors other than field errors', function () {
1103 var query = graphql_tag_1.default(templateObject_37 || (templateObject_37 = tslib_1.__makeTemplateObject(["\n query {\n ...notARealFragment\n fortuneCookie\n }\n "], ["\n query {\n ...notARealFragment\n fortuneCookie\n }\n "])));
1104 var result = {
1105 fortuneCookie: 'Star Wars unit tests are boring',
1106 };
1107 expect(function () {
1108 writer.writeQueryToStore({
1109 result: result,
1110 query: query,
1111 });
1112 }).toThrowError(/No fragment/);
1113 });
1114 it('does not change object references if the value is the same', function () {
1115 var query = graphql_tag_1.default(templateObject_38 || (templateObject_38 = tslib_1.__makeTemplateObject(["\n {\n id\n stringField\n numberField\n nullField\n }\n "], ["\n {\n id\n stringField\n numberField\n nullField\n }\n "])));
1116 var result = {
1117 id: 'abcd',
1118 stringField: 'This is a string!',
1119 numberField: 5,
1120 nullField: null,
1121 };
1122 var store = writer.writeQueryToStore({
1123 query: query,
1124 result: lodash_1.cloneDeep(result),
1125 });
1126 var newStore = writer.writeQueryToStore({
1127 query: query,
1128 result: lodash_1.cloneDeep(result),
1129 store: objectCache_1.defaultNormalizedCacheFactory(store.toObject()),
1130 });
1131 Object.keys(store.toObject()).forEach(function (field) {
1132 expect(store.get(field)).toEqual(newStore.get(field));
1133 });
1134 });
1135 describe('writeResultToStore shape checking', function () {
1136 var query = graphql_tag_1.default(templateObject_39 || (templateObject_39 = tslib_1.__makeTemplateObject(["\n query {\n todos {\n id\n name\n description\n }\n }\n "], ["\n query {\n todos {\n id\n name\n description\n }\n }\n "])));
1137 it('should write the result data without validating its shape when a fragment matcher is not provided', function () {
1138 var result = {
1139 todos: [
1140 {
1141 id: '1',
1142 name: 'Todo 1',
1143 },
1144 ],
1145 };
1146 var newStore = writer.writeResultToStore({
1147 dataId: 'ROOT_QUERY',
1148 result: result,
1149 document: query,
1150 dataIdFromObject: getIdField,
1151 });
1152 expect(newStore.get('1')).toEqual(result.todos[0]);
1153 });
1154 it('should warn when it receives the wrong data with non-union fragments (using an heuristic matcher)', function () {
1155 var fragmentMatcherFunction = new __1.HeuristicFragmentMatcher().match;
1156 var result = {
1157 todos: [
1158 {
1159 id: '1',
1160 name: 'Todo 1',
1161 },
1162 ],
1163 };
1164 return withWarning(function () {
1165 var newStore = writer.writeResultToStore({
1166 dataId: 'ROOT_QUERY',
1167 result: result,
1168 document: query,
1169 dataIdFromObject: getIdField,
1170 fragmentMatcherFunction: fragmentMatcherFunction,
1171 });
1172 expect(newStore.get('1')).toEqual(result.todos[0]);
1173 }, /Missing field description/);
1174 });
1175 it('should warn when it receives the wrong data inside a fragment (using an introspection matcher)', function () {
1176 var fragmentMatcherFunction = new __1.IntrospectionFragmentMatcher({
1177 introspectionQueryResultData: {
1178 __schema: {
1179 types: [
1180 {
1181 kind: 'UNION',
1182 name: 'Todo',
1183 possibleTypes: [
1184 { name: 'ShoppingCartItem' },
1185 { name: 'TaskItem' },
1186 ],
1187 },
1188 ],
1189 },
1190 },
1191 }).match;
1192 var queryWithInterface = graphql_tag_1.default(templateObject_40 || (templateObject_40 = tslib_1.__makeTemplateObject(["\n query {\n todos {\n id\n name\n description\n ...TodoFragment\n }\n }\n\n fragment TodoFragment on Todo {\n ... on ShoppingCartItem {\n price\n __typename\n }\n ... on TaskItem {\n date\n __typename\n }\n __typename\n }\n "], ["\n query {\n todos {\n id\n name\n description\n ...TodoFragment\n }\n }\n\n fragment TodoFragment on Todo {\n ... on ShoppingCartItem {\n price\n __typename\n }\n ... on TaskItem {\n date\n __typename\n }\n __typename\n }\n "])));
1193 var result = {
1194 todos: [
1195 {
1196 id: '1',
1197 name: 'Todo 1',
1198 description: 'Description 1',
1199 __typename: 'ShoppingCartItem',
1200 },
1201 ],
1202 };
1203 return withWarning(function () {
1204 var newStore = writer.writeResultToStore({
1205 dataId: 'ROOT_QUERY',
1206 result: result,
1207 document: queryWithInterface,
1208 dataIdFromObject: getIdField,
1209 fragmentMatcherFunction: fragmentMatcherFunction,
1210 });
1211 expect(newStore.get('1')).toEqual(result.todos[0]);
1212 }, /Missing field price/);
1213 });
1214 it('should warn if a result is missing __typename when required (using an heuristic matcher)', function () {
1215 var fragmentMatcherFunction = new __1.HeuristicFragmentMatcher().match;
1216 var result = {
1217 todos: [
1218 {
1219 id: '1',
1220 name: 'Todo 1',
1221 description: 'Description 1',
1222 },
1223 ],
1224 };
1225 return withWarning(function () {
1226 var newStore = writer.writeResultToStore({
1227 dataId: 'ROOT_QUERY',
1228 result: result,
1229 document: apollo_utilities_1.addTypenameToDocument(query),
1230 dataIdFromObject: getIdField,
1231 fragmentMatcherFunction: fragmentMatcherFunction,
1232 });
1233 expect(newStore.get('1')).toEqual(result.todos[0]);
1234 }, /Missing field __typename/);
1235 });
1236 it('should not warn if a field is null', function () {
1237 var result = {
1238 todos: null,
1239 };
1240 var newStore = writer.writeResultToStore({
1241 dataId: 'ROOT_QUERY',
1242 result: result,
1243 document: query,
1244 dataIdFromObject: getIdField,
1245 });
1246 expect(newStore.get('ROOT_QUERY')).toEqual({ todos: null });
1247 });
1248 it('should not warn if a field is defered', function () {
1249 var originalWarn = console.warn;
1250 console.warn = jest.fn(function () {
1251 var args = [];
1252 for (var _i = 0; _i < arguments.length; _i++) {
1253 args[_i] = arguments[_i];
1254 }
1255 });
1256 var defered = graphql_tag_1.default(templateObject_41 || (templateObject_41 = tslib_1.__makeTemplateObject(["\n query LazyLoad {\n id\n expensive @defer\n }\n "], ["\n query LazyLoad {\n id\n expensive @defer\n }\n "])));
1257 var result = {
1258 id: 1,
1259 };
1260 var fragmentMatcherFunction = new __1.HeuristicFragmentMatcher().match;
1261 var newStore = writer.writeResultToStore({
1262 dataId: 'ROOT_QUERY',
1263 result: result,
1264 document: defered,
1265 dataIdFromObject: getIdField,
1266 fragmentMatcherFunction: fragmentMatcherFunction,
1267 });
1268 expect(newStore.get('ROOT_QUERY')).toEqual({ id: 1 });
1269 expect(console.warn).not.toBeCalled();
1270 console.warn = originalWarn;
1271 });
1272 });
1273 it('throws when trying to write an object without id that was previously queried with id', function () {
1274 var store = objectCache_1.defaultNormalizedCacheFactory({
1275 ROOT_QUERY: lodash_1.assign({}, {
1276 __typename: 'Query',
1277 item: {
1278 type: 'id',
1279 id: 'abcd',
1280 generated: false,
1281 },
1282 }),
1283 abcd: lodash_1.assign({}, {
1284 id: 'abcd',
1285 __typename: 'Item',
1286 stringField: 'This is a string!',
1287 }),
1288 });
1289 expect(function () {
1290 writer.writeQueryToStore({
1291 store: store,
1292 result: {
1293 item: {
1294 __typename: 'Item',
1295 stringField: 'This is still a string!',
1296 },
1297 },
1298 query: graphql_tag_1.default(templateObject_42 || (templateObject_42 = tslib_1.__makeTemplateObject(["\n query Failure {\n item {\n stringField\n }\n }\n "], ["\n query Failure {\n item {\n stringField\n }\n }\n "]))),
1299 dataIdFromObject: getIdField,
1300 });
1301 }).toThrowErrorMatchingSnapshot();
1302 expect(function () {
1303 writer.writeResultToStore({
1304 store: store,
1305 result: {
1306 item: {
1307 __typename: 'Item',
1308 stringField: 'This is still a string!',
1309 },
1310 },
1311 dataId: 'ROOT_QUERY',
1312 document: graphql_tag_1.default(templateObject_43 || (templateObject_43 = tslib_1.__makeTemplateObject(["\n query {\n item {\n stringField\n }\n }\n "], ["\n query {\n item {\n stringField\n }\n }\n "]))),
1313 dataIdFromObject: getIdField,
1314 });
1315 }).toThrowError(/stringField(.|\n)*abcd/g);
1316 });
1317 it('properly handles the connection directive', function () {
1318 var store = objectCache_1.defaultNormalizedCacheFactory();
1319 writer.writeQueryToStore({
1320 query: graphql_tag_1.default(templateObject_44 || (templateObject_44 = tslib_1.__makeTemplateObject(["\n {\n books(skip: 0, limit: 2) @connection(key: \"abc\") {\n name\n }\n }\n "], ["\n {\n books(skip: 0, limit: 2) @connection(key: \"abc\") {\n name\n }\n }\n "]))),
1321 result: {
1322 books: [
1323 {
1324 name: 'abcd',
1325 },
1326 ],
1327 },
1328 store: store,
1329 });
1330 writer.writeQueryToStore({
1331 query: graphql_tag_1.default(templateObject_45 || (templateObject_45 = tslib_1.__makeTemplateObject(["\n {\n books(skip: 2, limit: 4) @connection(key: \"abc\") {\n name\n }\n }\n "], ["\n {\n books(skip: 2, limit: 4) @connection(key: \"abc\") {\n name\n }\n }\n "]))),
1332 result: {
1333 books: [
1334 {
1335 name: 'efgh',
1336 },
1337 ],
1338 },
1339 store: store,
1340 });
1341 expect(store.toObject()).toEqual({
1342 ROOT_QUERY: {
1343 abc: [
1344 {
1345 generated: true,
1346 id: 'ROOT_QUERY.abc.0',
1347 type: 'id',
1348 },
1349 ],
1350 },
1351 'ROOT_QUERY.abc.0': {
1352 name: 'efgh',
1353 },
1354 });
1355 });
1356 it('should keep reference when type of mixed inlined field changes', function () {
1357 var store = objectCache_1.defaultNormalizedCacheFactory();
1358 var query = graphql_tag_1.default(templateObject_46 || (templateObject_46 = tslib_1.__makeTemplateObject(["\n query {\n animals {\n species {\n name\n }\n }\n }\n "], ["\n query {\n animals {\n species {\n name\n }\n }\n }\n "])));
1359 writer.writeQueryToStore({
1360 query: query,
1361 result: {
1362 animals: [
1363 {
1364 __typename: 'Animal',
1365 species: {
1366 __typename: 'Cat',
1367 name: 'cat',
1368 },
1369 },
1370 ],
1371 },
1372 store: store,
1373 });
1374 expect(store.toObject()).toEqual({
1375 '$ROOT_QUERY.animals.0.species': { name: 'cat' },
1376 ROOT_QUERY: {
1377 animals: [
1378 {
1379 generated: true,
1380 id: 'ROOT_QUERY.animals.0',
1381 type: 'id',
1382 typename: 'Animal',
1383 },
1384 ],
1385 },
1386 'ROOT_QUERY.animals.0': {
1387 species: {
1388 generated: true,
1389 id: '$ROOT_QUERY.animals.0.species',
1390 type: 'id',
1391 typename: 'Cat',
1392 },
1393 },
1394 });
1395 writer.writeQueryToStore({
1396 query: query,
1397 result: {
1398 animals: [
1399 {
1400 __typename: 'Animal',
1401 species: {
1402 __typename: 'Dog',
1403 name: 'dog',
1404 },
1405 },
1406 ],
1407 },
1408 store: store,
1409 });
1410 expect(store.toObject()).toEqual({
1411 '$ROOT_QUERY.animals.0.species': { name: 'dog' },
1412 ROOT_QUERY: {
1413 animals: [
1414 {
1415 generated: true,
1416 id: 'ROOT_QUERY.animals.0',
1417 type: 'id',
1418 typename: 'Animal',
1419 },
1420 ],
1421 },
1422 'ROOT_QUERY.animals.0': {
1423 species: {
1424 generated: true,
1425 id: '$ROOT_QUERY.animals.0.species',
1426 type: 'id',
1427 typename: 'Dog',
1428 },
1429 },
1430 });
1431 });
1432 it('should not keep reference when type of mixed inlined field changes to non-inlined field', function () {
1433 var store = objectCache_1.defaultNormalizedCacheFactory();
1434 var dataIdFromObject = function (object) {
1435 if (object.__typename && object.id) {
1436 return object.__typename + '__' + object.id;
1437 }
1438 return undefined;
1439 };
1440 var query = graphql_tag_1.default(templateObject_47 || (templateObject_47 = tslib_1.__makeTemplateObject(["\n query {\n animals {\n species {\n id\n name\n }\n }\n }\n "], ["\n query {\n animals {\n species {\n id\n name\n }\n }\n }\n "])));
1441 writer.writeQueryToStore({
1442 query: query,
1443 result: {
1444 animals: [
1445 {
1446 __typename: 'Animal',
1447 species: {
1448 __typename: 'Cat',
1449 name: 'cat',
1450 },
1451 },
1452 ],
1453 },
1454 dataIdFromObject: dataIdFromObject,
1455 store: store,
1456 });
1457 expect(store.toObject()).toEqual({
1458 '$ROOT_QUERY.animals.0.species': { name: 'cat' },
1459 ROOT_QUERY: {
1460 animals: [
1461 {
1462 generated: true,
1463 id: 'ROOT_QUERY.animals.0',
1464 type: 'id',
1465 typename: 'Animal',
1466 },
1467 ],
1468 },
1469 'ROOT_QUERY.animals.0': {
1470 species: {
1471 generated: true,
1472 id: '$ROOT_QUERY.animals.0.species',
1473 type: 'id',
1474 typename: 'Cat',
1475 },
1476 },
1477 });
1478 writer.writeQueryToStore({
1479 query: query,
1480 result: {
1481 animals: [
1482 {
1483 __typename: 'Animal',
1484 species: {
1485 id: 'dog-species',
1486 __typename: 'Dog',
1487 name: 'dog',
1488 },
1489 },
1490 ],
1491 },
1492 dataIdFromObject: dataIdFromObject,
1493 store: store,
1494 });
1495 expect(store.toObject()).toEqual({
1496 '$ROOT_QUERY.animals.0.species': undefined,
1497 'Dog__dog-species': {
1498 id: 'dog-species',
1499 name: 'dog',
1500 },
1501 ROOT_QUERY: {
1502 animals: [
1503 {
1504 generated: true,
1505 id: 'ROOT_QUERY.animals.0',
1506 type: 'id',
1507 typename: 'Animal',
1508 },
1509 ],
1510 },
1511 'ROOT_QUERY.animals.0': {
1512 species: {
1513 generated: false,
1514 id: 'Dog__dog-species',
1515 type: 'id',
1516 typename: 'Dog',
1517 },
1518 },
1519 });
1520 });
1521});
1522var 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, templateObject_34, templateObject_35, templateObject_36, templateObject_37, templateObject_38, templateObject_39, templateObject_40, templateObject_41, templateObject_42, templateObject_43, templateObject_44, templateObject_45, templateObject_46, templateObject_47;
1523//# sourceMappingURL=writeToStore.js.map
\No newline at end of file