UNPKG

17.2 kBJavaScriptView Raw
1function p(a, b) {
2 void 0 === b && (b = []);
3 if (Array.isArray(a)) {
4 a.forEach((function c(a) {
5 p(a, b);
6 }));
7 } else if ("object" == typeof a && null !== a) {
8 for (var d in a) {
9 "__typename" === d && "string" == typeof a[d] ? b.push(a[d]) : p(a[d], b);
10 }
11 }
12 return b;
13}
14
15function q(a, b, c) {
16 return c.indexOf(a) === b;
17}
18
19function r(a) {
20 return a.kind === Kind.FIELD && "__typename" === a.name.value;
21}
22
23function t(a) {
24 if (a.selectionSet && !a.selectionSet.selections.some(r)) {
25 return _extends({}, a, {
26 selectionSet: _extends({}, a.selectionSet, {
27 selections: a.selectionSet.selections.concat([ {
28 kind: Kind.FIELD,
29 name: {
30 kind: Kind.NAME,
31 value: "__typename"
32 }
33 } ])
34 })
35 });
36 }
37}
38
39function u(a) {
40 var b = visit(a, {
41 Field: t,
42 InlineFragment: t
43 });
44 b.__key = a.__key;
45 return b;
46}
47
48function v(a) {
49 return a && "object" == typeof a ? Object.keys(a).reduce((function(b, c) {
50 var d = a[c];
51 "__typename" === c ? Object.defineProperty(b, "__typename", {
52 enumerable: !1,
53 value: d
54 }) : Array.isArray(d) ? b[c] = d.map(v) : b[c] = d && "object" == typeof d && "__typename" in d ? v(d) : d;
55 return b;
56 }), {}) : a;
57}
58
59function w(a) {
60 a.toPromise = function() {
61 return toPromise(take(1)(a));
62 };
63 return a;
64}
65
66function z(a, b, c) {
67 c || (c = b.context);
68 return {
69 key: b.key,
70 query: b.query,
71 variables: b.variables,
72 kind: a,
73 context: c,
74 get operationName() {
75 if ("production" !== process.env.NODE_ENV) {
76 var a = y_operationName, b = a.key;
77 a = a.message;
78 x[b] || (console.warn("[WARNING: Deprecated] " + a), x[b] = !0);
79 }
80 return this.kind;
81 }
82 };
83}
84
85function A(a, b) {
86 return z(a.kind, a, _extends({}, a.context, {
87 meta: _extends({}, a.context.meta, b)
88 }));
89}
90
91function B() {}
92
93function C(a) {
94 return "subscription" !== (a = a.kind) && "query" !== a;
95}
96
97function D(a) {
98 return a.path || a.extensions ? {
99 message: a.message,
100 path: a.path,
101 extensions: a.extensions
102 } : a.message;
103}
104
105function E(a) {
106 return "mutation" !== (a = a.kind) && "query" !== a;
107}
108
109function F(a) {
110 var b = z(a.kind, a);
111 b.query = u(a.query);
112 return b;
113}
114
115function G(a) {
116 return "query" !== a.kind || "cache-only" !== a.context.requestPolicy;
117}
118
119function H(a) {
120 return A(a, {
121 cacheOutcome: "miss"
122 });
123}
124
125function I(a) {
126 return E(a);
127}
128
129function J(a) {
130 function b(a) {
131 var c = a.context.requestPolicy;
132 return "query" === a.kind && "network-only" !== c && ("cache-only" === c || f.has(a.key));
133 }
134 function c(a) {
135 var b = f.get(a.key);
136 "production" !== process.env.NODE_ENV && k(_extends({}, {
137 operation: a
138 }, b ? {
139 type: "cacheHit",
140 message: "The result was successfully retried from the cache"
141 } : {
142 type: "cacheMiss",
143 message: "The result could not be retrieved from the cache"
144 }));
145 b = _extends({}, b, {
146 operation: A(a, {
147 cacheOutcome: b ? "hit" : "miss"
148 })
149 });
150 "cache-and-network" === a.context.requestPolicy && (b.stale = !0, K(l, a));
151 return b;
152 }
153 function d(a) {
154 return !E(a) && b(a);
155 }
156 function e(a) {
157 a.operation && "mutation" === a.operation.kind ? g(a) : a.operation && "query" === a.operation.kind && m(a);
158 }
159 function h(a) {
160 return !E(a) && !b(a);
161 }
162 var g, m, n = a.forward, l = a.client, k = a.dispatchDebug, f = new Map;
163 a = Object.create(null);
164 g = function L(a, b, c, d) {
165 function e(b) {
166 if (a.has(b)) {
167 var d = a.get(b).operation;
168 a.delete(b);
169 K(c, d);
170 }
171 }
172 return function(a) {
173 function c(a) {
174 l.add(a);
175 }
176 var l = new Set, k = a.operation.context.additionalTypenames;
177 k = p(a.data).filter(q).concat(k || []);
178 "production" !== process.env.NODE_ENV && d({
179 type: "cacheInvalidation",
180 message: "The following typenames have been invalidated: " + k,
181 operation: a.operation,
182 data: {
183 typenames: k,
184 response: a
185 },
186 source: "cacheExchange"
187 });
188 k.forEach((function(a) {
189 (a = b[a] || (b[a] = new Set)).forEach(c);
190 a.clear();
191 }));
192 l.forEach(e);
193 };
194 }(f, a, l, k), m = function M(a, b) {
195 return function(c) {
196 var d = c.operation, e = c.data, h = d.context.additionalTypenames;
197 null != e && (a.set(d.key, {
198 operation: d,
199 data: e,
200 error: c.error
201 }), p(c.data).filter(q).concat(h || []).forEach((function(a) {
202 (b[a] || (b[a] = new Set)).add(d.key);
203 })));
204 };
205 }(f, a);
206 return function(a) {
207 var b = share(a);
208 a = map(c)(filter(d)(b));
209 b = tap(e)(n(filter(G)(map(H)(merge([ map(F)(filter(h)(b)), filter(I)(b) ])))));
210 return merge([ a, b ]);
211 };
212}
213
214function K(a, b) {
215 return a.reexecuteOperation(z(b.kind, b, _extends({}, b.context, {
216 requestPolicy: "network-only"
217 })));
218}
219
220function N(a) {
221 return console.log("[Exchange debug]: Completed operation: ", a);
222}
223
224function O(a) {
225 return console.log("[Exchange debug]: Incoming operation: ", a);
226}
227
228function P(a) {
229 function b(a) {
230 h.delete(a.operation.key);
231 }
232 function c(a) {
233 var b = a.key, c = a.kind;
234 if ("teardown" === c) {
235 return h.delete(b), !0;
236 }
237 if ("query" !== c && "subscription" !== c) {
238 return !0;
239 }
240 c = h.has(b);
241 h.add(b);
242 c && "production" !== process.env.NODE_ENV && e({
243 type: "dedup",
244 message: "An operation has been deduped.",
245 operation: a,
246 source: "dedupExchange"
247 });
248 return !c;
249 }
250 var d = a.forward, e = a.dispatchDebug, h = new Set;
251 return function(a) {
252 a = filter(c)(a);
253 return tap(b)(d(a));
254 };
255}
256
257function Q(a) {
258 return "query" === a.kind || "mutation" === a.kind;
259}
260
261function R(a) {
262 return "query" !== a.kind && "mutation" !== a.kind;
263}
264
265function S(a) {
266 var b = a.forward, c = a.dispatchDebug;
267 return function(a) {
268 var h, d = share(a);
269 a = mergeMap((function(a) {
270 var b = a.key, e = filter((function(a) {
271 return "teardown" === a.kind && a.key === b;
272 }))(d), f = makeFetchBody(a), g = makeFetchURL(a, f), m = makeFetchOptions(a, f);
273 "production" !== process.env.NODE_ENV && c({
274 type: "fetchRequest",
275 message: "A fetch request is being executed.",
276 operation: a,
277 data: {
278 url: g,
279 fetchOptions: m
280 },
281 source: "fetchExchange"
282 });
283 return onPush((function(b) {
284 var d = b.data ? void 0 : b.error;
285 "production" !== process.env.NODE_ENV && c({
286 type: d ? "fetchError" : "fetchSuccess",
287 message: "A " + (d ? "failed" : "successful") + " fetch response has been returned.",
288 operation: a,
289 data: {
290 url: g,
291 fetchOptions: m,
292 value: d || b
293 },
294 source: "fetchExchange"
295 });
296 }))(takeUntil(e)(makeFetchSource(a, g, m)));
297 }))(filter(Q)(d));
298 h = b(filter(R)(d));
299 return merge([ a, h ]);
300 };
301}
302
303function T() {
304 return !1;
305}
306
307function U(a) {
308 function b(a) {
309 if ("teardown" !== a.kind && "production" !== process.env.NODE_ENV) {
310 var b = 'No exchange has handled operations of kind "' + a.kind + "\". Check whether you've added an exchange responsible for these operations.";
311 "production" !== process.env.NODE_ENV && c({
312 type: "fallbackCatch",
313 message: b,
314 operation: a,
315 source: "fallbackExchange"
316 });
317 console.warn(b);
318 }
319 }
320 var c = a.dispatchDebug;
321 return function(a) {
322 return filter(T)(tap(b)(a));
323 };
324}
325
326function W(a) {
327 return function(b) {
328 var c = b.client, d = b.dispatchDebug;
329 return a.reduceRight((function(a, b) {
330 return b({
331 client: c,
332 forward: a,
333 dispatchDebug: function(a) {
334 "production" !== process.env.NODE_ENV && d(_extends({}, {
335 timestamp: Date.now(),
336 source: b.name
337 }, a));
338 }
339 });
340 }), b.forward);
341 };
342}
343
344function Y(a) {
345 var d, e, h, n, l, c = this;
346 this.activeOperations = Object.create(null);
347 this.queue = [];
348 this.createOperationContext = function(a) {
349 a || (a = {});
350 return _extends({}, {
351 url: c.url,
352 fetchOptions: c.fetchOptions,
353 fetch: c.fetch,
354 preferGetMethod: c.preferGetMethod
355 }, a, {
356 suspense: a.suspense || !1 !== a.suspense && c.suspense,
357 requestPolicy: a.requestPolicy || c.requestPolicy
358 });
359 };
360 this.createRequestOperation = function(a, b, d) {
361 return z(a, b, c.createOperationContext(d));
362 };
363 this.executeQuery = function(a, b) {
364 a = c.createRequestOperation("query", a, b);
365 return c.executeRequestOperation(a);
366 };
367 this.executeSubscription = function(a, b) {
368 a = c.createRequestOperation("subscription", a, b);
369 return c.executeRequestOperation(a);
370 };
371 this.executeMutation = function(a, b) {
372 a = c.createRequestOperation("mutation", a, b);
373 return c.executeRequestOperation(a);
374 };
375 if ("production" !== process.env.NODE_ENV && !a.url) {
376 throw Error("You are creating an urql-client without a url.");
377 }
378 d = B;
379 if ("production" !== process.env.NODE_ENV) {
380 e = (d = makeSubject()).next, h = d.source;
381 this.subscribeToDebugTarget = function b(a) {
382 return subscribe(a)(h);
383 };
384 d = e;
385 }
386 this.url = a.url;
387 this.fetchOptions = a.fetchOptions;
388 this.fetch = a.fetch;
389 this.suspense = !!a.suspense;
390 this.requestPolicy = a.requestPolicy || "cache-first";
391 this.preferGetMethod = !!a.preferGetMethod;
392 this.maskTypename = !!a.maskTypename;
393 e = makeSubject();
394 n = e.next;
395 this.operations$ = e.source;
396 l = !1;
397 this.dispatchOperation = function(a) {
398 l = !0;
399 for (a && n(a); a = c.queue.shift(); ) {
400 n(a);
401 }
402 l = !1;
403 };
404 this.reexecuteOperation = function(a) {
405 if ("mutation" === a.kind || 0 < (c.activeOperations[a.key] || 0)) {
406 c.queue.push(a), l || Promise.resolve().then(c.dispatchOperation);
407 }
408 };
409 a = W(void 0 !== a.exchanges ? a.exchanges : X);
410 this.results$ = share(a({
411 client: this,
412 dispatchDebug: d,
413 forward: U({
414 dispatchDebug: d
415 })
416 })(this.operations$));
417 publish(this.results$);
418}
419
420function Z(a) {
421 a.data = v(a.data);
422 return a;
423}
424
425function createClient(a) {
426 return new Y(a);
427}
428
429function debugExchange(a) {
430 var d = a.forward;
431 return "production" === process.env.NODE_ENV ? function b(a) {
432 return d(a);
433 } : function c(a) {
434 return tap(N)(d(tap(O)(a)));
435 };
436}
437
438function errorExchange(a) {
439 function b(a) {
440 var b = a.error;
441 a = a.operation;
442 b && c(b, a);
443 }
444 var c = a.onError;
445 return function(a) {
446 var c = a.forward;
447 return function(a) {
448 return tap(b)(c(a));
449 };
450 };
451}
452
453function ssrExchange(a) {
454 function b(b) {
455 var c = b.client, m = b.forward;
456 return function(b) {
457 var e = a && "boolean" == typeof a.isClient ? !!a.isClient : !c.suspense, g = share(b);
458 b = m(filter(h)(g));
459 g = map(n)(filter(l)(g));
460 e ? g = tap(d)(g) : b = tap(k)(b);
461 return merge([ b, g ]);
462 };
463 }
464 function c(a) {
465 return !C(a) && void 0 !== f[a.key];
466 }
467 function d(a) {
468 g.push(a.operation.key);
469 1 === g.length && Promise.resolve().then(e);
470 }
471 function e() {
472 for (var a; a = g.shift(); ) {
473 delete f[a];
474 }
475 }
476 function h(a) {
477 return !c(a);
478 }
479 function n(a) {
480 var b = f[a.key], c = b.error;
481 return {
482 operation: a,
483 data: (b = b.data) ? JSON.parse(b) : void 0,
484 extensions: void 0,
485 error: c ? new CombinedError({
486 networkError: c.networkError ? Error(c.networkError) : void 0,
487 graphQLErrors: c.graphQLErrors && c.graphQLErrors.length ? c.graphQLErrors : void 0
488 }) : void 0
489 };
490 }
491 function l(a) {
492 return c(a);
493 }
494 function k(a) {
495 var c, b = a.operation;
496 if (!C(b)) {
497 c = a.error;
498 a = {
499 data: JSON.stringify(a.data),
500 error: void 0
501 };
502 c && (a.error = {
503 graphQLErrors: c.graphQLErrors.map(D),
504 networkError: c.networkError ? "" + c.networkError : void 0
505 });
506 f[b.key] = a;
507 }
508 }
509 var f = {}, g = [];
510 b.restoreData = function(a) {
511 return _extends(f, a);
512 };
513 b.extractData = function() {
514 return _extends({}, f);
515 };
516 a && a.initialState && b.restoreData(a.initialState);
517 return b;
518}
519
520function subscriptionExchange(a) {
521 function b(a) {
522 return "subscription" === (a = a.kind) || !!d && ("query" === a || "mutation" === a);
523 }
524 var c = a.forwardSubscription, d = a.enableAllOperations;
525 return function(a) {
526 function d(a) {
527 var b = c({
528 key: a.key.toString(36),
529 query: print(a.query),
530 variables: a.variables,
531 context: _extends({}, a.context)
532 });
533 return make((function(c) {
534 function d(b) {
535 return f(makeResult(a, b));
536 }
537 function e(b) {
538 return f(makeErrorResult(a, b));
539 }
540 function g() {
541 k || (k = !0, "subscription" === a.kind && l.reexecuteOperation(z("teardown", a, a.context)),
542 h());
543 }
544 var m, f = c.next, h = c.complete, k = !1;
545 Promise.resolve().then((function() {
546 k || (m = b.subscribe({
547 next: d,
548 error: e,
549 complete: g
550 }));
551 }));
552 return function() {
553 k = !0;
554 m && m.unsubscribe();
555 };
556 }));
557 }
558 function e(a) {
559 return !f(a);
560 }
561 var l = a.client, k = a.forward, f = b;
562 return function(a) {
563 var c, b = share(a);
564 a = mergeMap((function(a) {
565 var c = a.key, e = filter((function(a) {
566 return "teardown" === a.kind && a.key === c;
567 }))(b);
568 return takeUntil(e)(d(a));
569 }))(filter(f)(b));
570 c = k(filter(e)(b));
571 return merge([ a, c ]);
572 };
573 };
574}
575
576var x, y_operationName, V, X;
577
578import { visit } from "graphql/language/visitor.mjs";
579
580import { Kind } from "graphql/language/kinds.mjs";
581
582import { print } from "graphql/language/printer.mjs";
583
584import { toPromise, take, share, filter, map, tap, merge, mergeMap, takeUntil, make, onPush, makeSubject, publish, onStart, onEnd, switchMap, fromValue, interval, subscribe } from "wonka";
585
586import { _ as _extends, C as CombinedError, m as makeResult, a as makeErrorResult, b as makeFetchBody, c as makeFetchURL, d as makeFetchOptions, e as makeFetchSource, f as createRequest } from "./231258ed.mjs";
587
588export { C as CombinedError, f as createRequest, a as makeErrorResult, m as makeResult, s as stringifyVariables } from "./231258ed.mjs";
589
590x = {}, y_operationName = {
591 key: "Operation.operationName",
592 message: 'The "Operation.operationName" property has been deprecated and will be removed in a future release of urql. Use "Operation.kind" instead.'
593};
594
595V = U({
596 dispatchDebug: B
597});
598
599X = [ P, J, S ];
600
601Y.prototype.onOperationStart = function(a) {
602 var b = a.key;
603 this.activeOperations[b] = (this.activeOperations[b] || 0) + 1;
604 this.dispatchOperation(a);
605};
606
607Y.prototype.onOperationEnd = function(a) {
608 var b = a.key, c = this.activeOperations[b] || 0;
609 if (0 >= (this.activeOperations[b] = 0 >= c ? 0 : c - 1)) {
610 for (b = this.queue.length - 1; 0 <= b; b--) {
611 this.queue[b].key === a.key && this.queue.splice(b, 1);
612 }
613 this.dispatchOperation(z("teardown", a, a.context));
614 }
615};
616
617Y.prototype.executeRequestOperation = function(a) {
618 var h, n, d = this, e = filter((function(b) {
619 return b.operation.key === a.key;
620 }))(this.results$);
621 this.maskTypename && (e = map(Z)(e));
622 if ("mutation" === a.kind) {
623 return take(1)(onStart((function b() {
624 return d.dispatchOperation(a);
625 }))(e));
626 }
627 h = filter((function(b) {
628 return "teardown" === b.kind && b.key === a.key;
629 }))(this.operations$), n = onEnd((function() {
630 d.onOperationEnd(a);
631 }))(onStart((function() {
632 d.onOperationStart(a);
633 }))(takeUntil(h)(e)));
634 return "query" === a.kind && a.context.pollInterval ? switchMap((function c() {
635 return n;
636 }))(merge([ fromValue(0), interval(a.context.pollInterval) ])) : n;
637};
638
639Y.prototype.query = function(a, b, c) {
640 c && "boolean" == typeof c.suspense || (c = _extends({}, c, {
641 suspense: !1
642 }));
643 return w(this.executeQuery(createRequest(a, b), c));
644};
645
646Y.prototype.readQuery = function(a, b, c) {
647 var d = null;
648 subscribe((function(a) {
649 d = a;
650 }))(this.executeQuery(createRequest(a, b), c)).unsubscribe();
651 return d;
652};
653
654Y.prototype.subscription = function(a, b, c) {
655 return this.executeSubscription(createRequest(a, b), c);
656};
657
658Y.prototype.mutation = function(a, b, c) {
659 return w(this.executeMutation(createRequest(a, b), c));
660};
661
662export { Y as Client, J as cacheExchange, W as composeExchanges, createClient, debugExchange, P as dedupExchange, X as defaultExchanges, errorExchange, V as fallbackExchangeIO, S as fetchExchange, u as formatDocument, z as makeOperation, v as maskTypename, ssrExchange, subscriptionExchange };
663//# sourceMappingURL=urql-core.mjs.map