UNPKG

4.33 kBJavaScriptView Raw
1import { __assign, __extends, __rest } from "tslib";
2import * as React from 'react';
3import hoistNonReactStatics from 'hoist-non-react-statics';
4import { parser } from "../parser/index.js";
5import { Subscription } from "../components/index.js";
6import { getDisplayName, GraphQLBase, calculateVariablesFromProps, defaultMapPropsToOptions, defaultMapPropsToSkip } from "./hoc-utils.js";
7export function withSubscription(document, operationOptions) {
8 if (operationOptions === void 0) { operationOptions = {}; }
9 var operation = parser(document);
10 var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? 'Apollo' : _c, shouldResubscribe = operationOptions.shouldResubscribe;
11 var mapPropsToOptions = options;
12 if (typeof mapPropsToOptions !== 'function')
13 mapPropsToOptions = function () { return options; };
14 var mapPropsToSkip = skip;
15 if (typeof mapPropsToSkip !== 'function')
16 mapPropsToSkip = function () { return skip; };
17 var lastResultProps;
18 return function (WrappedComponent) {
19 var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName(WrappedComponent), ")");
20 var GraphQL = (function (_super) {
21 __extends(GraphQL, _super);
22 function GraphQL(props) {
23 var _this = _super.call(this, props) || this;
24 _this.state = { resubscribe: false };
25 return _this;
26 }
27 GraphQL.prototype.updateResubscribe = function (resubscribe) {
28 this.setState({ resubscribe: resubscribe });
29 };
30 GraphQL.prototype.componentDidUpdate = function (prevProps) {
31 var resubscribe = !!(shouldResubscribe &&
32 shouldResubscribe(prevProps, this.props));
33 if (this.state.resubscribe !== resubscribe) {
34 this.updateResubscribe(resubscribe);
35 }
36 };
37 GraphQL.prototype.render = function () {
38 var _this = this;
39 var props = this.props;
40 var shouldSkip = mapPropsToSkip(props);
41 var opts = shouldSkip
42 ? Object.create(null)
43 : mapPropsToOptions(props);
44 if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
45 opts.variables = calculateVariablesFromProps(operation, props);
46 }
47 return (React.createElement(Subscription, __assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, subscription: document, shouldResubscribe: this.state.resubscribe }), function (_a) {
48 var _b, _c;
49 var data = _a.data, r = __rest(_a, ["data"]);
50 if (operationOptions.withRef) {
51 _this.withRef = true;
52 props = Object.assign({}, props, {
53 ref: _this.setWrappedInstance
54 });
55 }
56 if (shouldSkip) {
57 return (React.createElement(WrappedComponent, __assign({}, props, {})));
58 }
59 var result = Object.assign(r, data || {});
60 var name = operationOptions.name || 'data';
61 var childProps = (_b = {}, _b[name] = result, _b);
62 if (operationOptions.props) {
63 var newResult = (_c = {},
64 _c[name] = result,
65 _c.ownProps = props,
66 _c);
67 lastResultProps = operationOptions.props(newResult, lastResultProps);
68 childProps = lastResultProps;
69 }
70 return (React.createElement(WrappedComponent, __assign({}, props, childProps)));
71 }));
72 };
73 GraphQL.displayName = graphQLDisplayName;
74 GraphQL.WrappedComponent = WrappedComponent;
75 return GraphQL;
76 }(GraphQLBase));
77 return hoistNonReactStatics(GraphQL, WrappedComponent, {});
78 };
79}
80//# sourceMappingURL=subscription-hoc.js.map
\No newline at end of file