@zendesk/laika
Version:
Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!
43 lines • 1.89 kB
JavaScript
import { ApolloLink, Observable } from '@apollo/client/core';
/**
* Creates a lazy-loadable Apollo Link.
* Useful when you want to use a given Apollo Link in a production environment conditionally,
* i.e. only load it when necessary.
* @param linkPromise A Promise to an Apollo Link to wrap.
*/
export const createLazyLoadableLink = (linkPromise) => new ApolloLink((operation, forward) => new Observable((observer) => {
let innerSubscription;
let disposed = false;
void linkPromise.then((link) => {
var _a;
if (disposed)
return;
const actualLinkObservable = link === null || link === void 0 ? void 0 : link.request(operation, forward);
if (!actualLinkObservable) {
(_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, new Error(`LazyLoadableLink: Incorrect linkPromise provided or it's request function returned null`));
return;
}
innerSubscription = actualLinkObservable.subscribe({
next: (result) => {
var _a;
(_a = observer.next) === null || _a === void 0 ? void 0 : _a.call(observer, result);
},
error: (error) => {
var _a;
(_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, error);
},
complete: () => {
var _a;
(_a = observer.complete) === null || _a === void 0 ? void 0 : _a.call(observer);
},
});
}, (error) => {
var _a;
(_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, error);
});
return () => {
disposed = true;
innerSubscription === null || innerSubscription === void 0 ? void 0 : innerSubscription.unsubscribe();
};
}));
//# sourceMappingURL=createLazyLoadableLink.js.map