UNPKG

2.05 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = createRequestHandler;
7
8var _relayRuntime = require("relay-runtime");
9
10var _helpers = require("./helpers");
11
12function createRequestHandler(customFetcher) {
13 var burstCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new _relayRuntime.QueryResponseCache({
14 size: 250,
15 ttl: 2 * 1000 // 2 seconds
16
17 });
18
19 function cleanup() {// noop, do anything here
20 }
21
22 return function handleRequest(requestNode, variables, cacheConfig, uploadables) {
23 return _relayRuntime.Observable.create(function (sink) {
24 var queryID = requestNode.text;
25
26 if ((0, _helpers.isMutation)(requestNode)) {
27 // mutations should always erase burst cache
28 burstCache.clear();
29 } else {
30 // otherwise we'll try to read from the burst cache and return without
31 // any additional fetching
32 var fromCache = burstCache.get(queryID, variables);
33
34 if ((0, _helpers.isQuery)(requestNode) && fromCache !== null && !(0, _helpers.forceFetch)(cacheConfig)) {
35 sink.next(fromCache);
36 sink.complete();
37 return cleanup; // that's it - we are done here (no network call)
38 }
39 } // this should be executed only when the request is a mutation or
40 // when there is no content in the burst cache
41
42
43 customFetcher(requestNode, variables, uploadables).then(function (response) {
44 if (response.errors) {
45 // What should we do with these partial errors?
46 // eslint-disable-next-line no-console
47 response.errors.map(function (error) {
48 return console.error(error.message, error);
49 });
50 } else {
51 // set burst cache only if there are no errors
52 burstCache.set(queryID, variables, response);
53 }
54
55 sink.next(response);
56 sink.complete();
57 }).catch(function (error) {
58 sink.error(error);
59 sink.complete();
60 });
61 return cleanup;
62 });
63 };
64}
\No newline at end of file