UNPKG

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