1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var globals = require('../../utilities/globals');
|
6 | var parser = require('../parser');
|
7 | var tslib = require('tslib');
|
8 | var React = require('react');
|
9 | var hoistNonReactStatics = require('hoist-non-react-statics');
|
10 | var components = require('../components');
|
11 | var context = require('../context');
|
12 |
|
13 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
14 |
|
15 | function _interopNamespace(e) {
|
16 | if (e && e.__esModule) return e;
|
17 | var n = Object.create(null);
|
18 | if (e) {
|
19 | for (var k in e) {
|
20 | n[k] = e[k];
|
21 | }
|
22 | }
|
23 | n["default"] = e;
|
24 | return Object.freeze(n);
|
25 | }
|
26 |
|
27 | var React__namespace = _interopNamespace(React);
|
28 | var hoistNonReactStatics__default = _interopDefaultLegacy(hoistNonReactStatics);
|
29 |
|
30 | var defaultMapPropsToOptions = function () { return ({}); };
|
31 | var defaultMapPropsToSkip = function () { return false; };
|
32 | function getDisplayName$1(WrappedComponent) {
|
33 | return WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
34 | }
|
35 | function calculateVariablesFromProps(operation, props) {
|
36 | var variables = {};
|
37 | for (var _i = 0, _a = operation.variables; _i < _a.length; _i++) {
|
38 | var _b = _a[_i], variable = _b.variable, type = _b.type;
|
39 | if (!variable.name || !variable.name.value)
|
40 | continue;
|
41 | var variableName = variable.name.value;
|
42 | var variableProp = props[variableName];
|
43 | if (typeof variableProp !== 'undefined') {
|
44 | variables[variableName] = variableProp;
|
45 | continue;
|
46 | }
|
47 | if (type.kind !== 'NonNullType') {
|
48 | variables[variableName] = undefined;
|
49 | }
|
50 | }
|
51 | return variables;
|
52 | }
|
53 | var GraphQLBase = (function (_super) {
|
54 | tslib.__extends(GraphQLBase, _super);
|
55 | function GraphQLBase(props) {
|
56 | var _this = _super.call(this, props) || this;
|
57 | _this.withRef = false;
|
58 | _this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
|
59 | return _this;
|
60 | }
|
61 | GraphQLBase.prototype.getWrappedInstance = function () {
|
62 | __DEV__ ? globals.invariant(this.withRef, "To access the wrapped instance, you need to specify " +
|
63 | "{ withRef: true } in the options") : globals.invariant(this.withRef, 30);
|
64 | return this.wrappedInstance;
|
65 | };
|
66 | GraphQLBase.prototype.setWrappedInstance = function (ref) {
|
67 | this.wrappedInstance = ref;
|
68 | };
|
69 | return GraphQLBase;
|
70 | }(React__namespace.Component));
|
71 |
|
72 | function withQuery(document, operationOptions) {
|
73 | if (operationOptions === void 0) { operationOptions = {}; }
|
74 | var operation = parser.parser(document);
|
75 | 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;
|
76 | var mapPropsToOptions = options;
|
77 | if (typeof mapPropsToOptions !== 'function') {
|
78 | mapPropsToOptions = function () { return options; };
|
79 | }
|
80 | var mapPropsToSkip = skip;
|
81 | if (typeof mapPropsToSkip !== 'function') {
|
82 | mapPropsToSkip = function () { return skip; };
|
83 | }
|
84 | var lastResultProps;
|
85 | return function (WrappedComponent) {
|
86 | var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
|
87 | var GraphQL = (function (_super) {
|
88 | tslib.__extends(GraphQL, _super);
|
89 | function GraphQL() {
|
90 | return _super !== null && _super.apply(this, arguments) || this;
|
91 | }
|
92 | GraphQL.prototype.render = function () {
|
93 | var _this = this;
|
94 | var props = this.props;
|
95 | var shouldSkip = mapPropsToSkip(props);
|
96 | var opts = shouldSkip
|
97 | ? Object.create(null)
|
98 | : tslib.__assign({}, mapPropsToOptions(props));
|
99 | if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
|
100 | opts.variables = calculateVariablesFromProps(operation, props);
|
101 | }
|
102 | return (React__namespace.createElement(components.Query, tslib.__assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, query: document }), function (_a) {
|
103 | var _b, _c;
|
104 | _a.client; var data = _a.data, r = tslib.__rest(_a, ["client", "data"]);
|
105 | if (operationOptions.withRef) {
|
106 | _this.withRef = true;
|
107 | props = Object.assign({}, props, {
|
108 | ref: _this.setWrappedInstance
|
109 | });
|
110 | }
|
111 | if (shouldSkip) {
|
112 | return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, {})));
|
113 | }
|
114 | var result = Object.assign(r, data || {});
|
115 | var name = operationOptions.name || 'data';
|
116 | var childProps = (_b = {}, _b[name] = result, _b);
|
117 | if (operationOptions.props) {
|
118 | var newResult = (_c = {},
|
119 | _c[name] = result,
|
120 | _c.ownProps = props,
|
121 | _c);
|
122 | lastResultProps = operationOptions.props(newResult, lastResultProps);
|
123 | childProps = lastResultProps;
|
124 | }
|
125 | return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps)));
|
126 | }));
|
127 | };
|
128 | GraphQL.displayName = graphQLDisplayName;
|
129 | GraphQL.WrappedComponent = WrappedComponent;
|
130 | return GraphQL;
|
131 | }(GraphQLBase));
|
132 | return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
|
133 | };
|
134 | }
|
135 |
|
136 | function withMutation(document, operationOptions) {
|
137 | if (operationOptions === void 0) { operationOptions = {}; }
|
138 | var operation = parser.parser(document);
|
139 | var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.alias, alias = _b === void 0 ? 'Apollo' : _b;
|
140 | var mapPropsToOptions = options;
|
141 | if (typeof mapPropsToOptions !== 'function')
|
142 | mapPropsToOptions = function () { return options; };
|
143 | return function (WrappedComponent) {
|
144 | var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
|
145 | var GraphQL = (function (_super) {
|
146 | tslib.__extends(GraphQL, _super);
|
147 | function GraphQL() {
|
148 | return _super !== null && _super.apply(this, arguments) || this;
|
149 | }
|
150 | GraphQL.prototype.render = function () {
|
151 | var props = this.props;
|
152 | var opts = mapPropsToOptions(props);
|
153 | if (operationOptions.withRef) {
|
154 | this.withRef = true;
|
155 | props = Object.assign({}, props, {
|
156 | ref: this.setWrappedInstance
|
157 | });
|
158 | }
|
159 | if (!opts.variables && operation.variables.length > 0) {
|
160 | opts.variables = calculateVariablesFromProps(operation, props);
|
161 | }
|
162 | return (React__namespace.createElement(components.Mutation, tslib.__assign({ ignoreResults: true }, opts, { mutation: document }), function (mutate, _a) {
|
163 | var _b, _c;
|
164 | var data = _a.data, r = tslib.__rest(_a, ["data"]);
|
165 | var result = Object.assign(r, data || {});
|
166 | var name = operationOptions.name || 'mutate';
|
167 | var resultName = operationOptions.name
|
168 | ? "".concat(name, "Result")
|
169 | : 'result';
|
170 | var childProps = (_b = {},
|
171 | _b[name] = mutate,
|
172 | _b[resultName] = result,
|
173 | _b);
|
174 | if (operationOptions.props) {
|
175 | var newResult = (_c = {},
|
176 | _c[name] = mutate,
|
177 | _c[resultName] = result,
|
178 | _c.ownProps = props,
|
179 | _c);
|
180 | childProps = operationOptions.props(newResult);
|
181 | }
|
182 | return React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps));
|
183 | }));
|
184 | };
|
185 | GraphQL.displayName = graphQLDisplayName;
|
186 | GraphQL.WrappedComponent = WrappedComponent;
|
187 | return GraphQL;
|
188 | }(GraphQLBase));
|
189 | return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
|
190 | };
|
191 | }
|
192 |
|
193 | function withSubscription(document, operationOptions) {
|
194 | if (operationOptions === void 0) { operationOptions = {}; }
|
195 | var operation = parser.parser(document);
|
196 | 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;
|
197 | var mapPropsToOptions = options;
|
198 | if (typeof mapPropsToOptions !== 'function')
|
199 | mapPropsToOptions = function () { return options; };
|
200 | var mapPropsToSkip = skip;
|
201 | if (typeof mapPropsToSkip !== 'function')
|
202 | mapPropsToSkip = function () { return skip; };
|
203 | var lastResultProps;
|
204 | return function (WrappedComponent) {
|
205 | var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
|
206 | var GraphQL = (function (_super) {
|
207 | tslib.__extends(GraphQL, _super);
|
208 | function GraphQL(props) {
|
209 | var _this = _super.call(this, props) || this;
|
210 | _this.state = { resubscribe: false };
|
211 | return _this;
|
212 | }
|
213 | GraphQL.prototype.updateResubscribe = function (resubscribe) {
|
214 | this.setState({ resubscribe: resubscribe });
|
215 | };
|
216 | GraphQL.prototype.componentDidUpdate = function (prevProps) {
|
217 | var resubscribe = !!(shouldResubscribe &&
|
218 | shouldResubscribe(prevProps, this.props));
|
219 | if (this.state.resubscribe !== resubscribe) {
|
220 | this.updateResubscribe(resubscribe);
|
221 | }
|
222 | };
|
223 | GraphQL.prototype.render = function () {
|
224 | var _this = this;
|
225 | var props = this.props;
|
226 | var shouldSkip = mapPropsToSkip(props);
|
227 | var opts = shouldSkip
|
228 | ? Object.create(null)
|
229 | : mapPropsToOptions(props);
|
230 | if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
|
231 | opts.variables = calculateVariablesFromProps(operation, props);
|
232 | }
|
233 | return (React__namespace.createElement(components.Subscription, tslib.__assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, subscription: document, shouldResubscribe: this.state.resubscribe }), function (_a) {
|
234 | var _b, _c;
|
235 | var data = _a.data, r = tslib.__rest(_a, ["data"]);
|
236 | if (operationOptions.withRef) {
|
237 | _this.withRef = true;
|
238 | props = Object.assign({}, props, {
|
239 | ref: _this.setWrappedInstance
|
240 | });
|
241 | }
|
242 | if (shouldSkip) {
|
243 | return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, {})));
|
244 | }
|
245 | var result = Object.assign(r, data || {});
|
246 | var name = operationOptions.name || 'data';
|
247 | var childProps = (_b = {}, _b[name] = result, _b);
|
248 | if (operationOptions.props) {
|
249 | var newResult = (_c = {},
|
250 | _c[name] = result,
|
251 | _c.ownProps = props,
|
252 | _c);
|
253 | lastResultProps = operationOptions.props(newResult, lastResultProps);
|
254 | childProps = lastResultProps;
|
255 | }
|
256 | return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps)));
|
257 | }));
|
258 | };
|
259 | GraphQL.displayName = graphQLDisplayName;
|
260 | GraphQL.WrappedComponent = WrappedComponent;
|
261 | return GraphQL;
|
262 | }(GraphQLBase));
|
263 | return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
|
264 | };
|
265 | }
|
266 |
|
267 | function graphql(document, operationOptions) {
|
268 | if (operationOptions === void 0) { operationOptions = {}; }
|
269 | switch (parser.parser(document).type) {
|
270 | case parser.DocumentType.Mutation:
|
271 | return withMutation(document, operationOptions);
|
272 | case parser.DocumentType.Subscription:
|
273 | return withSubscription(document, operationOptions);
|
274 | case parser.DocumentType.Query:
|
275 | default:
|
276 | return withQuery(document, operationOptions);
|
277 | }
|
278 | }
|
279 |
|
280 | function getDisplayName(WrappedComponent) {
|
281 | return WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
282 | }
|
283 | function withApollo(WrappedComponent, operationOptions) {
|
284 | if (operationOptions === void 0) { operationOptions = {}; }
|
285 | var withDisplayName = "withApollo(".concat(getDisplayName(WrappedComponent), ")");
|
286 | var WithApollo = (function (_super) {
|
287 | tslib.__extends(WithApollo, _super);
|
288 | function WithApollo(props) {
|
289 | var _this = _super.call(this, props) || this;
|
290 | _this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
|
291 | return _this;
|
292 | }
|
293 | WithApollo.prototype.getWrappedInstance = function () {
|
294 | __DEV__ ? globals.invariant(operationOptions.withRef, "To access the wrapped instance, you need to specify " +
|
295 | "{ withRef: true } in the options") : globals.invariant(operationOptions.withRef, 31);
|
296 | return this.wrappedInstance;
|
297 | };
|
298 | WithApollo.prototype.setWrappedInstance = function (ref) {
|
299 | this.wrappedInstance = ref;
|
300 | };
|
301 | WithApollo.prototype.render = function () {
|
302 | var _this = this;
|
303 | return (React__namespace.createElement(context.ApolloConsumer, null, function (client) {
|
304 | var props = Object.assign({}, _this.props, {
|
305 | client: client,
|
306 | ref: operationOptions.withRef
|
307 | ? _this.setWrappedInstance
|
308 | : undefined
|
309 | });
|
310 | return React__namespace.createElement(WrappedComponent, tslib.__assign({}, props));
|
311 | }));
|
312 | };
|
313 | WithApollo.displayName = withDisplayName;
|
314 | WithApollo.WrappedComponent = WrappedComponent;
|
315 | return WithApollo;
|
316 | }(React__namespace.Component));
|
317 | return hoistNonReactStatics__default(WithApollo, WrappedComponent, {});
|
318 | }
|
319 |
|
320 | exports.graphql = graphql;
|
321 | exports.withApollo = withApollo;
|
322 | exports.withMutation = withMutation;
|
323 | exports.withQuery = withQuery;
|
324 | exports.withSubscription = withSubscription;
|
325 |
|