UNPKG

3.68 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 { Query } from "../components/index.js";
6import { getDisplayName, GraphQLBase, calculateVariablesFromProps, defaultMapPropsToOptions, defaultMapPropsToSkip } from "./hoc-utils.js";
7export function withQuery(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;
11 var mapPropsToOptions = options;
12 if (typeof mapPropsToOptions !== 'function') {
13 mapPropsToOptions = function () { return options; };
14 }
15 var mapPropsToSkip = skip;
16 if (typeof mapPropsToSkip !== 'function') {
17 mapPropsToSkip = function () { return skip; };
18 }
19 var lastResultProps;
20 return function (WrappedComponent) {
21 var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName(WrappedComponent), ")");
22 var GraphQL = (function (_super) {
23 __extends(GraphQL, _super);
24 function GraphQL() {
25 return _super !== null && _super.apply(this, arguments) || this;
26 }
27 GraphQL.prototype.render = function () {
28 var _this = this;
29 var props = this.props;
30 var shouldSkip = mapPropsToSkip(props);
31 var opts = shouldSkip
32 ? Object.create(null)
33 : __assign({}, mapPropsToOptions(props));
34 if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
35 opts.variables = calculateVariablesFromProps(operation, props);
36 }
37 return (React.createElement(Query, __assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, query: document }), function (_a) {
38 var _b, _c;
39 var _ = _a.client, data = _a.data, r = __rest(_a, ["client", "data"]);
40 if (operationOptions.withRef) {
41 _this.withRef = true;
42 props = Object.assign({}, props, {
43 ref: _this.setWrappedInstance
44 });
45 }
46 if (shouldSkip) {
47 return (React.createElement(WrappedComponent, __assign({}, props, {})));
48 }
49 var result = Object.assign(r, data || {});
50 var name = operationOptions.name || 'data';
51 var childProps = (_b = {}, _b[name] = result, _b);
52 if (operationOptions.props) {
53 var newResult = (_c = {},
54 _c[name] = result,
55 _c.ownProps = props,
56 _c);
57 lastResultProps = operationOptions.props(newResult, lastResultProps);
58 childProps = lastResultProps;
59 }
60 return (React.createElement(WrappedComponent, __assign({}, props, childProps)));
61 }));
62 };
63 GraphQL.displayName = graphQLDisplayName;
64 GraphQL.WrappedComponent = WrappedComponent;
65 return GraphQL;
66 }(GraphQLBase));
67 return hoistNonReactStatics(GraphQL, WrappedComponent, {});
68 };
69}
70//# sourceMappingURL=query-hoc.js.map
\No newline at end of file