Skip to content

Commit f1a1e8b

Browse files
author
Guillaume Chau
committed
chore: v3.0.0-beta.14
1 parent 24f3d28 commit f1a1e8b

File tree

4 files changed

+204
-210
lines changed

4 files changed

+204
-210
lines changed

dist/vue-apollo.esm.js

Lines changed: 101 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
1515

1616

1717

18-
var asyncToGenerator = function (fn) {
19-
return function () {
20-
var gen = fn.apply(this, arguments);
21-
return new Promise(function (resolve, reject) {
22-
function step(key, arg) {
23-
try {
24-
var info = gen[key](arg);
25-
var value = info.value;
26-
} catch (error) {
27-
reject(error);
28-
return;
29-
}
30-
31-
if (info.done) {
32-
resolve(value);
33-
} else {
34-
return Promise.resolve(value).then(function (value) {
35-
step("next", value);
36-
}, function (err) {
37-
step("throw", err);
38-
});
39-
}
40-
}
4118

42-
return step("next");
43-
});
44-
};
45-
};
4619

4720
var classCallCheck = function (instance, Constructor) {
4821
if (!(instance instanceof Constructor)) {
@@ -263,6 +236,12 @@ function omit(obj, properties) {
263236
}, {});
264237
}
265238

239+
function addGqlError(error) {
240+
if (error.graphQLErrors && error.graphQLErrors.length) {
241+
error.gqlError = error.graphQLErrors[0];
242+
}
243+
}
244+
266245
var SmartApollo = function () {
267246
function SmartApollo(vm, key, options) {
268247
var autostart = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
@@ -420,8 +399,10 @@ var SmartApollo = function () {
420399
}
421400
}, {
422401
key: 'nextResult',
423-
value: function nextResult() {
424-
throw new Error('Not implemented');
402+
value: function nextResult(result) {
403+
var error = result.error;
404+
405+
if (error) addGqlError(error);
425406
}
426407
}, {
427408
key: 'callHandlers',
@@ -477,6 +458,8 @@ var SmartApollo = function () {
477458
}, {
478459
key: 'catchError',
479460
value: function catchError(error) {
461+
addGqlError(error);
462+
480463
var catched = this.errorHandler(error);
481464

482465
if (catched) return;
@@ -626,14 +609,10 @@ var SmartQuery = function (_SmartApollo) {
626609

627610
// Create observer
628611
this.observer = this.vm.$apollo.watchQuery(this.generateApolloOptions(variables));
629-
630-
// Create subscription
631-
this.sub = this.observer.subscribe({
632-
next: this.nextResult.bind(this),
633-
error: this.catchError.bind(this)
634-
});
635612
}
636613

614+
this.startQuerySubscription();
615+
637616
if (this.options.fetchPolicy !== 'no-cache') {
638617
var currentResult = this.maySetLoading();
639618

@@ -644,6 +623,17 @@ var SmartQuery = function (_SmartApollo) {
644623

645624
get(SmartQuery.prototype.__proto__ || Object.getPrototypeOf(SmartQuery.prototype), 'executeApollo', this).call(this, variables);
646625
}
626+
}, {
627+
key: 'startQuerySubscription',
628+
value: function startQuerySubscription() {
629+
if (this.sub && !this.sub.closed) return;
630+
631+
// Create subscription
632+
this.sub = this.observer.subscribe({
633+
next: this.nextResult.bind(this),
634+
error: this.catchError.bind(this)
635+
});
636+
}
647637
}, {
648638
key: 'maySetLoading',
649639
value: function maySetLoading() {
@@ -661,6 +651,8 @@ var SmartQuery = function (_SmartApollo) {
661651
}, {
662652
key: 'nextResult',
663653
value: function nextResult(result) {
654+
get(SmartQuery.prototype.__proto__ || Object.getPrototypeOf(SmartQuery.prototype), 'nextResult', this).call(this, result);
655+
664656
var data = result.data,
665657
loading = result.loading;
666658

@@ -676,7 +668,7 @@ var SmartQuery = function (_SmartApollo) {
676668
} else if (!this.options.manual) {
677669
if (typeof this.options.update === 'function') {
678670
this.setData(this.options.update.call(this.vm, data));
679-
} else if (data[this.key] === undefined) {
671+
} else if (typeof data[this.key] === 'undefined' && Object.keys(data).length) {
680672
console.error('Missing ' + this.key + ' attribute on result', data);
681673
} else {
682674
this.setData(data[this.key]);
@@ -699,6 +691,18 @@ var SmartQuery = function (_SmartApollo) {
699691
value: function catchError(error) {
700692
get(SmartQuery.prototype.__proto__ || Object.getPrototypeOf(SmartQuery.prototype), 'catchError', this).call(this, error);
701693
this.loadingDone();
694+
this.nextResult(this.observer.currentResult());
695+
// The observable closes the sub if an error occurs
696+
this.resubscribeToQuery();
697+
}
698+
}, {
699+
key: 'resubscribeToQuery',
700+
value: function resubscribeToQuery() {
701+
var lastError = this.observer.getLastError();
702+
var lastResult = this.observer.getLastResult();
703+
this.observer.resetLastResults();
704+
this.startQuerySubscription();
705+
Object.assign(this.observer, { lastError: lastError, lastResult: lastResult });
702706
}
703707
}, {
704708
key: 'watchLoading',
@@ -900,6 +904,8 @@ var SmartSubscription = function (_SmartApollo) {
900904
}, {
901905
key: 'nextResult',
902906
value: function nextResult(data) {
907+
get(SmartSubscription.prototype.__proto__ || Object.getPrototypeOf(SmartSubscription.prototype), 'nextResult', this).call(this, data);
908+
903909
if (typeof this.options.result === 'function') {
904910
this.options.result.call(this.vm, data);
905911
}
@@ -1520,7 +1526,6 @@ var CApolloQuery = {
15201526
error: function error(_error) {
15211527
this.result.loading = false;
15221528
this.result.error = _error;
1523-
console.log(this.$apollo.queries.query.observer.currentResult());
15241529
this.$emit('error', _error);
15251530
}
15261531
};
@@ -1545,7 +1550,8 @@ var CApolloQuery = {
15451550
var result = this.$scopedSlots.default({
15461551
result: this.result,
15471552
query: this.$apollo.queries.query,
1548-
isLoading: this.$apolloData.loading
1553+
isLoading: this.$apolloData.loading,
1554+
gqlError: this.result && this.result.error && this.result.error.gqlError
15491555
});
15501556
if (Array.isArray(result)) {
15511557
result = result.concat(this.$slots.default);
@@ -1664,56 +1670,32 @@ var CApolloMutation = {
16641670
mutate: function mutate(options) {
16651671
var _this = this;
16661672

1667-
return asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
1668-
var result;
1669-
return regeneratorRuntime.wrap(function _callee$(_context) {
1670-
while (1) {
1671-
switch (_context.prev = _context.next) {
1672-
case 0:
1673-
_this.loading = true;
1674-
_this.error = null;
1675-
_context.prev = 2;
1676-
_context.next = 5;
1677-
return _this.$apollo.mutate(_extends({
1678-
mutation: _this.mutation,
1679-
variables: _this.variables,
1680-
optimisticResponse: _this.optimisticResponse,
1681-
update: _this.update,
1682-
refetchQueries: _this.refetchQueries
1683-
}, options));
1684-
1685-
case 5:
1686-
result = _context.sent;
1687-
1688-
_this.$emit('done', result);
1689-
_context.next = 13;
1690-
break;
1691-
1692-
case 9:
1693-
_context.prev = 9;
1694-
_context.t0 = _context['catch'](2);
1695-
1696-
_this.error = _context.t0;
1697-
_this.$emit('error', _context.t0);
1698-
1699-
case 13:
1700-
_this.loading = false;
1701-
1702-
case 14:
1703-
case 'end':
1704-
return _context.stop();
1705-
}
1706-
}
1707-
}, _callee, _this, [[2, 9]]);
1708-
}))();
1673+
this.loading = true;
1674+
this.error = null;
1675+
this.$apollo.mutate(_extends({
1676+
mutation: this.mutation,
1677+
variables: this.variables,
1678+
optimisticResponse: this.optimisticResponse,
1679+
update: this.update,
1680+
refetchQueries: this.refetchQueries
1681+
}, options)).then(function (result) {
1682+
_this.$emit('done', result);
1683+
_this.loading = false;
1684+
}).catch(function (e) {
1685+
addGqlError(e);
1686+
_this.error = e;
1687+
_this.$emit('error', e);
1688+
_this.loading = false;
1689+
});
17091690
}
17101691
},
17111692

17121693
render: function render(h) {
17131694
var result = this.$scopedSlots.default({
17141695
mutate: this.mutate,
17151696
loading: this.loading,
1716-
error: this.error
1697+
error: this.error,
1698+
gqlError: this.error && this.error.gqlError
17171699
});
17181700
if (Array.isArray(result)) {
17191701
result = result.concat(this.$slots.default);
@@ -1730,9 +1712,38 @@ function hasProperty(holder, key) {
17301712
return typeof holder !== 'undefined' && holder.hasOwnProperty(key);
17311713
}
17321714

1733-
var launch = function launch() {
1715+
function proxyData() {
17341716
var _this = this;
17351717

1718+
var apollo = this.$options.apollo;
1719+
1720+
if (apollo) {
1721+
var _loop = function _loop(key) {
1722+
if (key.charAt(0) !== '$') {
1723+
var options = apollo[key];
1724+
// Property proxy
1725+
if (!options.manual && !hasProperty(_this.$options.props, key) && !hasProperty(_this.$options.computed, key) && !hasProperty(_this.$options.methods, key)) {
1726+
Object.defineProperty(_this, key, {
1727+
get: function get$$1() {
1728+
return _this.$data.$apolloData.data[key];
1729+
},
1730+
enumerable: true,
1731+
configurable: true
1732+
});
1733+
}
1734+
}
1735+
};
1736+
1737+
// watchQuery
1738+
for (var key in apollo) {
1739+
_loop(key);
1740+
}
1741+
}
1742+
}
1743+
1744+
function launch() {
1745+
var _this2 = this;
1746+
17361747
var apolloProvider = this.$apolloProvider;
17371748

17381749
if (this._apolloLaunched || !apolloProvider) return;
@@ -1762,46 +1773,31 @@ var launch = function launch() {
17621773
// Apollo Data
17631774
Object.defineProperty(this, '$apolloData', {
17641775
get: function get$$1() {
1765-
return _this.$data.$apolloData;
1776+
return _this2.$data.$apolloData;
17661777
},
17671778
enumerable: true,
17681779
configurable: true
17691780
});
17701781

17711782
// watchQuery
1772-
1773-
var _loop = function _loop(key) {
1783+
for (var key in apollo) {
17741784
if (key.charAt(0) !== '$') {
17751785
var options = apollo[key];
1776-
// Property proxy
1777-
if (!options.manual && !hasProperty(_this, key) && !hasProperty(_this.$props, key) && !hasProperty(_this.$data, key)) {
1778-
Object.defineProperty(_this, key, {
1779-
get: function get$$1() {
1780-
return _this.$data.$apolloData.data[key];
1781-
},
1782-
enumerable: true,
1783-
configurable: true
1784-
});
1785-
}
1786-
_this.$apollo.addSmartQuery(key, options);
1786+
this.$apollo.addSmartQuery(key, options);
17871787
}
1788-
};
1789-
1790-
for (var key in apollo) {
1791-
_loop(key);
17921788
}
17931789

17941790
if (apollo.subscribe) {
17951791
Globals.Vue.util.warn('vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.');
17961792
}
17971793

17981794
if (apollo.$subscribe) {
1799-
for (var key in apollo.$subscribe) {
1800-
this.$apollo.addSmartSubscription(key, apollo.$subscribe[key]);
1795+
for (var _key in apollo.$subscribe) {
1796+
this.$apollo.addSmartSubscription(_key, apollo.$subscribe[_key]);
18011797
}
18021798
}
18031799
}
1804-
};
1800+
}
18051801

18061802
function defineReactiveSetter($apollo, key, value, deep) {
18071803
if (typeof value !== 'undefined') {
@@ -1875,6 +1871,7 @@ function install(Vue, options) {
18751871
}
18761872
} : {}, {
18771873

1874+
beforeCreate: proxyData,
18781875
created: launch,
18791876

18801877
destroyed: function destroyed() {
@@ -1899,7 +1896,7 @@ function install(Vue, options) {
18991896
ApolloProvider.install = install;
19001897

19011898
// eslint-disable-next-line no-undef
1902-
ApolloProvider.version = "3.0.0-beta.13";
1899+
ApolloProvider.version = "3.0.0-beta.14";
19031900

19041901
// Apollo provider
19051902
var ApolloProvider$1 = ApolloProvider;

0 commit comments

Comments
 (0)