diff --git a/dist/vue-apollo.esm.js b/dist/vue-apollo.esm.js index 8bb08e7a..f958cd70 100644 --- a/dist/vue-apollo.esm.js +++ b/dist/vue-apollo.esm.js @@ -2612,6 +2612,19 @@ var SmartApollo = function () { value: function nextResult() { throw new Error('Not implemented'); } + }, { + key: 'errorHandler', + value: function errorHandler() { + var _options$error, _vm$$apollo$error, _vm$$apollo$provider$; + + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + this.options.error && (_options$error = this.options.error).call.apply(_options$error, [this.vm].concat(args)); + this.vm.$apollo.error && (_vm$$apollo$error = this.vm.$apollo.error).call.apply(_vm$$apollo$error, [this.vm].concat(args)); + this.vm.$apollo.provider.errorHandler && (_vm$$apollo$provider$ = this.vm.$apollo.provider.errorHandler).call.apply(_vm$$apollo$provider$, [this.vm].concat(args)); + } }, { key: 'catchError', value: function catchError(error) { @@ -2654,9 +2667,7 @@ var SmartApollo = function () { } } - if (typeof this.options.error === 'function') { - this.options.error.call(this.vm, error); - } + this.errorHandler(error); } }, { key: 'destroy', @@ -2807,16 +2818,28 @@ var SmartQuery = function (_SmartApollo) { get(SmartQuery.prototype.__proto__ || Object.getPrototypeOf(SmartQuery.prototype), 'catchError', this).call(this, error); this.loadingDone(); } + }, { + key: 'watchLoading', + value: function watchLoading() { + var _options$watchLoading, _vm$$apollo$watchLoad, _vm$$apollo$provider$2; + + for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + args[_key2] = arguments[_key2]; + } + + this.options.watchLoading && (_options$watchLoading = this.options.watchLoading).call.apply(_options$watchLoading, [this.vm].concat(args)); + this.vm.$apollo.watchLoading && (_vm$$apollo$watchLoad = this.vm.$apollo.watchLoading).call.apply(_vm$$apollo$watchLoad, [this.vm].concat(args)); + this.vm.$apollo.provider.watchLoading && (_vm$$apollo$provider$2 = this.vm.$apollo.provider.watchLoading).call.apply(_vm$$apollo$provider$2, [this.vm].concat(args)); + } }, { key: 'applyLoadingModifier', value: function applyLoadingModifier(value) { - if (this.options.loadingKey) { - this.vm[this.options.loadingKey] += value; + var loadingKey = this.loadingKey; + if (loadingKey && typeof this.vm[loadingKey] === 'number') { + this.vm[loadingKey] += value; } - if (this.options.watchLoading) { - this.options.watchLoading.call(this.vm, value === 1, value); - } + this.watchLoading(value === 1, value); } }, { key: 'loadingDone', @@ -2909,6 +2932,11 @@ var SmartQuery = function (_SmartApollo) { return (_observer4 = this.observer).stopPolling.apply(_observer4, arguments); } } + }, { + key: 'loadingKey', + get: function get$$1() { + return this.options.loadingKey || this.vm.$apollo.loadingKey; + } }]); return SmartQuery; }(SmartApollo); @@ -2923,8 +2951,8 @@ var SmartSubscription = function (_SmartApollo2) { classCallCheck(this, SmartSubscription); - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; + for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { + args[_key3] = arguments[_key3]; } return _ret = (_temp = (_this6 = possibleConstructorReturn(this, (_ref = SmartSubscription.__proto__ || Object.getPrototypeOf(SmartSubscription)).call.apply(_ref, [this].concat(args))), _this6), _this6.type = 'subscription', _this6.vueApolloSpecialKeys = ['variables', 'result', 'error', 'throttle', 'debounce', 'linkedQuery'], _temp), possibleConstructorReturn(_this6, _ret); @@ -2976,6 +3004,8 @@ var DollarApollo = function () { this.queries = {}; this.subscriptions = {}; this.client = undefined; + this.loadingKey = undefined; + this.error = undefined; } createClass(DollarApollo, [{ @@ -3168,6 +3198,9 @@ var ApolloProvider$1 = function () { } this.clients = options.clients || {}; this.clients.defaultClient = this.defaultClient = options.defaultClient; + this.defaultOptions = options.defaultOptions; + this.watchLoading = options.watchLoading; + this.errorHandler = options.errorHandler; this.prefetchQueries = []; } @@ -3286,16 +3319,19 @@ var ApolloProvider$1 = function () { result = prefetch; } - var optVariables = queryOptions.variables; - if (!result) { return Promise.resolve(); - } else if (prefetchType === 'boolean' && typeof optVariables !== 'undefined') { - // Reuse `variables` option with `prefetch: true` - if (typeof optVariables === 'function') { - variables = optVariables.call(context); + } else if (prefetchType === 'boolean') { + var optVariables = queryOptions.variables; + if (typeof optVariables !== 'undefined') { + // Reuse `variables` option with `prefetch: true` + if (typeof optVariables === 'function') { + variables = optVariables.call(context); + } else { + variables = optVariables; + } } else { - variables = optVariables; + variables = undefined; } } else { variables = result; @@ -3351,8 +3387,11 @@ function willPrefetch(component) { var keywords = ['$subscribe']; var prepare = function prepare() { + var apolloProvider = void 0; if (this.$options.apolloProvider) { - this._apolloProvider = this.$options.apolloProvider; + apolloProvider = this._apolloProvider = this.$options.apolloProvider; + } else { + apolloProvider = this.$root._apolloProvider; } if (this._apolloPrepared) return; @@ -3360,9 +3399,20 @@ var prepare = function prepare() { // Prepare properties var apollo = this.$options.apollo; + if (apollo) { this._apolloQueries = {}; this._apolloInitData = {}; + this.$apollo = new DollarApollo(this); + + if (!apollo.$init) { + apollo.$init = true; + + // Default options applied to `apollo` options + if (apolloProvider.defaultOptions) { + apollo = this.$options.apollo = Object.assign({}, apolloProvider.defaultOptions, apollo); + } + } // watchQuery for (var key in apollo) { @@ -3378,6 +3428,16 @@ var launch = function launch() { if (this._apolloLaunched) return; this._apolloLaunched = true; + var apollo = this.$options.apollo; + if (apollo) { + defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll); + defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries); + defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions); + defineReactiveSetter(this.$apollo, 'client', apollo.$client); + defineReactiveSetter(this.$apollo, 'loadingKey', apollo.$loadingKey); + defineReactiveSetter(this.$apollo, 'error', apollo.$error); + } + if (this._apolloQueries) { // watchQuery for (var key in this._apolloQueries) { @@ -3385,7 +3445,6 @@ var launch = function launch() { } } - var apollo = this.$options.apollo; if (apollo) { if (apollo.subscribe) { Globals.Vue.util.warn('vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.'); @@ -3396,11 +3455,6 @@ var launch = function launch() { this.$apollo.addSmartSubscription(_key, apollo.$subscribe[_key]); } } - - defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll); - defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries); - defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions); - defineReactiveSetter(this.$apollo, 'client', apollo.$client); } }; @@ -3438,16 +3492,6 @@ function install(Vue, options) { return Object.assign(map, merge(toData, fromData)); }; - // Lazy creation - Object.defineProperty(Vue.prototype, '$apollo', { - get: function get() { - if (!this._apollo) { - this._apollo = new DollarApollo(this); - } - return this._apollo; - } - }); - Vue.mixin({ // Vue 1.x diff --git a/dist/vue-apollo.min.js b/dist/vue-apollo.min.js index f2917f38..6c9efde6 100644 --- a/dist/vue-apollo.min.js +++ b/dist/vue-apollo.min.js @@ -1 +1 @@ -!function(t){"use strict";function e(t,e,r){switch(r.length){case 0:return t.call(e);case 1:return t.call(e,r[0]);case 2:return t.call(e,r[0],r[1]);case 3:return t.call(e,r[0],r[1],r[2])}return t.apply(e,r)}function r(t,e){var r=t?t.length:0;return!!r&&u(t,e,0)>-1}function i(t,e,r){for(var i=-1,n=t?t.length:0;++i-1}function A(t,e){var r=this.__data__,i=V(r,t);return i<0?r.push([t,e]):r[i][1]=e,this}function P(t){var e=-1,r=t?t.length:0;for(this.clear();++e=Tt&&(a=f,l=!1,e=new M(e));t:for(;++u0&&r(a)?e>1?R(a,e-1,r,i,n):o(n,a):i||(n[n.length]=a)}return n}function D(t,e,r){var i=e(t);return he(t)?i:o(i,r(t))}function I(t){if(!lt(t)||Y(t))return!1;var e=ut(t)||p(t)?te:It;return e.test(rt(t))}function K(t){if(!lt(t))return tt(t);var e=Z(t),r=[];for(var i in t)("constructor"!=i||!e&&Yt.call(t,i))&&r.push(i);return r}function F(t,e){return t=Object(t),W(t,e,function(e,r){return r in t})}function W(t,e,r){for(var i=-1,n=e.length,o={};++i-1&&t%1==0&&t-1&&t%1==0&&t<=Lt}function lt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function ct(t){return!!t&&"object"==typeof t}function ft(t){return"symbol"==typeof t||ct(t)&&Zt.call(t)==Rt}function ht(t){return ot(t)?C(t,!0):K(t)}function pt(){return[]}function vt(t,e,r){function i(e){var r=h,i=p;return h=p=void 0,g=e,y=t.apply(i,r)}function n(t){return g=t,b=setTimeout(u,e),_?i(t):y}function o(t){var r=t-d,i=t-g,n=e-r;return m?xe(n,v-i):n}function s(t){var r=t-d,i=t-g;return void 0===d||r>=e||r<0||m&&i>=v}function u(){var t=Ee();return s(t)?a(t):void(b=setTimeout(u,o(t)))}function a(t){return b=void 0,w&&h?i(t):(h=p=void 0,y)}function l(){void 0!==b&&clearTimeout(b),g=0,h=d=p=b=void 0}function c(){return void 0===b?y:a(Ee())}function f(){var t=Ee(),r=s(t);if(h=arguments,p=this,d=t,r){if(void 0===b)return n(d);if(m)return b=setTimeout(u,e),i(d)}return void 0===b&&(b=setTimeout(u,e)),y}var h,p,v,y,b,d,g=0,_=!1,m=!1,w=!0;if("function"!=typeof t)throw new TypeError(ye);return e=_t(e)||0,bt(r)&&(_=!!r.leading,m="maxWait"in r,v=m?$e(_t(r.maxWait)||0,e):v,w="trailing"in r?!!r.trailing:w),f.cancel=l,f.flush=c,f}function yt(t,e,r){var i=!0,n=!0;if("function"!=typeof t)throw new TypeError(ye);return bt(r)&&(i="leading"in r?!!r.leading:i,n="trailing"in r?!!r.trailing:n),vt(t,e,{leading:i,maxWait:e,trailing:n})}function bt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function dt(t){return!!t&&"object"==typeof t}function gt(t){return"symbol"==typeof t||dt(t)&&Pe.call(t)==de}function _t(t){if("number"==typeof t)return t;if(gt(t))return be;if(bt(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=bt(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(ge,"");var r=me.test(t);return r||we.test(t)?ke(t.slice(2),r?2:8):_e.test(t)?be:+t}function mt(t,e,r){function i(e){var r=h,i=p;return h=p=void 0,g=e,y=t.apply(i,r)}function n(t){return g=t,b=setTimeout(u,e),_?i(t):y}function o(t){var r=t-d,i=t-g,n=e-r;return m?Je(n,v-i):n}function s(t){var r=t-d,i=t-g;return void 0===d||r>=e||r<0||m&&i>=v}function u(){var t=Ue();return s(t)?a(t):void(b=setTimeout(u,o(t)))}function a(t){return b=void 0,w&&h?i(t):(h=p=void 0,y)}function l(){void 0!==b&&clearTimeout(b),g=0,h=d=p=b=void 0}function c(){return void 0===b?y:a(Ue())}function f(){var t=Ue(),r=s(t);if(h=arguments,p=this,d=t,r){if(void 0===b)return n(d);if(m)return b=setTimeout(u,e),i(d)}return void 0===b&&(b=setTimeout(u,e)),y}var h,p,v,y,b,d,g=0,_=!1,m=!1,w=!0;if("function"!=typeof t)throw new TypeError(Te);return e=jt(e)||0,wt(r)&&(_=!!r.leading,m="maxWait"in r,v=m?Ge(jt(r.maxWait)||0,e):v,w="trailing"in r?!!r.trailing:w),f.cancel=l,f.flush=c,f}function wt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function kt(t){return!!t&&"object"==typeof t}function Ot(t){return"symbol"==typeof t||kt(t)&&We.call(t)==qe}function jt(t){if("number"==typeof t)return t;if(Ot(t))return Me;if(wt(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=wt(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(Le,"");var r=Ve.test(t);return r||Ne.test(t)?Re(t.slice(2),r?2:8):Ce.test(t)?Me:+t}function St(t){return function(e,r){return"number"==typeof r?t(e,r):t(e,r.wait,r)}}function At(t){return Be.Vue.util.mergeOptions({},t)}function Pt(t,e){for(;"function"==typeof t;)t=t.call(e);return t}function $t(t){return pr.push(t),t}function xt(t,e,r){"undefined"!=typeof r&&("function"==typeof r?t.defineReactiveSetter(e,r):t[e]=r)}function Et(t,e){if(!Et.installed){Et.installed=!0,Be.Vue=t;var r=t.config.optionMergeStrategies.methods;t.config.optionMergeStrategies.apollo=function(t,e,i){if(!t)return e;if(!e)return t;for(var n=Object.assign({},ve(t,vr),t.data),o=Object.assign({},ve(e,vr),e.data),s={},u=0;u3&&void 0!==arguments[3])||arguments[3];if(tr(this,t),this.type=null,this.vueApolloSpecialKeys=[],this.vm=e,this.key=r,this.options=Object.assign({},i),this._skip=!1,this._watchers=[],"function"==typeof this.options.query){var s=this.options.query.bind(this.vm);this.options.query=s(),this._watchers.push(this.vm.$watch(s,function(t){n.options.query=t,n.refresh()}))}if("function"==typeof this.options.document){var u=this.options.document.bind(this.vm);this.options.document=u(),this._watchers.push(this.vm.$watch(u,function(t){n.options.document=t,n.refresh()}))}o&&this.autostart()}return er(t,[{key:"autostart",value:function(){"function"==typeof this.options.skip?this._watchers.push(this.vm.$watch(this.options.skip.bind(this.vm),this.skipChanged.bind(this),{immediate:!0})):this.options.skip?this._skip=!0:this.start()}},{key:"skipChanged",value:function(t,e){t!==e&&(this.skip=t)}},{key:"refresh",value:function(){this._skip||(this.stop(),this.start())}},{key:"start",value:function(){var t=this;if(this.starting=!0,"function"==typeof this.options.variables){var e=this.executeApollo.bind(this);e=this.options.throttle?He(e,this.options.throttle):e,e=this.options.debounce?Xe(e,this.options.debounce):e,this.unwatchVariables=this.vm.$watch(function(){return t.options.variables.call(t.vm)},e,{immediate:!0})}else this.executeApollo(this.options.variables)}},{key:"stop",value:function(){this.unwatchVariables&&(this.unwatchVariables(),this.unwatchVariables=null),this.sub&&(this.sub.unsubscribe(),this.sub=null)}},{key:"generateApolloOptions",value:function(t){var e=ve(this.options,this.vueApolloSpecialKeys);return e.variables=t,e}},{key:"executeApollo",value:function(t){this.starting=!1}},{key:"nextResult",value:function(){throw new Error("Not implemented")}},{key:"catchError",value:function(t){if(t.graphQLErrors&&0!==t.graphQLErrors.length){console.error("GraphQL execution errors for "+this.type+" '"+this.key+"'");var e=!0,r=!1,i=void 0;try{for(var n,o=t.graphQLErrors[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var s=n.value;console.error(s)}}catch(t){r=!0,i=t}finally{try{!e&&o.return&&o.return()}finally{if(r)throw i}}}else if(t.networkError)console.error("Error sending the "+this.type+" '"+this.key+"'",t.networkError);else if(console.error("[vue-apollo] An error has occured for "+this.type+" '"+this.key+"'"),Array.isArray(t)){var u;(u=console).error.apply(u,ur(t))}else console.error(t);"function"==typeof this.options.error&&this.options.error.call(this.vm,t)}},{key:"destroy",value:function(){this.stop();var t=!0,e=!1,r=void 0;try{for(var i,n=this._watchers[Symbol.iterator]();!(t=(i=n.next()).done);t=!0){var o=i.value;o()}}catch(t){e=!0,r=t}finally{try{!t&&n.return&&n.return()}finally{if(e)throw r}}}},{key:"skip",get:function(){return this._skip},set:function(t){t?this.stop():this.start(),this._skip=t}}]),t}(),lr=function(t){function e(t,r,i){var n=!(arguments.length>3&&void 0!==arguments[3])||arguments[3];if(tr(this,e),!i.query){var o=i;i={query:o}}var s=sr(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,r,i,n));return s.type="query",s.vueApolloSpecialKeys=Ye,s.loading=!1,s}return or(e,t),er(e,[{key:"stop",value:function(){nr(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"stop",this).call(this),this.observer&&(this.observer.stopPolling(),this.observer=null)}},{key:"executeApollo",value:function(t){this.observer?this.observer.setOptions(this.generateApolloOptions(t)):(this.sub&&this.sub.unsubscribe(),this.observer=this.vm.$apollo.watchQuery(this.generateApolloOptions(t)),this.sub=this.observer.subscribe({next:this.nextResult.bind(this),error:this.catchError.bind(this)})),this.maySetLoading(),nr(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"executeApollo",this).call(this,t)}},{key:"maySetLoading",value:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=this.observer.currentResult();(t||e.loading)&&(this.loading||this.applyLoadingModifier(1),this.loading=!0)}},{key:"nextResult",value:function(t){var e=t.data,r=t.loading;r||this.loadingDone(),"undefined"==typeof e||("function"==typeof this.options.update?this.vm[this.key]=this.options.update.call(this.vm,e):void 0===e[this.key]?console.error("Missing "+this.key+" attribute on result",e):this.vm[this.key]=e[this.key]),"function"==typeof this.options.result&&this.options.result.call(this.vm,t)}},{key:"catchError",value:function(t){nr(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"catchError",this).call(this,t),this.loadingDone()}},{key:"applyLoadingModifier",value:function(t){this.options.loadingKey&&(this.vm[this.options.loadingKey]+=t),this.options.watchLoading&&this.options.watchLoading.call(this.vm,1===t,t)}},{key:"loadingDone",value:function(){this.loading&&this.applyLoadingModifier(-1),this.loading=!1}},{key:"fetchMore",value:function(){var t=this;if(this.observer){var e;return this.maySetLoading(!0),(e=this.observer).fetchMore.apply(e,arguments).then(function(e){return e.loading||t.loadingDone(),e})}}},{key:"subscribeToMore",value:function(){if(this.observer){var t;return{unsubscribe:(t=this.observer).subscribeToMore.apply(t,arguments)}}}},{key:"refetch",value:function(t){var e=this;if(t&&(this.options.variables=t),this.observer){var r=this.observer.refetch(t).then(function(t){return t.loading||e.loadingDone(),t});return this.maySetLoading(),r}}},{key:"setVariables",value:function(t,e){if(this.options.variables=t,this.observer){var r=this.observer.setVariables(t,e);return this.maySetLoading(),r}}},{key:"setOptions",value:function(t){if(Object.assign(this.options,t),this.observer){var e=this.observer.setOptions(t);return this.maySetLoading(),e}}},{key:"startPolling",value:function(){if(this.observer){var t;return(t=this.observer).startPolling.apply(t,arguments)}}},{key:"stopPolling",value:function(){if(this.observer){var t;return(t=this.observer).stopPolling.apply(t,arguments)}}}]),e}(ar),cr=function(t){function e(){var t,r,i,n;tr(this,e);for(var o=arguments.length,s=Array(o),u=0;u `subscribe` option is deprecated. Use the `$subscribe` option instead."),e.$subscribe)for(var r in e.$subscribe)this.$apollo.addSmartSubscription(r,e.$subscribe[r]);xt(this.$apollo,"skipAll",e.$skipAll),xt(this.$apollo,"skipAllQueries",e.$skipAllQueries),xt(this.$apollo,"skipAllSubscriptions",e.$skipAllSubscriptions),xt(this.$apollo,"client",e.$client)}}};hr.install=Et;var dr=hr,gr=null;"undefined"!=typeof window?gr=window.Vue:"undefined"!=typeof global&&(gr=global.Vue),gr&&gr.use(hr),t.install=Et,t.ApolloProvider=dr,t.default=hr,t.willPrefetch=$t}(this.VueApollo=this.VueApollo||{}); +!function(t){"use strict";function e(t,e,r){switch(r.length){case 0:return t.call(e);case 1:return t.call(e,r[0]);case 2:return t.call(e,r[0],r[1]);case 3:return t.call(e,r[0],r[1],r[2])}return t.apply(e,r)}function r(t,e){var r=t?t.length:0;return!!r&&a(t,e,0)>-1}function i(t,e,r){for(var i=-1,n=t?t.length:0;++i-1}function $(t,e){var r=this.__data__,i=K(r,t);return i<0?r.push([t,e]):r[i][1]=e,this}function A(t){var e=-1,r=t?t.length:0;for(this.clear();++e=Lt&&(u=f,l=!1,e=new T(e));t:for(;++a0&&r(u)?e>1?N(u,e-1,r,i,n):o(n,u):i||(n[n.length]=u)}return n}function R(t,e,r){var i=e(t);return he(t)?i:o(i,r(t))}function D(t){if(!lt(t)||Y(t))return!1;var e=at(t)||p(t)?te:Dt;return e.test(rt(t))}function I(t){if(!lt(t))return tt(t);var e=Z(t),r=[];for(var i in t)("constructor"!=i||!e&&Yt.call(t,i))&&r.push(i);return r}function F(t,e){return t=Object(t),H(t,e,function(e,r){return r in t})}function H(t,e,r){for(var i=-1,n=e.length,o={};++i-1&&t%1==0&&t-1&&t%1==0&&t<=qt}function lt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function ct(t){return!!t&&"object"==typeof t}function ft(t){return"symbol"==typeof t||ct(t)&&Zt.call(t)==Nt}function ht(t){return ot(t)?C(t,!0):I(t)}function pt(){return[]}function vt(t,e,r){function i(e){var r=h,i=p;return h=p=void 0,g=e,y=t.apply(i,r)}function n(t){return g=t,d=setTimeout(a,e),_?i(t):y}function o(t){var r=t-b,i=t-g,n=e-r;return m?xe(n,v-i):n}function s(t){var r=t-b,i=t-g;return void 0===b||r>=e||r<0||m&&i>=v}function a(){var t=Ee();return s(t)?u(t):void(d=setTimeout(a,o(t)))}function u(t){return d=void 0,w&&h?i(t):(h=p=void 0,y)}function l(){void 0!==d&&clearTimeout(d),g=0,h=b=p=d=void 0}function c(){return void 0===d?y:u(Ee())}function f(){var t=Ee(),r=s(t);if(h=arguments,p=this,b=t,r){if(void 0===d)return n(b);if(m)return d=setTimeout(a,e),i(b)}return void 0===d&&(d=setTimeout(a,e)),y}var h,p,v,y,d,b,g=0,_=!1,m=!1,w=!0;if("function"!=typeof t)throw new TypeError(ye);return e=_t(e)||0,dt(r)&&(_=!!r.leading,m="maxWait"in r,v=m?Pe(_t(r.maxWait)||0,e):v,w="trailing"in r?!!r.trailing:w),f.cancel=l,f.flush=c,f}function yt(t,e,r){var i=!0,n=!0;if("function"!=typeof t)throw new TypeError(ye);return dt(r)&&(i="leading"in r?!!r.leading:i,n="trailing"in r?!!r.trailing:n),vt(t,e,{leading:i,maxWait:e,trailing:n})}function dt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function bt(t){return!!t&&"object"==typeof t}function gt(t){return"symbol"==typeof t||bt(t)&&Ae.call(t)==be}function _t(t){if("number"==typeof t)return t;if(gt(t))return de;if(dt(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=dt(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(ge,"");var r=me.test(t);return r||we.test(t)?ke(t.slice(2),r?2:8):_e.test(t)?de:+t}function mt(t,e,r){function i(e){var r=h,i=p;return h=p=void 0,g=e,y=t.apply(i,r)}function n(t){return g=t,d=setTimeout(a,e),_?i(t):y}function o(t){var r=t-b,i=t-g,n=e-r;return m?Ge(n,v-i):n}function s(t){var r=t-b,i=t-g;return void 0===b||r>=e||r<0||m&&i>=v}function a(){var t=Je();return s(t)?u(t):void(d=setTimeout(a,o(t)))}function u(t){return d=void 0,w&&h?i(t):(h=p=void 0,y)}function l(){void 0!==d&&clearTimeout(d),g=0,h=b=p=d=void 0}function c(){return void 0===d?y:u(Je())}function f(){var t=Je(),r=s(t);if(h=arguments,p=this,b=t,r){if(void 0===d)return n(b);if(m)return d=setTimeout(a,e),i(b)}return void 0===d&&(d=setTimeout(a,e)),y}var h,p,v,y,d,b,g=0,_=!1,m=!1,w=!0;if("function"!=typeof t)throw new TypeError(Le);return e=jt(e)||0,wt(r)&&(_=!!r.leading,m="maxWait"in r,v=m?We(jt(r.maxWait)||0,e):v,w="trailing"in r?!!r.trailing:w),f.cancel=l,f.flush=c,f}function wt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function kt(t){return!!t&&"object"==typeof t}function Ot(t){return"symbol"==typeof t||kt(t)&&He.call(t)==Me}function jt(t){if("number"==typeof t)return t;if(Ot(t))return Te;if(wt(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=wt(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(qe,"");var r=Ke.test(t);return r||Ve.test(t)?Ne(t.slice(2),r?2:8):Ce.test(t)?Te:+t}function St(t){return function(e,r){return"number"==typeof r?t(e,r):t(e,r.wait,r)}}function $t(t){return ze.Vue.util.mergeOptions({},t)}function At(t,e){for(;"function"==typeof t;)t=t.call(e);return t}function Pt(t){return pr.push(t),t}function xt(t,e,r){"undefined"!=typeof r&&("function"==typeof r?t.defineReactiveSetter(e,r):t[e]=r)}function Et(t,e){if(!Et.installed){Et.installed=!0,ze.Vue=t;var r=t.config.optionMergeStrategies.methods;t.config.optionMergeStrategies.apollo=function(t,e,i){if(!t)return e;if(!e)return t;for(var n=Object.assign({},ve(t,vr),t.data),o=Object.assign({},ve(e,vr),e.data),s={},a=0;a3&&void 0!==arguments[3])||arguments[3];if(tr(this,t),this.type=null,this.vueApolloSpecialKeys=[],this.vm=e,this.key=r,this.options=Object.assign({},i),this._skip=!1,this._watchers=[],"function"==typeof this.options.query){var s=this.options.query.bind(this.vm);this.options.query=s(),this._watchers.push(this.vm.$watch(s,function(t){n.options.query=t,n.refresh()}))}if("function"==typeof this.options.document){var a=this.options.document.bind(this.vm);this.options.document=a(),this._watchers.push(this.vm.$watch(a,function(t){n.options.document=t,n.refresh()}))}o&&this.autostart()}return er(t,[{key:"autostart",value:function(){"function"==typeof this.options.skip?this._watchers.push(this.vm.$watch(this.options.skip.bind(this.vm),this.skipChanged.bind(this),{immediate:!0})):this.options.skip?this._skip=!0:this.start()}},{key:"skipChanged",value:function(t,e){t!==e&&(this.skip=t)}},{key:"refresh",value:function(){this._skip||(this.stop(),this.start())}},{key:"start",value:function(){var t=this;if(this.starting=!0,"function"==typeof this.options.variables){var e=this.executeApollo.bind(this);e=this.options.throttle?Be(e,this.options.throttle):e,e=this.options.debounce?Xe(e,this.options.debounce):e,this.unwatchVariables=this.vm.$watch(function(){return t.options.variables.call(t.vm)},e,{immediate:!0})}else this.executeApollo(this.options.variables)}},{key:"stop",value:function(){this.unwatchVariables&&(this.unwatchVariables(),this.unwatchVariables=null),this.sub&&(this.sub.unsubscribe(),this.sub=null)}},{key:"generateApolloOptions",value:function(t){var e=ve(this.options,this.vueApolloSpecialKeys);return e.variables=t,e}},{key:"executeApollo",value:function(t){this.starting=!1}},{key:"nextResult",value:function(){throw new Error("Not implemented")}},{key:"errorHandler",value:function(){for(var t,e,r,i=arguments.length,n=Array(i),o=0;o3&&void 0!==arguments[3])||arguments[3];if(tr(this,e),!i.query){var o=i;i={query:o}}var s=sr(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,r,i,n));return s.type="query",s.vueApolloSpecialKeys=Ye,s.loading=!1,s}return or(e,t),er(e,[{key:"stop",value:function(){nr(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"stop",this).call(this),this.observer&&(this.observer.stopPolling(),this.observer=null)}},{key:"executeApollo",value:function(t){this.observer?this.observer.setOptions(this.generateApolloOptions(t)):(this.sub&&this.sub.unsubscribe(),this.observer=this.vm.$apollo.watchQuery(this.generateApolloOptions(t)),this.sub=this.observer.subscribe({next:this.nextResult.bind(this),error:this.catchError.bind(this)})),this.maySetLoading(),nr(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"executeApollo",this).call(this,t)}},{key:"maySetLoading",value:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=this.observer.currentResult();(t||e.loading)&&(this.loading||this.applyLoadingModifier(1),this.loading=!0)}},{key:"nextResult",value:function(t){var e=t.data,r=t.loading;r||this.loadingDone(),"undefined"==typeof e||("function"==typeof this.options.update?this.vm[this.key]=this.options.update.call(this.vm,e):void 0===e[this.key]?console.error("Missing "+this.key+" attribute on result",e):this.vm[this.key]=e[this.key]),"function"==typeof this.options.result&&this.options.result.call(this.vm,t)}},{key:"catchError",value:function(t){nr(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"catchError",this).call(this,t),this.loadingDone()}},{key:"watchLoading",value:function(){for(var t,e,r,i=arguments.length,n=Array(i),o=0;o `subscribe` option is deprecated. Use the `$subscribe` option instead."),t.$subscribe))for(var r in t.$subscribe)this.$apollo.addSmartSubscription(r,t.$subscribe[r])}};hr.install=Et;var br=hr,gr=null;"undefined"!=typeof window?gr=window.Vue:"undefined"!=typeof global&&(gr=global.Vue),gr&&gr.use(hr),t.install=Et,t.ApolloProvider=br,t.default=hr,t.willPrefetch=Pt}(this.VueApollo=this.VueApollo||{}); diff --git a/dist/vue-apollo.umd.js b/dist/vue-apollo.umd.js index be5a6671..4e8e9533 100644 --- a/dist/vue-apollo.umd.js +++ b/dist/vue-apollo.umd.js @@ -2618,6 +2618,19 @@ var SmartApollo = function () { value: function nextResult() { throw new Error('Not implemented'); } + }, { + key: 'errorHandler', + value: function errorHandler() { + var _options$error, _vm$$apollo$error, _vm$$apollo$provider$; + + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + this.options.error && (_options$error = this.options.error).call.apply(_options$error, [this.vm].concat(args)); + this.vm.$apollo.error && (_vm$$apollo$error = this.vm.$apollo.error).call.apply(_vm$$apollo$error, [this.vm].concat(args)); + this.vm.$apollo.provider.errorHandler && (_vm$$apollo$provider$ = this.vm.$apollo.provider.errorHandler).call.apply(_vm$$apollo$provider$, [this.vm].concat(args)); + } }, { key: 'catchError', value: function catchError(error) { @@ -2660,9 +2673,7 @@ var SmartApollo = function () { } } - if (typeof this.options.error === 'function') { - this.options.error.call(this.vm, error); - } + this.errorHandler(error); } }, { key: 'destroy', @@ -2813,16 +2824,28 @@ var SmartQuery = function (_SmartApollo) { get(SmartQuery.prototype.__proto__ || Object.getPrototypeOf(SmartQuery.prototype), 'catchError', this).call(this, error); this.loadingDone(); } + }, { + key: 'watchLoading', + value: function watchLoading() { + var _options$watchLoading, _vm$$apollo$watchLoad, _vm$$apollo$provider$2; + + for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + args[_key2] = arguments[_key2]; + } + + this.options.watchLoading && (_options$watchLoading = this.options.watchLoading).call.apply(_options$watchLoading, [this.vm].concat(args)); + this.vm.$apollo.watchLoading && (_vm$$apollo$watchLoad = this.vm.$apollo.watchLoading).call.apply(_vm$$apollo$watchLoad, [this.vm].concat(args)); + this.vm.$apollo.provider.watchLoading && (_vm$$apollo$provider$2 = this.vm.$apollo.provider.watchLoading).call.apply(_vm$$apollo$provider$2, [this.vm].concat(args)); + } }, { key: 'applyLoadingModifier', value: function applyLoadingModifier(value) { - if (this.options.loadingKey) { - this.vm[this.options.loadingKey] += value; + var loadingKey = this.loadingKey; + if (loadingKey && typeof this.vm[loadingKey] === 'number') { + this.vm[loadingKey] += value; } - if (this.options.watchLoading) { - this.options.watchLoading.call(this.vm, value === 1, value); - } + this.watchLoading(value === 1, value); } }, { key: 'loadingDone', @@ -2915,6 +2938,11 @@ var SmartQuery = function (_SmartApollo) { return (_observer4 = this.observer).stopPolling.apply(_observer4, arguments); } } + }, { + key: 'loadingKey', + get: function get$$1() { + return this.options.loadingKey || this.vm.$apollo.loadingKey; + } }]); return SmartQuery; }(SmartApollo); @@ -2929,8 +2957,8 @@ var SmartSubscription = function (_SmartApollo2) { classCallCheck(this, SmartSubscription); - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; + for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { + args[_key3] = arguments[_key3]; } return _ret = (_temp = (_this6 = possibleConstructorReturn(this, (_ref = SmartSubscription.__proto__ || Object.getPrototypeOf(SmartSubscription)).call.apply(_ref, [this].concat(args))), _this6), _this6.type = 'subscription', _this6.vueApolloSpecialKeys = ['variables', 'result', 'error', 'throttle', 'debounce', 'linkedQuery'], _temp), possibleConstructorReturn(_this6, _ret); @@ -2982,6 +3010,8 @@ var DollarApollo = function () { this.queries = {}; this.subscriptions = {}; this.client = undefined; + this.loadingKey = undefined; + this.error = undefined; } createClass(DollarApollo, [{ @@ -3174,6 +3204,9 @@ var ApolloProvider$1 = function () { } this.clients = options.clients || {}; this.clients.defaultClient = this.defaultClient = options.defaultClient; + this.defaultOptions = options.defaultOptions; + this.watchLoading = options.watchLoading; + this.errorHandler = options.errorHandler; this.prefetchQueries = []; } @@ -3292,16 +3325,19 @@ var ApolloProvider$1 = function () { result = prefetch; } - var optVariables = queryOptions.variables; - if (!result) { return Promise.resolve(); - } else if (prefetchType === 'boolean' && typeof optVariables !== 'undefined') { - // Reuse `variables` option with `prefetch: true` - if (typeof optVariables === 'function') { - variables = optVariables.call(context); + } else if (prefetchType === 'boolean') { + var optVariables = queryOptions.variables; + if (typeof optVariables !== 'undefined') { + // Reuse `variables` option with `prefetch: true` + if (typeof optVariables === 'function') { + variables = optVariables.call(context); + } else { + variables = optVariables; + } } else { - variables = optVariables; + variables = undefined; } } else { variables = result; @@ -3357,8 +3393,11 @@ function willPrefetch(component) { var keywords = ['$subscribe']; var prepare = function prepare() { + var apolloProvider = void 0; if (this.$options.apolloProvider) { - this._apolloProvider = this.$options.apolloProvider; + apolloProvider = this._apolloProvider = this.$options.apolloProvider; + } else { + apolloProvider = this.$root._apolloProvider; } if (this._apolloPrepared) return; @@ -3366,9 +3405,20 @@ var prepare = function prepare() { // Prepare properties var apollo = this.$options.apollo; + if (apollo) { this._apolloQueries = {}; this._apolloInitData = {}; + this.$apollo = new DollarApollo(this); + + if (!apollo.$init) { + apollo.$init = true; + + // Default options applied to `apollo` options + if (apolloProvider.defaultOptions) { + apollo = this.$options.apollo = Object.assign({}, apolloProvider.defaultOptions, apollo); + } + } // watchQuery for (var key in apollo) { @@ -3384,6 +3434,16 @@ var launch = function launch() { if (this._apolloLaunched) return; this._apolloLaunched = true; + var apollo = this.$options.apollo; + if (apollo) { + defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll); + defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries); + defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions); + defineReactiveSetter(this.$apollo, 'client', apollo.$client); + defineReactiveSetter(this.$apollo, 'loadingKey', apollo.$loadingKey); + defineReactiveSetter(this.$apollo, 'error', apollo.$error); + } + if (this._apolloQueries) { // watchQuery for (var key in this._apolloQueries) { @@ -3391,7 +3451,6 @@ var launch = function launch() { } } - var apollo = this.$options.apollo; if (apollo) { if (apollo.subscribe) { Globals.Vue.util.warn('vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.'); @@ -3402,11 +3461,6 @@ var launch = function launch() { this.$apollo.addSmartSubscription(_key, apollo.$subscribe[_key]); } } - - defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll); - defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries); - defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions); - defineReactiveSetter(this.$apollo, 'client', apollo.$client); } }; @@ -3444,16 +3498,6 @@ function install(Vue, options) { return Object.assign(map, merge(toData, fromData)); }; - // Lazy creation - Object.defineProperty(Vue.prototype, '$apollo', { - get: function get() { - if (!this._apollo) { - this._apollo = new DollarApollo(this); - } - return this._apollo; - } - }); - Vue.mixin({ // Vue 1.x diff --git a/package.json b/package.json index 8c140e05..0fe875f6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-apollo", - "version": "2.1.0-beta.23", + "version": "2.1.0-rc.1", "description": "Vue apollo integration", "main": "dist/vue-apollo.umd.js", "module": "dist/vue-apollo.esm.js",