1 | "use strict";
|
2 | var __extends = (this && this.__extends) || function (d, b) {
|
3 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
4 | function __() { this.constructor = d; }
|
5 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
6 | };
|
7 | var Observable_1 = require('./util/Observable');
|
8 | var errors_1 = require('./errors');
|
9 | var errorHandling_1 = require('./util/errorHandling');
|
10 | var assign = require('lodash.assign');
|
11 | var isEqual = require('lodash.isequal');
|
12 | var ObservableQuery = (function (_super) {
|
13 | __extends(ObservableQuery, _super);
|
14 | function ObservableQuery(_a) {
|
15 | var _this = this;
|
16 | var scheduler = _a.scheduler, options = _a.options, _b = _a.shouldSubscribe, shouldSubscribe = _b === void 0 ? true : _b;
|
17 | var queryManager = scheduler.queryManager;
|
18 | var queryId = queryManager.generateQueryId();
|
19 | var isPollingQuery = !!options.pollInterval;
|
20 | var subscriberFunction = function (observer) {
|
21 | var retQuerySubscription = {
|
22 | unsubscribe: function () {
|
23 | if (isPollingQuery) {
|
24 | scheduler.stopPollingQuery(queryId);
|
25 | }
|
26 | queryManager.stopQuery(queryId);
|
27 | },
|
28 | };
|
29 | if (shouldSubscribe) {
|
30 | queryManager.addObservableQuery(queryId, _this);
|
31 | queryManager.addQuerySubscription(queryId, retQuerySubscription);
|
32 | }
|
33 | if (isPollingQuery) {
|
34 | if (_this.options.noFetch) {
|
35 | throw new Error('noFetch option should not use query polling.');
|
36 | }
|
37 | _this.scheduler.startPollingQuery(_this.options, queryId);
|
38 | }
|
39 | queryManager.startQuery(queryId, _this.options, queryManager.queryListenerForObserver(queryId, _this.options, observer));
|
40 | return retQuerySubscription;
|
41 | };
|
42 | _super.call(this, subscriberFunction);
|
43 | this.options = options;
|
44 | this.variables = this.options.variables || {};
|
45 | this.scheduler = scheduler;
|
46 | this.queryManager = queryManager;
|
47 | this.queryId = queryId;
|
48 | this.refetch = function (variables) {
|
49 | _this.variables = assign({}, _this.variables, variables);
|
50 | if (_this.options.noFetch) {
|
51 | throw new Error('noFetch option should not use query refetch.');
|
52 | }
|
53 | return _this.queryManager.fetchQuery(_this.queryId, assign(_this.options, {
|
54 | forceFetch: true,
|
55 | variables: _this.variables,
|
56 | }))
|
57 | .then(function (result) { return _this.queryManager.transformResult(result); });
|
58 | };
|
59 | this.setOptions = function (opts) {
|
60 | _this.options = assign({}, _this.options, opts);
|
61 | if (opts.pollInterval) {
|
62 | _this.startPolling(opts.pollInterval);
|
63 | }
|
64 | else if (opts.pollInterval === 0) {
|
65 | _this.stopPolling();
|
66 | }
|
67 | return _this.setVariables(opts.variables);
|
68 | };
|
69 | this._setOptionsNoResult = function (opts) {
|
70 | _this.options = assign({}, _this.options, opts);
|
71 | if (opts.pollInterval) {
|
72 | _this.startPolling(opts.pollInterval);
|
73 | }
|
74 | else if (opts.pollInterval === 0) {
|
75 | _this.stopPolling();
|
76 | }
|
77 | _this._setVariablesNoResult(opts.variables);
|
78 | };
|
79 | this.setVariables = function (variables) {
|
80 | var newVariables = assign({}, _this.variables, variables);
|
81 | if (isEqual(newVariables, _this.variables)) {
|
82 | return _this.result();
|
83 | }
|
84 | else {
|
85 | _this.variables = newVariables;
|
86 | return _this.queryManager.fetchQuery(_this.queryId, assign(_this.options, {
|
87 | variables: _this.variables,
|
88 | }))
|
89 | .then(function (result) { return _this.queryManager.transformResult(result); });
|
90 | }
|
91 | };
|
92 | this._setVariablesNoResult = function (variables) {
|
93 | var newVariables = assign({}, _this.variables, variables);
|
94 | if (isEqual(newVariables, _this.variables)) {
|
95 | return;
|
96 | }
|
97 | else {
|
98 | _this.variables = newVariables;
|
99 | _this.queryManager.fetchQuery(_this.queryId, assign(_this.options, {
|
100 | variables: _this.variables,
|
101 | }));
|
102 | }
|
103 | };
|
104 | this.fetchMore = function (fetchMoreOptions) {
|
105 | return Promise.resolve()
|
106 | .then(function () {
|
107 | var qid = _this.queryManager.generateQueryId();
|
108 | var combinedOptions = null;
|
109 | if (fetchMoreOptions.query) {
|
110 | combinedOptions = fetchMoreOptions;
|
111 | }
|
112 | else {
|
113 | var variables = assign({}, _this.variables, fetchMoreOptions.variables);
|
114 | combinedOptions = assign({}, _this.options, fetchMoreOptions, {
|
115 | variables: variables,
|
116 | });
|
117 | }
|
118 | combinedOptions = assign({}, combinedOptions, {
|
119 | forceFetch: true,
|
120 | });
|
121 | return _this.queryManager.fetchQuery(qid, combinedOptions);
|
122 | })
|
123 | .then(function (fetchMoreResult) {
|
124 | var reducer = fetchMoreOptions.updateQuery;
|
125 | var mapFn = function (previousResult, _a) {
|
126 | var queryVariables = _a.queryVariables;
|
127 | return reducer(previousResult, {
|
128 | fetchMoreResult: fetchMoreResult,
|
129 | queryVariables: queryVariables,
|
130 | });
|
131 | };
|
132 | _this.updateQuery(mapFn);
|
133 | return fetchMoreResult;
|
134 | });
|
135 | };
|
136 | this.updateQuery = function (mapFn) {
|
137 | var _a = _this.queryManager.getQueryWithPreviousResult(_this.queryId), previousResult = _a.previousResult, queryVariables = _a.queryVariables, querySelectionSet = _a.querySelectionSet, _b = _a.queryFragments, queryFragments = _b === void 0 ? [] : _b;
|
138 | var newResult = errorHandling_1.tryFunctionOrLogError(function () { return mapFn(previousResult, { queryVariables: queryVariables }); });
|
139 | if (newResult) {
|
140 | _this.queryManager.store.dispatch({
|
141 | type: 'APOLLO_UPDATE_QUERY_RESULT',
|
142 | newResult: newResult,
|
143 | queryVariables: queryVariables,
|
144 | querySelectionSet: querySelectionSet,
|
145 | queryFragments: queryFragments,
|
146 | });
|
147 | }
|
148 | };
|
149 | this.stopPolling = function () {
|
150 | if (isPollingQuery) {
|
151 | _this.scheduler.stopPollingQuery(_this.queryId);
|
152 | }
|
153 | };
|
154 | this.startPolling = function (pollInterval) {
|
155 | if (_this.options.noFetch) {
|
156 | throw new Error('noFetch option should not use query polling.');
|
157 | }
|
158 | if (isPollingQuery) {
|
159 | _this.scheduler.stopPollingQuery(_this.queryId);
|
160 | }
|
161 | _this.options.pollInterval = pollInterval;
|
162 | _this.scheduler.startPollingQuery(_this.options, _this.queryId, false);
|
163 | };
|
164 | }
|
165 | ObservableQuery.prototype.result = function () {
|
166 | var _this = this;
|
167 | return new Promise(function (resolve, reject) {
|
168 | var subscription = _this.subscribe({
|
169 | next: function (result) {
|
170 | resolve(result);
|
171 | setTimeout(function () {
|
172 | subscription.unsubscribe();
|
173 | }, 0);
|
174 | },
|
175 | error: function (error) {
|
176 | reject(error);
|
177 | },
|
178 | });
|
179 | });
|
180 | };
|
181 | ObservableQuery.prototype.currentResult = function () {
|
182 | var _a = this.queryManager.getCurrentQueryResult(this), data = _a.data, partial = _a.partial;
|
183 | var queryStoreValue = this.queryManager.getApolloState().queries[this.queryId];
|
184 | if (queryStoreValue && (queryStoreValue.graphQLErrors || queryStoreValue.networkError)) {
|
185 | var error = new errors_1.ApolloError({
|
186 | graphQLErrors: queryStoreValue.graphQLErrors,
|
187 | networkError: queryStoreValue.networkError,
|
188 | });
|
189 | return { data: {}, loading: false, error: error };
|
190 | }
|
191 | var queryLoading = !queryStoreValue || queryStoreValue.loading;
|
192 | var loading = (this.options.forceFetch && queryLoading)
|
193 | || (partial && !this.options.noFetch);
|
194 | return { data: data, loading: loading };
|
195 | };
|
196 | return ObservableQuery;
|
197 | }(Observable_1.Observable));
|
198 | exports.ObservableQuery = ObservableQuery;
|
199 |
|
\ | No newline at end of file |