UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

45 lines 2.12 kB
import { __assign, __extends } from "tslib"; import { invariant } from "../../utilities/globals/index.js"; import * as React from 'react'; import hoistNonReactStatics from 'hoist-non-react-statics'; import { ApolloConsumer } from "../context/index.js"; function getDisplayName(WrappedComponent) { return WrappedComponent.displayName || WrappedComponent.name || 'Component'; } export function withApollo(WrappedComponent, operationOptions) { if (operationOptions === void 0) { operationOptions = {}; } var withDisplayName = "withApollo(".concat(getDisplayName(WrappedComponent), ")"); var WithApollo = (function (_super) { __extends(WithApollo, _super); function WithApollo(props) { var _this = _super.call(this, props) || this; _this.setWrappedInstance = _this.setWrappedInstance.bind(_this); return _this; } WithApollo.prototype.getWrappedInstance = function () { __DEV__ ? invariant(operationOptions.withRef, "To access the wrapped instance, you need to specify " + "{ withRef: true } in the options") : invariant(operationOptions.withRef, 31); return this.wrappedInstance; }; WithApollo.prototype.setWrappedInstance = function (ref) { this.wrappedInstance = ref; }; WithApollo.prototype.render = function () { var _this = this; return (React.createElement(ApolloConsumer, null, function (client) { var props = Object.assign({}, _this.props, { client: client, ref: operationOptions.withRef ? _this.setWrappedInstance : undefined }); return React.createElement(WrappedComponent, __assign({}, props)); })); }; WithApollo.displayName = withDisplayName; WithApollo.WrappedComponent = WrappedComponent; return WithApollo; }(React.Component)); return hoistNonReactStatics(WithApollo, WrappedComponent, {}); } //# sourceMappingURL=withApollo.js.map