react-apollo
Version:
React data container for Apollo Client
51 lines • 2.44 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Component, createElement, } from 'react';
import * as PropTypes from 'prop-types';
var invariant = require('invariant');
var assign = require('object-assign');
var hoistNonReactStatics = require('hoist-non-react-statics');
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
export function withApollo(WrappedComponent, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var withDisplayName = "withApollo(" + getDisplayName(WrappedComponent) + ")";
var WithApollo = (function (_super) {
__extends(WithApollo, _super);
function WithApollo(props, context) {
var _this = _super.call(this, props, context) || this;
_this.client = context.client;
invariant(!!_this.client, "Could not find \"client\" in the context of " +
("\"" + withDisplayName + "\". ") +
"Wrap the root component in an <ApolloProvider>");
return _this;
}
WithApollo.prototype.getWrappedInstance = function () {
invariant(operationOptions.withRef, "To access the wrapped instance, you need to specify " +
"{ withRef: true } in the options");
return this.refs.wrappedInstance;
};
WithApollo.prototype.render = function () {
var props = assign({}, this.props);
props.client = this.client;
if (operationOptions.withRef)
props.ref = 'wrappedInstance';
return createElement(WrappedComponent, props);
};
return WithApollo;
}(Component));
WithApollo.displayName = withDisplayName;
WithApollo.WrappedComponent = WrappedComponent;
WithApollo.contextTypes = { client: PropTypes.object.isRequired };
return hoistNonReactStatics(WithApollo, WrappedComponent, {});
}
//# sourceMappingURL=withApollo.js.map