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