1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var graphql_tag_1 = tslib_1.__importStar(require("graphql-tag"));
|
5 | var apollo_utilities_1 = require("apollo-utilities");
|
6 | var lodash_1 = require("lodash");
|
7 | var __1 = require("..");
|
8 | graphql_tag_1.disableFragmentWarnings();
|
9 | describe('Cache', function () {
|
10 | function itWithInitialData(message, initialDataForCaches, callback) {
|
11 | var cachesList = [
|
12 | initialDataForCaches.map(function (data) {
|
13 | return new __1.InMemoryCache({
|
14 | addTypename: false,
|
15 | }).restore(lodash_1.cloneDeep(data));
|
16 | }),
|
17 | initialDataForCaches.map(function (data) {
|
18 | return new __1.InMemoryCache({
|
19 | addTypename: false,
|
20 | resultCaching: false,
|
21 | }).restore(lodash_1.cloneDeep(data));
|
22 | }),
|
23 | initialDataForCaches.map(function (data) {
|
24 | return new __1.InMemoryCache({
|
25 | addTypename: false,
|
26 | freezeResults: true,
|
27 | }).restore(lodash_1.cloneDeep(data));
|
28 | }),
|
29 | ];
|
30 | cachesList.forEach(function (caches, i) {
|
31 | it(message + (" (" + (i + 1) + "/" + cachesList.length + ")"), function () {
|
32 | return callback.apply(void 0, caches);
|
33 | });
|
34 | });
|
35 | }
|
36 | function itWithCacheConfig(message, config, callback) {
|
37 | var caches = [
|
38 | new __1.InMemoryCache(tslib_1.__assign(tslib_1.__assign({ addTypename: false }, config), { resultCaching: true })),
|
39 | new __1.InMemoryCache(tslib_1.__assign(tslib_1.__assign({ addTypename: false }, config), { resultCaching: false })),
|
40 | new __1.InMemoryCache(tslib_1.__assign(tslib_1.__assign({ addTypename: false }, config), { freezeResults: true })),
|
41 | ];
|
42 | caches.forEach(function (cache, i) {
|
43 | it(message + (" (" + (i + 1) + "/" + caches.length + ")"), function () { return callback(cache); });
|
44 | });
|
45 | }
|
46 | describe('readQuery', function () {
|
47 | itWithInitialData('will read some data from the store', [
|
48 | {
|
49 | ROOT_QUERY: {
|
50 | a: 1,
|
51 | b: 2,
|
52 | c: 3,
|
53 | },
|
54 | },
|
55 | ], function (proxy) {
|
56 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
57 | query: graphql_tag_1.default(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n {\n a\n }\n "], ["\n {\n a\n }\n "]))),
|
58 | }))).toEqual({ a: 1 });
|
59 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
60 | query: graphql_tag_1.default(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n {\n b\n c\n }\n "], ["\n {\n b\n c\n }\n "]))),
|
61 | }))).toEqual({ b: 2, c: 3 });
|
62 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
63 | query: graphql_tag_1.default(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n {\n a\n b\n c\n }\n "], ["\n {\n a\n b\n c\n }\n "]))),
|
64 | }))).toEqual({ a: 1, b: 2, c: 3 });
|
65 | });
|
66 | itWithInitialData('will read some deeply nested data from the store', [
|
67 | {
|
68 | ROOT_QUERY: {
|
69 | a: 1,
|
70 | b: 2,
|
71 | c: 3,
|
72 | d: {
|
73 | type: 'id',
|
74 | id: 'foo',
|
75 | generated: false,
|
76 | },
|
77 | },
|
78 | foo: {
|
79 | e: 4,
|
80 | f: 5,
|
81 | g: 6,
|
82 | h: {
|
83 | type: 'id',
|
84 | id: 'bar',
|
85 | generated: false,
|
86 | },
|
87 | },
|
88 | bar: {
|
89 | i: 7,
|
90 | j: 8,
|
91 | k: 9,
|
92 | },
|
93 | },
|
94 | ], function (proxy) {
|
95 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
96 | query: graphql_tag_1.default(templateObject_4 || (templateObject_4 = tslib_1.__makeTemplateObject(["\n {\n a\n d {\n e\n }\n }\n "], ["\n {\n a\n d {\n e\n }\n }\n "]))),
|
97 | }))).toEqual({ a: 1, d: { e: 4 } });
|
98 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
99 | query: graphql_tag_1.default(templateObject_5 || (templateObject_5 = tslib_1.__makeTemplateObject(["\n {\n a\n d {\n e\n h {\n i\n }\n }\n }\n "], ["\n {\n a\n d {\n e\n h {\n i\n }\n }\n }\n "]))),
|
100 | }))).toEqual({ a: 1, d: { e: 4, h: { i: 7 } } });
|
101 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
102 | query: graphql_tag_1.default(templateObject_6 || (templateObject_6 = tslib_1.__makeTemplateObject(["\n {\n a\n b\n c\n d {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n }\n "], ["\n {\n a\n b\n c\n d {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n }\n "]))),
|
103 | }))).toEqual({
|
104 | a: 1,
|
105 | b: 2,
|
106 | c: 3,
|
107 | d: { e: 4, f: 5, g: 6, h: { i: 7, j: 8, k: 9 } },
|
108 | });
|
109 | });
|
110 | itWithInitialData('will read some data from the store with variables', [
|
111 | {
|
112 | ROOT_QUERY: {
|
113 | 'field({"literal":true,"value":42})': 1,
|
114 | 'field({"literal":false,"value":42})': 2,
|
115 | },
|
116 | },
|
117 | ], function (proxy) {
|
118 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
119 | query: graphql_tag_1.default(templateObject_7 || (templateObject_7 = tslib_1.__makeTemplateObject(["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "], ["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "]))),
|
120 | variables: {
|
121 | literal: false,
|
122 | value: 42,
|
123 | },
|
124 | }))).toEqual({ a: 1, b: 2 });
|
125 | });
|
126 | itWithInitialData('will read some data from the store with null variables', [
|
127 | {
|
128 | ROOT_QUERY: {
|
129 | 'field({"literal":false,"value":null})': 1,
|
130 | },
|
131 | },
|
132 | ], function (proxy) {
|
133 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery({
|
134 | query: graphql_tag_1.default(templateObject_8 || (templateObject_8 = tslib_1.__makeTemplateObject(["\n query($literal: Boolean, $value: Int) {\n a: field(literal: $literal, value: $value)\n }\n "], ["\n query($literal: Boolean, $value: Int) {\n a: field(literal: $literal, value: $value)\n }\n "]))),
|
135 | variables: {
|
136 | literal: false,
|
137 | value: null,
|
138 | },
|
139 | }))).toEqual({ a: 1 });
|
140 | });
|
141 | itWithInitialData('should not mutate arguments passed in', [
|
142 | {
|
143 | ROOT_QUERY: {
|
144 | 'field({"literal":true,"value":42})': 1,
|
145 | 'field({"literal":false,"value":42})': 2,
|
146 | },
|
147 | },
|
148 | ], function (proxy) {
|
149 | var options = {
|
150 | query: graphql_tag_1.default(templateObject_9 || (templateObject_9 = tslib_1.__makeTemplateObject(["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "], ["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "]))),
|
151 | variables: {
|
152 | literal: false,
|
153 | value: 42,
|
154 | },
|
155 | };
|
156 | var preQueryCopy = lodash_1.cloneDeep(options);
|
157 | expect(apollo_utilities_1.stripSymbols(proxy.readQuery(options))).toEqual({ a: 1, b: 2 });
|
158 | expect(preQueryCopy).toEqual(options);
|
159 | });
|
160 | });
|
161 | describe('readFragment', function () {
|
162 | itWithInitialData('will throw an error when there is no fragment', [
|
163 | {},
|
164 | ], function (proxy) {
|
165 | expect(function () {
|
166 | proxy.readFragment({
|
167 | id: 'x',
|
168 | fragment: graphql_tag_1.default(templateObject_10 || (templateObject_10 = tslib_1.__makeTemplateObject(["\n query {\n a\n b\n c\n }\n "], ["\n query {\n a\n b\n c\n }\n "]))),
|
169 | });
|
170 | }).toThrowError('Found a query operation. No operations are allowed when using a fragment as a query. Only fragments are allowed.');
|
171 | expect(function () {
|
172 | proxy.readFragment({
|
173 | id: 'x',
|
174 | fragment: graphql_tag_1.default(templateObject_11 || (templateObject_11 = tslib_1.__makeTemplateObject(["\n schema {\n query: Query\n }\n "], ["\n schema {\n query: Query\n }\n "]))),
|
175 | });
|
176 | }).toThrowError('Found 0 fragments. `fragmentName` must be provided when there is not exactly 1 fragment.');
|
177 | });
|
178 | itWithInitialData('will throw an error when there is more than one fragment but no fragment name', [{}], function (proxy) {
|
179 | expect(function () {
|
180 | proxy.readFragment({
|
181 | id: 'x',
|
182 | fragment: graphql_tag_1.default(templateObject_12 || (templateObject_12 = tslib_1.__makeTemplateObject(["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n "], ["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n "]))),
|
183 | });
|
184 | }).toThrowError('Found 2 fragments. `fragmentName` must be provided when there is not exactly 1 fragment.');
|
185 | expect(function () {
|
186 | proxy.readFragment({
|
187 | id: 'x',
|
188 | fragment: graphql_tag_1.default(templateObject_13 || (templateObject_13 = tslib_1.__makeTemplateObject(["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n\n fragment c on C {\n c\n }\n "], ["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n\n fragment c on C {\n c\n }\n "]))),
|
189 | });
|
190 | }).toThrowError('Found 3 fragments. `fragmentName` must be provided when there is not exactly 1 fragment.');
|
191 | });
|
192 | itWithInitialData('will read some deeply nested data from the store at any id', [
|
193 | {
|
194 | ROOT_QUERY: {
|
195 | __typename: 'Type1',
|
196 | a: 1,
|
197 | b: 2,
|
198 | c: 3,
|
199 | d: {
|
200 | type: 'id',
|
201 | id: 'foo',
|
202 | generated: false,
|
203 | },
|
204 | },
|
205 | foo: {
|
206 | __typename: 'Foo',
|
207 | e: 4,
|
208 | f: 5,
|
209 | g: 6,
|
210 | h: {
|
211 | type: 'id',
|
212 | id: 'bar',
|
213 | generated: false,
|
214 | },
|
215 | },
|
216 | bar: {
|
217 | __typename: 'Bar',
|
218 | i: 7,
|
219 | j: 8,
|
220 | k: 9,
|
221 | },
|
222 | },
|
223 | ], function (proxy) {
|
224 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
225 | id: 'foo',
|
226 | fragment: graphql_tag_1.default(templateObject_14 || (templateObject_14 = tslib_1.__makeTemplateObject(["\n fragment fragmentFoo on Foo {\n e\n h {\n i\n }\n }\n "], ["\n fragment fragmentFoo on Foo {\n e\n h {\n i\n }\n }\n "]))),
|
227 | }))).toEqual({ e: 4, h: { i: 7 } });
|
228 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
229 | id: 'foo',
|
230 | fragment: graphql_tag_1.default(templateObject_15 || (templateObject_15 = tslib_1.__makeTemplateObject(["\n fragment fragmentFoo on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n "], ["\n fragment fragmentFoo on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n "]))),
|
231 | }))).toEqual({ e: 4, f: 5, g: 6, h: { i: 7, j: 8, k: 9 } });
|
232 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
233 | id: 'bar',
|
234 | fragment: graphql_tag_1.default(templateObject_16 || (templateObject_16 = tslib_1.__makeTemplateObject(["\n fragment fragmentBar on Bar {\n i\n }\n "], ["\n fragment fragmentBar on Bar {\n i\n }\n "]))),
|
235 | }))).toEqual({ i: 7 });
|
236 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
237 | id: 'bar',
|
238 | fragment: graphql_tag_1.default(templateObject_17 || (templateObject_17 = tslib_1.__makeTemplateObject(["\n fragment fragmentBar on Bar {\n i\n j\n k\n }\n "], ["\n fragment fragmentBar on Bar {\n i\n j\n k\n }\n "]))),
|
239 | }))).toEqual({ i: 7, j: 8, k: 9 });
|
240 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
241 | id: 'foo',
|
242 | fragment: graphql_tag_1.default(templateObject_18 || (templateObject_18 = tslib_1.__makeTemplateObject(["\n fragment fragmentFoo on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment fragmentBar on Bar {\n i\n j\n k\n }\n "], ["\n fragment fragmentFoo on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment fragmentBar on Bar {\n i\n j\n k\n }\n "]))),
|
243 | fragmentName: 'fragmentFoo',
|
244 | }))).toEqual({ e: 4, f: 5, g: 6, h: { i: 7, j: 8, k: 9 } });
|
245 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
246 | id: 'bar',
|
247 | fragment: graphql_tag_1.default(templateObject_19 || (templateObject_19 = tslib_1.__makeTemplateObject(["\n fragment fragmentFoo on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment fragmentBar on Bar {\n i\n j\n k\n }\n "], ["\n fragment fragmentFoo on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment fragmentBar on Bar {\n i\n j\n k\n }\n "]))),
|
248 | fragmentName: 'fragmentBar',
|
249 | }))).toEqual({ i: 7, j: 8, k: 9 });
|
250 | });
|
251 | itWithInitialData('will read some data from the store with variables', [
|
252 | {
|
253 | foo: {
|
254 | __typename: 'Foo',
|
255 | 'field({"literal":true,"value":42})': 1,
|
256 | 'field({"literal":false,"value":42})': 2,
|
257 | },
|
258 | },
|
259 | ], function (proxy) {
|
260 | expect(apollo_utilities_1.stripSymbols(proxy.readFragment({
|
261 | id: 'foo',
|
262 | fragment: graphql_tag_1.default(templateObject_20 || (templateObject_20 = tslib_1.__makeTemplateObject(["\n fragment foo on Foo {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "], ["\n fragment foo on Foo {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "]))),
|
263 | variables: {
|
264 | literal: false,
|
265 | value: 42,
|
266 | },
|
267 | }))).toEqual({ a: 1, b: 2 });
|
268 | });
|
269 | itWithInitialData('will return null when an id that can’t be found is provided', [
|
270 | {},
|
271 | {
|
272 | bar: { __typename: 'Bar', a: 1, b: 2, c: 3 },
|
273 | },
|
274 | {
|
275 | foo: { __typename: 'Foo', a: 1, b: 2, c: 3 },
|
276 | },
|
277 | ], function (client1, client2, client3) {
|
278 | expect(apollo_utilities_1.stripSymbols(client1.readFragment({
|
279 | id: 'foo',
|
280 | fragment: graphql_tag_1.default(templateObject_21 || (templateObject_21 = tslib_1.__makeTemplateObject(["\n fragment fooFragment on Foo {\n a\n b\n c\n }\n "], ["\n fragment fooFragment on Foo {\n a\n b\n c\n }\n "]))),
|
281 | }))).toEqual(null);
|
282 | expect(apollo_utilities_1.stripSymbols(client2.readFragment({
|
283 | id: 'foo',
|
284 | fragment: graphql_tag_1.default(templateObject_22 || (templateObject_22 = tslib_1.__makeTemplateObject(["\n fragment fooFragment on Foo {\n a\n b\n c\n }\n "], ["\n fragment fooFragment on Foo {\n a\n b\n c\n }\n "]))),
|
285 | }))).toEqual(null);
|
286 | expect(apollo_utilities_1.stripSymbols(client3.readFragment({
|
287 | id: 'foo',
|
288 | fragment: graphql_tag_1.default(templateObject_23 || (templateObject_23 = tslib_1.__makeTemplateObject(["\n fragment fooFragment on Foo {\n a\n b\n c\n }\n "], ["\n fragment fooFragment on Foo {\n a\n b\n c\n }\n "]))),
|
289 | }))).toEqual({ a: 1, b: 2, c: 3 });
|
290 | });
|
291 | });
|
292 | describe('writeQuery', function () {
|
293 | itWithInitialData('will write some data to the store', [{}], function (proxy) {
|
294 | proxy.writeQuery({
|
295 | data: { a: 1 },
|
296 | query: graphql_tag_1.default(templateObject_24 || (templateObject_24 = tslib_1.__makeTemplateObject(["\n {\n a\n }\n "], ["\n {\n a\n }\n "]))),
|
297 | });
|
298 | expect(proxy.extract()).toEqual({
|
299 | ROOT_QUERY: {
|
300 | a: 1,
|
301 | },
|
302 | });
|
303 | proxy.writeQuery({
|
304 | data: { b: 2, c: 3 },
|
305 | query: graphql_tag_1.default(templateObject_25 || (templateObject_25 = tslib_1.__makeTemplateObject(["\n {\n b\n c\n }\n "], ["\n {\n b\n c\n }\n "]))),
|
306 | });
|
307 | expect(proxy.extract()).toEqual({
|
308 | ROOT_QUERY: {
|
309 | a: 1,
|
310 | b: 2,
|
311 | c: 3,
|
312 | },
|
313 | });
|
314 | proxy.writeQuery({
|
315 | data: { a: 4, b: 5, c: 6 },
|
316 | query: graphql_tag_1.default(templateObject_26 || (templateObject_26 = tslib_1.__makeTemplateObject(["\n {\n a\n b\n c\n }\n "], ["\n {\n a\n b\n c\n }\n "]))),
|
317 | });
|
318 | expect(proxy.extract()).toEqual({
|
319 | ROOT_QUERY: {
|
320 | a: 4,
|
321 | b: 5,
|
322 | c: 6,
|
323 | },
|
324 | });
|
325 | });
|
326 | itWithInitialData('will write some deeply nested data to the store', [{}], function (proxy) {
|
327 | proxy.writeQuery({
|
328 | data: { a: 1, d: { e: 4 } },
|
329 | query: graphql_tag_1.default(templateObject_27 || (templateObject_27 = tslib_1.__makeTemplateObject(["\n {\n a\n d {\n e\n }\n }\n "], ["\n {\n a\n d {\n e\n }\n }\n "]))),
|
330 | });
|
331 | expect(proxy.extract()).toEqual({
|
332 | ROOT_QUERY: {
|
333 | a: 1,
|
334 | d: {
|
335 | type: 'id',
|
336 | id: '$ROOT_QUERY.d',
|
337 | generated: true,
|
338 | },
|
339 | },
|
340 | '$ROOT_QUERY.d': {
|
341 | e: 4,
|
342 | },
|
343 | });
|
344 | proxy.writeQuery({
|
345 | data: { a: 1, d: { h: { i: 7 } } },
|
346 | query: graphql_tag_1.default(templateObject_28 || (templateObject_28 = tslib_1.__makeTemplateObject(["\n {\n a\n d {\n h {\n i\n }\n }\n }\n "], ["\n {\n a\n d {\n h {\n i\n }\n }\n }\n "]))),
|
347 | });
|
348 | expect(proxy.extract()).toEqual({
|
349 | ROOT_QUERY: {
|
350 | a: 1,
|
351 | d: {
|
352 | type: 'id',
|
353 | id: '$ROOT_QUERY.d',
|
354 | generated: true,
|
355 | },
|
356 | },
|
357 | '$ROOT_QUERY.d': {
|
358 | e: 4,
|
359 | h: {
|
360 | type: 'id',
|
361 | id: '$ROOT_QUERY.d.h',
|
362 | generated: true,
|
363 | },
|
364 | },
|
365 | '$ROOT_QUERY.d.h': {
|
366 | i: 7,
|
367 | },
|
368 | });
|
369 | proxy.writeQuery({
|
370 | data: {
|
371 | a: 1,
|
372 | b: 2,
|
373 | c: 3,
|
374 | d: { e: 4, f: 5, g: 6, h: { i: 7, j: 8, k: 9 } },
|
375 | },
|
376 | query: graphql_tag_1.default(templateObject_29 || (templateObject_29 = tslib_1.__makeTemplateObject(["\n {\n a\n b\n c\n d {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n }\n "], ["\n {\n a\n b\n c\n d {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n }\n "]))),
|
377 | });
|
378 | expect(proxy.extract()).toEqual({
|
379 | ROOT_QUERY: {
|
380 | a: 1,
|
381 | b: 2,
|
382 | c: 3,
|
383 | d: {
|
384 | type: 'id',
|
385 | id: '$ROOT_QUERY.d',
|
386 | generated: true,
|
387 | },
|
388 | },
|
389 | '$ROOT_QUERY.d': {
|
390 | e: 4,
|
391 | f: 5,
|
392 | g: 6,
|
393 | h: {
|
394 | type: 'id',
|
395 | id: '$ROOT_QUERY.d.h',
|
396 | generated: true,
|
397 | },
|
398 | },
|
399 | '$ROOT_QUERY.d.h': {
|
400 | i: 7,
|
401 | j: 8,
|
402 | k: 9,
|
403 | },
|
404 | });
|
405 | });
|
406 | itWithInitialData('will write some data to the store with variables', [{}], function (proxy) {
|
407 | proxy.writeQuery({
|
408 | data: {
|
409 | a: 1,
|
410 | b: 2,
|
411 | },
|
412 | query: graphql_tag_1.default(templateObject_30 || (templateObject_30 = tslib_1.__makeTemplateObject(["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "], ["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "]))),
|
413 | variables: {
|
414 | literal: false,
|
415 | value: 42,
|
416 | },
|
417 | });
|
418 | expect(proxy.extract()).toEqual({
|
419 | ROOT_QUERY: {
|
420 | 'field({"literal":true,"value":42})': 1,
|
421 | 'field({"literal":false,"value":42})': 2,
|
422 | },
|
423 | });
|
424 | });
|
425 | itWithInitialData('will write some data to the store with variables where some are null', [{}], function (proxy) {
|
426 | proxy.writeQuery({
|
427 | data: {
|
428 | a: 1,
|
429 | b: 2,
|
430 | },
|
431 | query: graphql_tag_1.default(templateObject_31 || (templateObject_31 = tslib_1.__makeTemplateObject(["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "], ["\n query($literal: Boolean, $value: Int) {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "]))),
|
432 | variables: {
|
433 | literal: false,
|
434 | value: null,
|
435 | },
|
436 | });
|
437 | expect(proxy.extract()).toEqual({
|
438 | ROOT_QUERY: {
|
439 | 'field({"literal":true,"value":42})': 1,
|
440 | 'field({"literal":false,"value":null})': 2,
|
441 | },
|
442 | });
|
443 | });
|
444 | });
|
445 | describe('writeFragment', function () {
|
446 | itWithInitialData('will throw an error when there is no fragment', [{}], function (proxy) {
|
447 | expect(function () {
|
448 | proxy.writeFragment({
|
449 | data: {},
|
450 | id: 'x',
|
451 | fragment: graphql_tag_1.default(templateObject_32 || (templateObject_32 = tslib_1.__makeTemplateObject(["\n query {\n a\n b\n c\n }\n "], ["\n query {\n a\n b\n c\n }\n "]))),
|
452 | });
|
453 | }).toThrowError('Found a query operation. No operations are allowed when using a fragment as a query. Only fragments are allowed.');
|
454 | expect(function () {
|
455 | proxy.writeFragment({
|
456 | data: {},
|
457 | id: 'x',
|
458 | fragment: graphql_tag_1.default(templateObject_33 || (templateObject_33 = tslib_1.__makeTemplateObject(["\n schema {\n query: Query\n }\n "], ["\n schema {\n query: Query\n }\n "]))),
|
459 | });
|
460 | }).toThrowError('Found 0 fragments. `fragmentName` must be provided when there is not exactly 1 fragment.');
|
461 | });
|
462 | itWithInitialData('will throw an error when there is more than one fragment but no fragment name', [{}], function (proxy) {
|
463 | expect(function () {
|
464 | proxy.writeFragment({
|
465 | data: {},
|
466 | id: 'x',
|
467 | fragment: graphql_tag_1.default(templateObject_34 || (templateObject_34 = tslib_1.__makeTemplateObject(["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n "], ["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n "]))),
|
468 | });
|
469 | }).toThrowError('Found 2 fragments. `fragmentName` must be provided when there is not exactly 1 fragment.');
|
470 | expect(function () {
|
471 | proxy.writeFragment({
|
472 | data: {},
|
473 | id: 'x',
|
474 | fragment: graphql_tag_1.default(templateObject_35 || (templateObject_35 = tslib_1.__makeTemplateObject(["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n\n fragment c on C {\n c\n }\n "], ["\n fragment a on A {\n a\n }\n\n fragment b on B {\n b\n }\n\n fragment c on C {\n c\n }\n "]))),
|
475 | });
|
476 | }).toThrowError('Found 3 fragments. `fragmentName` must be provided when there is not exactly 1 fragment.');
|
477 | });
|
478 | itWithCacheConfig('will write some deeply nested data into the store at any id', {
|
479 | dataIdFromObject: function (o) { return o.id; },
|
480 | addTypename: false,
|
481 | }, function (proxy) {
|
482 | proxy.writeFragment({
|
483 | data: { __typename: 'Foo', e: 4, h: { id: 'bar', i: 7 } },
|
484 | id: 'foo',
|
485 | fragment: graphql_tag_1.default(templateObject_36 || (templateObject_36 = tslib_1.__makeTemplateObject(["\n fragment fragmentFoo on Foo {\n e\n h {\n i\n }\n }\n "], ["\n fragment fragmentFoo on Foo {\n e\n h {\n i\n }\n }\n "]))),
|
486 | });
|
487 | expect(proxy.extract()).toMatchSnapshot();
|
488 | proxy.writeFragment({
|
489 | data: { __typename: 'Foo', f: 5, g: 6, h: { id: 'bar', j: 8, k: 9 } },
|
490 | id: 'foo',
|
491 | fragment: graphql_tag_1.default(templateObject_37 || (templateObject_37 = tslib_1.__makeTemplateObject(["\n fragment fragmentFoo on Foo {\n f\n g\n h {\n j\n k\n }\n }\n "], ["\n fragment fragmentFoo on Foo {\n f\n g\n h {\n j\n k\n }\n }\n "]))),
|
492 | });
|
493 | expect(proxy.extract()).toMatchSnapshot();
|
494 | proxy.writeFragment({
|
495 | data: { i: 10, __typename: 'Bar' },
|
496 | id: 'bar',
|
497 | fragment: graphql_tag_1.default(templateObject_38 || (templateObject_38 = tslib_1.__makeTemplateObject(["\n fragment fragmentBar on Bar {\n i\n }\n "], ["\n fragment fragmentBar on Bar {\n i\n }\n "]))),
|
498 | });
|
499 | expect(proxy.extract()).toMatchSnapshot();
|
500 | proxy.writeFragment({
|
501 | data: { j: 11, k: 12, __typename: 'Bar' },
|
502 | id: 'bar',
|
503 | fragment: graphql_tag_1.default(templateObject_39 || (templateObject_39 = tslib_1.__makeTemplateObject(["\n fragment fragmentBar on Bar {\n j\n k\n }\n "], ["\n fragment fragmentBar on Bar {\n j\n k\n }\n "]))),
|
504 | });
|
505 | expect(proxy.extract()).toMatchSnapshot();
|
506 | proxy.writeFragment({
|
507 | data: {
|
508 | __typename: 'Foo',
|
509 | e: 4,
|
510 | f: 5,
|
511 | g: 6,
|
512 | h: { __typename: 'Bar', id: 'bar', i: 7, j: 8, k: 9 },
|
513 | },
|
514 | id: 'foo',
|
515 | fragment: graphql_tag_1.default(templateObject_40 || (templateObject_40 = tslib_1.__makeTemplateObject(["\n fragment fooFragment on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment barFragment on Bar {\n i\n j\n k\n }\n "], ["\n fragment fooFragment on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment barFragment on Bar {\n i\n j\n k\n }\n "]))),
|
516 | fragmentName: 'fooFragment',
|
517 | });
|
518 | expect(proxy.extract()).toMatchSnapshot();
|
519 | proxy.writeFragment({
|
520 | data: { __typename: 'Bar', i: 10, j: 11, k: 12 },
|
521 | id: 'bar',
|
522 | fragment: graphql_tag_1.default(templateObject_41 || (templateObject_41 = tslib_1.__makeTemplateObject(["\n fragment fooFragment on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment barFragment on Bar {\n i\n j\n k\n }\n "], ["\n fragment fooFragment on Foo {\n e\n f\n g\n h {\n i\n j\n k\n }\n }\n\n fragment barFragment on Bar {\n i\n j\n k\n }\n "]))),
|
523 | fragmentName: 'barFragment',
|
524 | });
|
525 | expect(proxy.extract()).toMatchSnapshot();
|
526 | });
|
527 | itWithCacheConfig('writes data that can be read back', {
|
528 | addTypename: true,
|
529 | }, function (proxy) {
|
530 | var readWriteFragment = graphql_tag_1.default(templateObject_42 || (templateObject_42 = tslib_1.__makeTemplateObject(["\n fragment aFragment on query {\n getSomething {\n id\n }\n }\n "], ["\n fragment aFragment on query {\n getSomething {\n id\n }\n }\n "])));
|
531 | var data = {
|
532 | __typename: 'query',
|
533 | getSomething: { id: '123', __typename: 'Something' },
|
534 | };
|
535 | proxy.writeFragment({
|
536 | data: data,
|
537 | id: 'query',
|
538 | fragment: readWriteFragment,
|
539 | });
|
540 | var result = proxy.readFragment({
|
541 | fragment: readWriteFragment,
|
542 | id: 'query',
|
543 | });
|
544 | expect(apollo_utilities_1.stripSymbols(result)).toEqual(data);
|
545 | });
|
546 | itWithCacheConfig('will write some data to the store with variables', {
|
547 | addTypename: true,
|
548 | }, function (proxy) {
|
549 | proxy.writeFragment({
|
550 | data: {
|
551 | a: 1,
|
552 | b: 2,
|
553 | __typename: 'Foo',
|
554 | },
|
555 | id: 'foo',
|
556 | fragment: graphql_tag_1.default(templateObject_43 || (templateObject_43 = tslib_1.__makeTemplateObject(["\n fragment foo on Foo {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "], ["\n fragment foo on Foo {\n a: field(literal: true, value: 42)\n b: field(literal: $literal, value: $value)\n }\n "]))),
|
557 | variables: {
|
558 | literal: false,
|
559 | value: 42,
|
560 | },
|
561 | });
|
562 | expect(proxy.extract()).toEqual({
|
563 | foo: {
|
564 | __typename: 'Foo',
|
565 | 'field({"literal":true,"value":42})': 1,
|
566 | 'field({"literal":false,"value":42})': 2,
|
567 | },
|
568 | });
|
569 | });
|
570 | });
|
571 | describe('performTransaction', function () {
|
572 | itWithInitialData('will not broadcast mid-transaction', [{}], function (cache) {
|
573 | var numBroadcasts = 0;
|
574 | var query = graphql_tag_1.default(templateObject_44 || (templateObject_44 = tslib_1.__makeTemplateObject(["\n {\n a\n }\n "], ["\n {\n a\n }\n "])));
|
575 | cache.watch({
|
576 | query: query,
|
577 | optimistic: false,
|
578 | callback: function () {
|
579 | numBroadcasts++;
|
580 | },
|
581 | });
|
582 | expect(numBroadcasts).toEqual(0);
|
583 | cache.performTransaction(function (proxy) {
|
584 | proxy.writeQuery({
|
585 | data: { a: 1 },
|
586 | query: query,
|
587 | });
|
588 | expect(numBroadcasts).toEqual(0);
|
589 | proxy.writeQuery({
|
590 | data: { a: 4, b: 5, c: 6 },
|
591 | query: graphql_tag_1.default(templateObject_45 || (templateObject_45 = tslib_1.__makeTemplateObject(["\n {\n a\n b\n c\n }\n "], ["\n {\n a\n b\n c\n }\n "]))),
|
592 | });
|
593 | expect(numBroadcasts).toEqual(0);
|
594 | });
|
595 | expect(numBroadcasts).toEqual(1);
|
596 | });
|
597 | });
|
598 | describe('performOptimisticTransaction', function () {
|
599 | itWithInitialData('will only broadcast once', [{}], function (cache) {
|
600 | var numBroadcasts = 0;
|
601 | var query = graphql_tag_1.default(templateObject_46 || (templateObject_46 = tslib_1.__makeTemplateObject(["\n {\n a\n }\n "], ["\n {\n a\n }\n "])));
|
602 | cache.watch({
|
603 | query: query,
|
604 | optimistic: true,
|
605 | callback: function () {
|
606 | numBroadcasts++;
|
607 | },
|
608 | });
|
609 | expect(numBroadcasts).toEqual(0);
|
610 | cache.recordOptimisticTransaction(function (proxy) {
|
611 | proxy.writeQuery({
|
612 | data: { a: 1 },
|
613 | query: query,
|
614 | });
|
615 | expect(numBroadcasts).toEqual(0);
|
616 | proxy.writeQuery({
|
617 | data: { a: 4, b: 5, c: 6 },
|
618 | query: graphql_tag_1.default(templateObject_47 || (templateObject_47 = tslib_1.__makeTemplateObject(["\n {\n a\n b\n c\n }\n "], ["\n {\n a\n b\n c\n }\n "]))),
|
619 | });
|
620 | expect(numBroadcasts).toEqual(0);
|
621 | }, 1);
|
622 | expect(numBroadcasts).toEqual(1);
|
623 | });
|
624 | });
|
625 | });
|
626 | 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, 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;
|
627 |
|
\ | No newline at end of file |