apollo-client-rxjs
Version:
Adds RxJS to ApolloClient
33 lines • 1.48 kB
JavaScript
import { ApolloClient } from 'apollo-client';
import { observeOn } from 'rxjs/operator/observeOn';
import { AsyncAction } from 'rxjs/scheduler/AsyncAction';
import { AsyncScheduler } from 'rxjs/scheduler/AsyncScheduler';
import { isObject } from './utils/helpers';
import { createWithObservableVariables } from './utils/variables';
import { RxObservableQuery } from './RxObservableQuery';
export function rxify(clientOrWatchQuery) {
if (clientOrWatchQuery instanceof ApolloClient) {
var savedWatchQuery_1 = clientOrWatchQuery.watchQuery;
clientOrWatchQuery.watchQuery = function (options) {
var _this = this;
return wrapWatchQuery(function (opts) { return savedWatchQuery_1.call(_this, opts); })(options);
}.bind(clientOrWatchQuery);
return clientOrWatchQuery;
}
if (isObject(clientOrWatchQuery)) {
return wrapWatchQuery(clientOrWatchQuery);
}
throw new Error('Use ApolloClient or a function that returns ObservableQuery');
}
function wrapAsync(obs) {
return observeOn.call(obs, new AsyncScheduler(AsyncAction));
}
function wrapWatchQuery(watchQuery) {
return function (options) {
if (typeof options.variables === 'object') {
return wrapAsync(createWithObservableVariables(options, function (newOptions) { return watchQuery(newOptions); }));
}
return wrapAsync(new RxObservableQuery(watchQuery(options)));
};
}
//# sourceMappingURL=rxify.js.map