1 | import { __assign, __extends, __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import hoistNonReactStatics from 'hoist-non-react-statics';
|
4 | import { parser } from "../parser/index.js";
|
5 | import { Mutation } from "../components/index.js";
|
6 | import { defaultMapPropsToOptions, getDisplayName, calculateVariablesFromProps, GraphQLBase } from "./hoc-utils.js";
|
7 | export function withMutation(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.alias, alias = _b === void 0 ? 'Apollo' : _b;
|
11 | var mapPropsToOptions = options;
|
12 | if (typeof mapPropsToOptions !== 'function')
|
13 | mapPropsToOptions = function () { return options; };
|
14 | return function (WrappedComponent) {
|
15 | var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName(WrappedComponent), ")");
|
16 | var GraphQL = (function (_super) {
|
17 | __extends(GraphQL, _super);
|
18 | function GraphQL() {
|
19 | return _super !== null && _super.apply(this, arguments) || this;
|
20 | }
|
21 | GraphQL.prototype.render = function () {
|
22 | var props = this.props;
|
23 | var opts = mapPropsToOptions(props);
|
24 | if (operationOptions.withRef) {
|
25 | this.withRef = true;
|
26 | props = Object.assign({}, props, {
|
27 | ref: this.setWrappedInstance
|
28 | });
|
29 | }
|
30 | if (!opts.variables && operation.variables.length > 0) {
|
31 | opts.variables = calculateVariablesFromProps(operation, props);
|
32 | }
|
33 | return (React.createElement(Mutation, __assign({ ignoreResults: true }, opts, { mutation: document }), function (mutate, _a) {
|
34 | var _b, _c;
|
35 | var data = _a.data, r = __rest(_a, ["data"]);
|
36 | var result = Object.assign(r, data || {});
|
37 | var name = operationOptions.name || 'mutate';
|
38 | var resultName = operationOptions.name
|
39 | ? "".concat(name, "Result")
|
40 | : 'result';
|
41 | var childProps = (_b = {},
|
42 | _b[name] = mutate,
|
43 | _b[resultName] = result,
|
44 | _b);
|
45 | if (operationOptions.props) {
|
46 | var newResult = (_c = {},
|
47 | _c[name] = mutate,
|
48 | _c[resultName] = result,
|
49 | _c.ownProps = props,
|
50 | _c);
|
51 | childProps = operationOptions.props(newResult);
|
52 | }
|
53 | return React.createElement(WrappedComponent, __assign({}, props, childProps));
|
54 | }));
|
55 | };
|
56 | GraphQL.displayName = graphQLDisplayName;
|
57 | GraphQL.WrappedComponent = WrappedComponent;
|
58 | return GraphQL;
|
59 | }(GraphQLBase));
|
60 | return hoistNonReactStatics(GraphQL, WrappedComponent, {});
|
61 | };
|
62 | }
|
63 |
|
\ | No newline at end of file |