UNPKG

5.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var apollo_utilities_1 = require("apollo-utilities");
4var DataStore = (function () {
5 function DataStore(initialCache) {
6 this.cache = initialCache;
7 }
8 DataStore.prototype.getCache = function () {
9 return this.cache;
10 };
11 DataStore.prototype.markQueryResult = function (result, document, variables, fetchMoreForQueryId, ignoreErrors) {
12 if (ignoreErrors === void 0) { ignoreErrors = false; }
13 var writeWithErrors = !apollo_utilities_1.graphQLResultHasError(result);
14 if (ignoreErrors && apollo_utilities_1.graphQLResultHasError(result) && result.data) {
15 writeWithErrors = true;
16 }
17 if (!fetchMoreForQueryId && writeWithErrors) {
18 this.cache.write({
19 result: result.data,
20 dataId: 'ROOT_QUERY',
21 query: document,
22 variables: variables,
23 });
24 }
25 };
26 DataStore.prototype.markSubscriptionResult = function (result, document, variables) {
27 if (!apollo_utilities_1.graphQLResultHasError(result)) {
28 this.cache.write({
29 result: result.data,
30 dataId: 'ROOT_SUBSCRIPTION',
31 query: document,
32 variables: variables,
33 });
34 }
35 };
36 DataStore.prototype.markMutationInit = function (mutation) {
37 var _this = this;
38 if (mutation.optimisticResponse) {
39 var optimistic_1;
40 if (typeof mutation.optimisticResponse === 'function') {
41 optimistic_1 = mutation.optimisticResponse(mutation.variables);
42 }
43 else {
44 optimistic_1 = mutation.optimisticResponse;
45 }
46 this.cache.recordOptimisticTransaction(function (c) {
47 var orig = _this.cache;
48 _this.cache = c;
49 try {
50 _this.markMutationResult({
51 mutationId: mutation.mutationId,
52 result: { data: optimistic_1 },
53 document: mutation.document,
54 variables: mutation.variables,
55 updateQueries: mutation.updateQueries,
56 update: mutation.update,
57 });
58 }
59 finally {
60 _this.cache = orig;
61 }
62 }, mutation.mutationId);
63 }
64 };
65 DataStore.prototype.markMutationResult = function (mutation) {
66 var _this = this;
67 if (!apollo_utilities_1.graphQLResultHasError(mutation.result)) {
68 var cacheWrites_1 = [{
69 result: mutation.result.data,
70 dataId: 'ROOT_MUTATION',
71 query: mutation.document,
72 variables: mutation.variables,
73 }];
74 var updateQueries_1 = mutation.updateQueries;
75 if (updateQueries_1) {
76 Object.keys(updateQueries_1).forEach(function (id) {
77 var _a = updateQueries_1[id], query = _a.query, updater = _a.updater;
78 var _b = _this.cache.diff({
79 query: query.document,
80 variables: query.variables,
81 returnPartialData: true,
82 optimistic: false,
83 }), currentQueryResult = _b.result, complete = _b.complete;
84 if (complete) {
85 var nextQueryResult = apollo_utilities_1.tryFunctionOrLogError(function () {
86 return updater(currentQueryResult, {
87 mutationResult: mutation.result,
88 queryName: apollo_utilities_1.getOperationName(query.document) || undefined,
89 queryVariables: query.variables,
90 });
91 });
92 if (nextQueryResult) {
93 cacheWrites_1.push({
94 result: nextQueryResult,
95 dataId: 'ROOT_QUERY',
96 query: query.document,
97 variables: query.variables,
98 });
99 }
100 }
101 });
102 }
103 this.cache.performTransaction(function (c) {
104 cacheWrites_1.forEach(function (write) { return c.write(write); });
105 var update = mutation.update;
106 if (update) {
107 apollo_utilities_1.tryFunctionOrLogError(function () { return update(c, mutation.result); });
108 }
109 });
110 }
111 };
112 DataStore.prototype.markMutationComplete = function (_a) {
113 var mutationId = _a.mutationId, optimisticResponse = _a.optimisticResponse;
114 if (optimisticResponse) {
115 this.cache.removeOptimistic(mutationId);
116 }
117 };
118 DataStore.prototype.markUpdateQueryResult = function (document, variables, newResult) {
119 this.cache.write({
120 result: newResult,
121 dataId: 'ROOT_QUERY',
122 variables: variables,
123 query: document,
124 });
125 };
126 DataStore.prototype.reset = function () {
127 return this.cache.reset();
128 };
129 return DataStore;
130}());
131exports.DataStore = DataStore;
132//# sourceMappingURL=store.js.map
\No newline at end of file