apollo-mobx
Version:
HOC for querying graphql server
44 lines (43 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
function waitForData(keys, options) {
if (keys === void 0) { keys = ['data']; }
if (options === void 0) { options = {}; }
if (typeof keys === 'string') {
keys = [keys];
}
return function (WrappedComponent) {
var func = function (props, context) {
var loading = options.loadingComponent || props.client.loadingComponent;
if (!loading) {
throw new Error('Apollo-Mobx: Loading component for "waitForData" is not defined.');
}
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
var selector = void 0;
if (key.indexOf('.') >= 0) {
var splitKey = key.split('.');
key = splitKey[0];
selector = splitKey[1];
}
if (!props[key]) {
throw new Error('Loading container did not find key in the apollo result set: ' + key);
}
if (selector) {
if (props[key][selector] == null) {
return loading.apply(void 0, props);
}
}
else if (props[key].loading) {
return loading.apply(void 0, props);
}
}
return React.createElement(WrappedComponent, tslib_1.__assign({}, props));
};
func.displayName = "WaitForLoad(" + (WrappedComponent.displayName || 'Component') + ")";
return func;
};
}
exports.waitForData = waitForData;