1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.ApolloProvider = ApolloProvider;
|
7 | exports.useApolloClient = useApolloClient;
|
8 | exports.useApolloQuery = useApolloQuery;
|
9 | exports.useApolloMutation = useApolloMutation;
|
10 |
|
11 | var _react = _interopRequireWildcard(require("react"));
|
12 |
|
13 | var _reactFastCompare = _interopRequireDefault(require("react-fast-compare"));
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
18 |
|
19 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
20 |
|
21 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
22 |
|
23 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
|
24 |
|
25 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
|
26 |
|
27 | function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
28 |
|
29 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
30 |
|
31 | function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
32 |
|
33 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
34 |
|
35 | var ApolloContext = _react.default.createContext();
|
36 |
|
37 | function ApolloProvider(_ref) {
|
38 | var children = _ref.children,
|
39 | client = _ref.client;
|
40 | return _react.default.createElement(ApolloContext.Provider, {
|
41 | value: client
|
42 | }, children);
|
43 | }
|
44 |
|
45 | function useApolloClient() {
|
46 | return (0, _react.useContext)(ApolloContext);
|
47 | }
|
48 |
|
49 | function useApolloQuery(query) {
|
50 | var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
51 | variables = _ref2.variables,
|
52 | restOptions = _objectWithoutProperties(_ref2, ["variables"]);
|
53 |
|
54 | var client = useApolloClient();
|
55 |
|
56 | var _useState = (0, _react.useState)(),
|
57 | _useState2 = _slicedToArray(_useState, 2),
|
58 | result = _useState2[0],
|
59 | setResult = _useState2[1];
|
60 |
|
61 | var previousQuery = (0, _react.useRef)();
|
62 | var previousVariables = (0, _react.useRef)();
|
63 | var observableQuery = (0, _react.useRef)();
|
64 | (0, _react.useEffect)(function () {
|
65 | var subscription = observableQuery.current.subscribe(function (nextResult) {
|
66 | setResult(nextResult);
|
67 | });
|
68 | return function () {
|
69 | subscription.unsubscribe();
|
70 | };
|
71 | }, [query, objToKey(variables), objToKey(restOptions)]);
|
72 | var helpers = {
|
73 | fetchMore: function fetchMore(opts) {
|
74 | return observableQuery.current.fetchMore(opts);
|
75 | }
|
76 | };
|
77 |
|
78 | if (!(query === previousQuery.current && (0, _reactFastCompare.default)(variables, previousVariables.current))) {
|
79 | previousQuery.current = query;
|
80 | previousVariables.current = variables;
|
81 | var watchedQuery = client.watchQuery(_objectSpread({
|
82 | query: query,
|
83 | variables: variables
|
84 | }, restOptions));
|
85 | observableQuery.current = watchedQuery;
|
86 | var currentResult = watchedQuery.currentResult();
|
87 |
|
88 | if (currentResult.partial) {
|
89 |
|
90 |
|
91 | throw watchedQuery.result();
|
92 | }
|
93 |
|
94 | setResult(currentResult);
|
95 | return _objectSpread({}, helpers, currentResult);
|
96 | }
|
97 |
|
98 | return _objectSpread({}, helpers, result);
|
99 | }
|
100 |
|
101 | function useApolloMutation(mutation, baseOptions) {
|
102 | var client = useApolloClient();
|
103 | return function (localOptions) {
|
104 | return client.mutate(_objectSpread({
|
105 | mutation: mutation
|
106 | }, baseOptions, localOptions));
|
107 | };
|
108 | }
|
109 |
|
110 | function objToKey(obj) {
|
111 | if (!obj) {
|
112 | return null;
|
113 | }
|
114 |
|
115 | var keys = Object.keys(obj);
|
116 | keys.sort();
|
117 | var sortedObj = keys.reduce(function (result, key) {
|
118 | result[key] = obj[key];
|
119 | return result;
|
120 | }, {});
|
121 | return JSON.stringify(sortedObj);
|
122 | } |
\ | No newline at end of file |