-
Notifications
You must be signed in to change notification settings - Fork 72
/
apollo.js
321 lines (292 loc) · 9.82 KB
/
apollo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import Ember from 'ember';
import Service from "@ember/service"
import EmberObject, { get, setProperties, computed } from "@ember/object";
import { A } from "@ember/array";
import { copy } from "@ember/object/internals";
import { deprecate } from "@ember/application/deprecations";
import { isArray } from "@ember/array";
import { isNone, isPresent } from "@ember/utils";
import { getOwner } from "@ember/application";
import { merge } from '@ember/polyfills';
import RSVP from "rsvp";
import { run } from "@ember/runloop";
import { alias } from "@ember/object/computed";
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { apolloObservableKey } from 'ember-apollo-client';
import QueryManager from 'ember-apollo-client/apollo/query-manager';
import copyWithExtras from 'ember-apollo-client/utils/copy-with-extras';
import { registerWaiter } from '@ember/test';
const {
testing,
} = Ember;
function newDataFunc(observable, resultKey, resolve, mergedProps = {}) {
let obj;
mergedProps[apolloObservableKey] = observable;
return ({ data, loading }) => {
if (loading && data === undefined) {
// This happens when the cache has no data and the data is still loading
// from the server. We don't want to resolve the promise with empty data
// so we instead just bail out.
//
// See https://github.com/bgentry/ember-apollo-client/issues/45
return;
}
let keyedData = isNone(resultKey) ? data : get(data, resultKey);
let dataToSend = copyWithExtras(keyedData, [], []);
if (isNone(obj)) {
if (isArray(dataToSend)) {
obj = A(dataToSend);
obj.setProperties(mergedProps);
} else {
obj = EmberObject.create(merge(dataToSend, mergedProps));
}
return resolve(obj);
}
run(() => {
isArray(obj)
? obj.setObjects(dataToSend)
: setProperties(obj, dataToSend);
});
};
}
// used in environments without injected `config:environment` (i.e. unit tests):
const defaultOptions = {
apiURL: "http://testserver.example/v1/graph",
};
export default Service.extend({
client: null,
apiURL: alias('options.apiURL'),
requestCredentials: alias('options.requestCredentials'),
// options are configured in your environment.js.
options: computed(function() {
// config:environment not injected into tests, so try to handle that gracefully.
let config = getOwner(this).resolveRegistration("config:environment");
if (config && config.apollo) {
return config.apollo;
} else if (testing) {
return defaultOptions;
}
throw new Error("no Apollo service options defined");
}),
init() {
this._super(...arguments);
const owner = getOwner(this);
if (owner) {
owner.registerOptionsForType('apollo', { instantiate: false });
}
let client = new ApolloClient(this.get('clientOptions'));
this.set('client', client);
if (testing) {
this._registerWaiter();
}
},
/**
* This is the options hash that will be passed to the ApolloClient constructor.
* You can override it if you wish to customize the ApolloClient.
*
* @method clientOptions
* @return {!Object}
* @public
*/
clientOptions: computed(function() {
const apiURL = this.get('apiURL');
const requestCredentials = this.get('requestCredentials');
const middlewares = this.get('middlewares');
const networkInterfaceOptions = {
uri: apiURL,
opts: {},
}
if (isPresent(requestCredentials)) {
networkInterfaceOptions.opts.credentials = requestCredentials;
}
const networkInterface = createNetworkInterface(networkInterfaceOptions);
if (isPresent(middlewares)) {
networkInterface.use(middlewares);
}
return {
networkInterface,
};
}),
/**
* Executes a mutation on the Apollo client. The resolved object will
* never be updated and does not have to be unsubscribed.
*
* @method mutate
* @param {!Object} opts The query options used in the Apollo Client mutate.
* @param {String} resultKey The key that will be returned from the resulting response data. If null or undefined, the entire response data will be returned.
* @return {!Promise}
* @public
*/
mutate(opts, resultKey) {
return this._waitFor(
new RSVP.Promise((resolve, reject) => {
this.client
.mutate(opts)
.then(result => {
let dataToSend = isNone(resultKey)
? result.data
: result.data[resultKey];
dataToSend = copy(dataToSend, true);
return resolve(dataToSend);
})
.catch(error => {
let errors;
if (isPresent(error.networkError)) {
error.networkError.code = 'network_error';
errors = [error.networkError];
} else if (isPresent(error.graphQLErrors)) {
errors = error.graphQLErrors;
}
if (errors) {
return reject({ errors });
}
throw error;
});
})
);
},
/**
* Executes a `watchQuery` on the Apollo client. If updated data for this
* query is loaded into the store by another query, the resolved object will
* be updated with the new data.
*
* When using this method, it is important to call `apolloUnsubscribe()` on
* the resolved data when the route or component is torn down. That tells
* Apollo to stop trying to send updated data to a non-existent listener.
*
* @method query
* @param {!Object} opts The query options used in the Apollo Client watchQuery.
* @param {String} resultKey The key that will be returned from the resulting response data. If null or undefined, the entire response data will be returned.
* @deprecated Use `watchQuery` instead.
* @return {!Promise}
* @public
*/
query(opts, resultKey) {
deprecate(`Usage of \`query\` is deprecated, use \`watchQuery\` instead.`, false, {
id: 'ember-apollo-client.deprecate-query-for-watch-query',
until: '1.0.0',
});
return this.watchQuery(opts, resultKey);
},
/**
* Executes a `watchQuery` on the Apollo client. If updated data for this
* query is loaded into the store by another query, the resolved object will
* be updated with the new data.
*
* When using this method, it is important to call `apolloUnsubscribe()` on
* the resolved data when the route or component is torn down. That tells
* Apollo to stop trying to send updated data to a non-existent listener.
*
* @method watchQuery
* @param {!Object} opts The query options used in the Apollo Client watchQuery.
* @param {String} resultKey The key that will be returned from the resulting response data. If null or undefined, the entire response data will be returned.
* @return {!Promise}
* @public
*/
watchQuery(opts, resultKey) {
let observable = this.client.watchQuery(opts);
let subscription;
let mergedProps = {
_apolloUnsubscribe() {
subscription.unsubscribe();
},
};
mergedProps[apolloObservableKey] = observable;
return this._waitFor(
new RSVP.Promise((resolve, reject) => {
// TODO: add an error function here for handling errors
subscription = observable.subscribe({
next: newDataFunc(observable, resultKey, resolve, mergedProps),
error(e) {
reject(e);
},
});
})
);
},
/**
* Executes a single `query` on the Apollo client. The resolved object will
* never be updated and does not have to be unsubscribed.
*
* @method queryOnce
* @param {!Object} opts The query options used in the Apollo Client query.
* @param {String} resultKey The key that will be returned from the resulting response data. If null or undefined, the entire response data will be returned.
* @return {!Promise}
* @public
*/
queryOnce(opts, resultKey) {
return this._waitFor(
this.client.query(opts).then(result => {
let response = result.data;
if (!isNone(resultKey)) {
response = response[resultKey];
}
return RSVP.resolve(copy(response, true));
})
);
},
/**
* Executes a `watchQuery` on the Apollo client and tracks the resulting
* subscription on the provided query manager.
*
* @method managedWatchQuery
* @param {!Object} manager A QueryManager that should track this active watchQuery.
* @param {!Object} opts The query options used in the Apollo Client watchQuery.
* @param {String} resultKey The key that will be returned from the resulting response data. If null or undefined, the entire response data will be returned.
* @return {!Promise}
* @private
*/
managedWatchQuery(manager, opts, resultKey) {
let observable = this.client.watchQuery(opts);
return this._waitFor(
new RSVP.Promise((resolve, reject) => {
let subscription = observable.subscribe({
next: newDataFunc(observable, resultKey, resolve),
error(e) {
reject(e);
},
});
manager.trackSubscription(subscription);
})
);
},
createQueryManager() {
return QueryManager.create({ apollo: this });
},
/**
* Wraps a promise in test waiters.
*
* @param {!Promise} promise
* @return {!Promise}
* @private
*/
_waitFor(promise) {
this._incrementOngoing();
return promise
.then(result => {
this._decrementOngoing();
return result;
})
.catch(err => {
this._decrementOngoing();
return RSVP.reject(err);
});
},
// unresolved / ongoing requests, used for tests:
_ongoing: 0,
_incrementOngoing() {
this._ongoing++;
},
_decrementOngoing() {
this._ongoing--;
},
_shouldWait() {
return this._ongoing === 0;
},
_registerWaiter() {
this._waiter = () => {
return this._shouldWait();
};
registerWaiter(this._waiter);
},
});