UNPKG

4.76 kBJavaScriptView Raw
1var __assign = (this && this.__assign) || function () {
2 __assign = Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9 };
10 return __assign.apply(this, arguments);
11};
12import { print } from 'graphql/language/printer';
13import { isEqual } from 'apollo-utilities';
14import { NetworkStatus } from '../core/networkStatus';
15var QueryStore = (function () {
16 function QueryStore() {
17 this.store = {};
18 }
19 QueryStore.prototype.getStore = function () {
20 return this.store;
21 };
22 QueryStore.prototype.get = function (queryId) {
23 return this.store[queryId];
24 };
25 QueryStore.prototype.initQuery = function (query) {
26 var previousQuery = this.store[query.queryId];
27 if (previousQuery &&
28 previousQuery.document !== query.document &&
29 print(previousQuery.document) !== print(query.document)) {
30 throw new Error('Internal Error: may not update existing query string in store');
31 }
32 var isSetVariables = false;
33 var previousVariables = null;
34 if (query.storePreviousVariables &&
35 previousQuery &&
36 previousQuery.networkStatus !== NetworkStatus.loading) {
37 if (!isEqual(previousQuery.variables, query.variables)) {
38 isSetVariables = true;
39 previousVariables = previousQuery.variables;
40 }
41 }
42 var networkStatus;
43 if (isSetVariables) {
44 networkStatus = NetworkStatus.setVariables;
45 }
46 else if (query.isPoll) {
47 networkStatus = NetworkStatus.poll;
48 }
49 else if (query.isRefetch) {
50 networkStatus = NetworkStatus.refetch;
51 }
52 else {
53 networkStatus = NetworkStatus.loading;
54 }
55 var graphQLErrors = [];
56 if (previousQuery && previousQuery.graphQLErrors) {
57 graphQLErrors = previousQuery.graphQLErrors;
58 }
59 this.store[query.queryId] = {
60 document: query.document,
61 variables: query.variables,
62 previousVariables: previousVariables,
63 networkError: null,
64 graphQLErrors: graphQLErrors,
65 networkStatus: networkStatus,
66 metadata: query.metadata,
67 };
68 if (typeof query.fetchMoreForQueryId === 'string' &&
69 this.store[query.fetchMoreForQueryId]) {
70 this.store[query.fetchMoreForQueryId].networkStatus =
71 NetworkStatus.fetchMore;
72 }
73 };
74 QueryStore.prototype.markQueryResult = function (queryId, result, fetchMoreForQueryId) {
75 if (!this.store || !this.store[queryId])
76 return;
77 this.store[queryId].networkError = null;
78 this.store[queryId].graphQLErrors =
79 result.errors && result.errors.length ? result.errors : [];
80 this.store[queryId].previousVariables = null;
81 this.store[queryId].networkStatus = NetworkStatus.ready;
82 if (typeof fetchMoreForQueryId === 'string' &&
83 this.store[fetchMoreForQueryId]) {
84 this.store[fetchMoreForQueryId].networkStatus = NetworkStatus.ready;
85 }
86 };
87 QueryStore.prototype.markQueryError = function (queryId, error, fetchMoreForQueryId) {
88 if (!this.store || !this.store[queryId])
89 return;
90 this.store[queryId].networkError = error;
91 this.store[queryId].networkStatus = NetworkStatus.error;
92 if (typeof fetchMoreForQueryId === 'string') {
93 this.markQueryResultClient(fetchMoreForQueryId, true);
94 }
95 };
96 QueryStore.prototype.markQueryResultClient = function (queryId, complete) {
97 if (!this.store || !this.store[queryId])
98 return;
99 this.store[queryId].networkError = null;
100 this.store[queryId].previousVariables = null;
101 this.store[queryId].networkStatus = complete
102 ? NetworkStatus.ready
103 : NetworkStatus.loading;
104 };
105 QueryStore.prototype.stopQuery = function (queryId) {
106 delete this.store[queryId];
107 };
108 QueryStore.prototype.reset = function (observableQueryIds) {
109 var _this = this;
110 this.store = Object.keys(this.store)
111 .filter(function (queryId) {
112 return observableQueryIds.indexOf(queryId) > -1;
113 })
114 .reduce(function (res, key) {
115 res[key] = __assign({}, _this.store[key], { networkStatus: NetworkStatus.loading });
116 return res;
117 }, {});
118 };
119 return QueryStore;
120}());
121export { QueryStore };
122//# sourceMappingURL=queries.js.map
\No newline at end of file