From d5a4407049f0df3fc996cda117ebde7405201836 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Thu, 19 Oct 2017 19:45:45 +0200 Subject: [PATCH] Subscriptions are no longer run on server + SSR Fixes --- dist/vue-apollo.esm.js | 59 +++++++++++++++++++++++++----------------- dist/vue-apollo.min.js | 2 +- dist/vue-apollo.umd.js | 59 +++++++++++++++++++++++++----------------- package.json | 2 +- src/apollo-provider.js | 6 ++++- src/consts.js | 2 ++ src/dollar-apollo.js | 52 +++++++++++++++++++++---------------- src/smart-apollo.js | 4 +++ 8 files changed, 112 insertions(+), 74 deletions(-) diff --git a/dist/vue-apollo.esm.js b/dist/vue-apollo.esm.js index eb93b7a..f078d01 100644 --- a/dist/vue-apollo.esm.js +++ b/dist/vue-apollo.esm.js @@ -2334,7 +2334,7 @@ function reapply(options, context) { return options; } -var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore']; +var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore', 'prefetch', 'manual']; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; @@ -2530,6 +2530,10 @@ var SmartApollo = function () { })); } + if (this.vm.$isServer) { + this.options.fetchPolicy = 'cache-first'; + } + if (autostart) { this.autostart(); } @@ -3073,14 +3077,16 @@ var DollarApollo = function () { value: function subscribe(options) { var _this2 = this; - var observable = this.getClient(options).subscribe(options); - var _subscribe = observable.subscribe.bind(observable); - observable.subscribe = function (options) { - var sub = _subscribe(options); - _this2._apolloSubscriptions.push(sub); - return sub; - }; - return observable; + if (!this.vm.$isServer) { + var observable = this.getClient(options).subscribe(options); + var _subscribe = observable.subscribe.bind(observable); + observable.subscribe = function (options) { + var sub = _subscribe(options); + _this2._apolloSubscriptions.push(sub); + return sub; + }; + return observable; + } } }, { key: 'addSmartQuery', @@ -3092,18 +3098,20 @@ var DollarApollo = function () { var smart = this.queries[key] = new SmartQuery(this.vm, key, options, false); smart.autostart(); - var subs = options.subscribeToMore; - if (subs) { - if (Array.isArray(subs)) { - subs.forEach(function (sub, index) { - _this3.addSmartSubscription('' + key + index, _extends({}, sub, { + if (!this.vm.$isServer) { + var subs = options.subscribeToMore; + if (subs) { + if (Array.isArray(subs)) { + subs.forEach(function (sub, index) { + _this3.addSmartSubscription('' + key + index, _extends({}, sub, { + linkedQuery: smart + })); + }); + } else { + this.addSmartSubscription(key, _extends({}, subs, { linkedQuery: smart })); - }); - } else { - this.addSmartSubscription(key, _extends({}, subs, { - linkedQuery: smart - })); + } } } @@ -3112,12 +3120,14 @@ var DollarApollo = function () { }, { key: 'addSmartSubscription', value: function addSmartSubscription(key, options) { - options = reapply(options, this.vm); + if (!this.vm.$isServer) { + options = reapply(options, this.vm); - var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false); - smart.autostart(); + var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false); + smart.autostart(); - return smart; + return smart; + } } }, { key: 'defineReactiveSetter', @@ -3351,8 +3361,9 @@ var ApolloProvider$1 = function () { // Query return new Promise(function (resolve, reject) { - var options = lodash_omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS); + var options = lodash_omit(queryOptions, [].concat(toConsumableArray(VUE_APOLLO_QUERY_KEYWORDS), ['fetchPolicy'])); options.variables = variables; + options.fetchPolicy = 'network-only'; client.query(options).then(resolve, reject); }); } diff --git a/dist/vue-apollo.min.js b/dist/vue-apollo.min.js index 9227396..dd499e8 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&&a(t,e,0)>-1}function i(t,e,r){for(var i=-1,n=t?t.length:0;++i-1}function S(t,e){var r=this.__data__,i=V(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=Qt&&(u=f,l=!1,e=new T(e));t:for(;++a0&&r(u)?e>1?R(u,e-1,r,i,n):o(n,u):i||(n[n.length]=u)}return n}function D(t,e,r){var i=e(t);return he(t)?i:o(i,r(t))}function N(t){if(!lt(t)||Y(t))return!1;var e=at(t)||p(t)?te:Nt;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<=Mt}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):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(Qe);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)==qe}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(Me,"");var r=Ve.test(t);return r||Ke.test(t)?Re(t.slice(2),r?2:8):Ce.test(t)?Te:+t}function $t(t){return function(e,r){return"number"==typeof r?t(e,r):t(e,r.wait,r)}}function St(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();var i="function"==typeof this.options.result;"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.options.manual?i||console.error(this.key+" query must have a 'result' hook in manual mode"):this.vm[this.key]=e[this.key]),i&&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||{}); +!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 S(t,e){var r=this.__data__,i=V(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=Qt&&(u=h,l=!1,e=new T(e));t:for(;++a0&&r(u)?e>1?R(u,e-1,r,i,n):o(n,u):i||(n[n.length]=u)}return n}function D(t,e,r){var i=e(t);return fe(t)?i:o(i,r(t))}function N(t){if(!lt(t)||Y(t))return!1;var e=at(t)||p(t)?te:Nt;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<=Mt}function lt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function ct(t){return!!t&&"object"==typeof t}function ht(t){return"symbol"==typeof t||ct(t)&&Zt.call(t)==Rt}function ft(t){return ot(t)?C(t,!0):I(t)}function pt(){return[]}function vt(t,e,r){function i(e){var r=f,i=p;return f=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&&f?i(t):(f=p=void 0,y)}function l(){void 0!==d&&clearTimeout(d),g=0,f=b=p=d=void 0}function c(){return void 0===d?y:u(Ee())}function h(){var t=Ee(),r=s(t);if(f=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 f,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),h.cancel=l,h.flush=c,h}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=f,i=p;return f=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&&f?i(t):(f=p=void 0,y)}function l(){void 0!==d&&clearTimeout(d),g=0,f=b=p=d=void 0}function c(){return void 0===d?y:u(Je())}function h(){var t=Je(),r=s(t);if(f=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 f,p,v,y,d,b,g=0,_=!1,m=!1,w=!0;if("function"!=typeof t)throw new TypeError(Qe);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),h.cancel=l,h.flush=c,h}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)==qe}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(Me,"");var r=Ve.test(t);return r||Ke.test(t)?Re(t.slice(2),r?2:8):Ce.test(t)?Te:+t}function $t(t){return function(e,r){return"number"==typeof r?t(e,r):t(e,r.wait,r)}}function St(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()}))}this.vm.$isServer&&(this.options.fetchPolicy="cache-first"),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();var i="function"==typeof this.options.result;"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.options.manual?i||console.error(this.key+" query must have a 'result' hook in manual mode"):this.vm[this.key]=e[this.key]),i&&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])}};fr.install=Et;var br=fr,gr=null;"undefined"!=typeof window?gr=window.Vue:"undefined"!=typeof global&&(gr=global.Vue),gr&&gr.use(fr),t.install=Et,t.ApolloProvider=br,t.default=fr,t.willPrefetch=Pt}(this.VueApollo=this.VueApollo||{}); diff --git a/dist/vue-apollo.umd.js b/dist/vue-apollo.umd.js index 46e8395..11acbf4 100644 --- a/dist/vue-apollo.umd.js +++ b/dist/vue-apollo.umd.js @@ -2340,7 +2340,7 @@ function reapply(options, context) { return options; } -var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore']; +var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore', 'prefetch', 'manual']; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; @@ -2536,6 +2536,10 @@ var SmartApollo = function () { })); } + if (this.vm.$isServer) { + this.options.fetchPolicy = 'cache-first'; + } + if (autostart) { this.autostart(); } @@ -3079,14 +3083,16 @@ var DollarApollo = function () { value: function subscribe(options) { var _this2 = this; - var observable = this.getClient(options).subscribe(options); - var _subscribe = observable.subscribe.bind(observable); - observable.subscribe = function (options) { - var sub = _subscribe(options); - _this2._apolloSubscriptions.push(sub); - return sub; - }; - return observable; + if (!this.vm.$isServer) { + var observable = this.getClient(options).subscribe(options); + var _subscribe = observable.subscribe.bind(observable); + observable.subscribe = function (options) { + var sub = _subscribe(options); + _this2._apolloSubscriptions.push(sub); + return sub; + }; + return observable; + } } }, { key: 'addSmartQuery', @@ -3098,18 +3104,20 @@ var DollarApollo = function () { var smart = this.queries[key] = new SmartQuery(this.vm, key, options, false); smart.autostart(); - var subs = options.subscribeToMore; - if (subs) { - if (Array.isArray(subs)) { - subs.forEach(function (sub, index) { - _this3.addSmartSubscription('' + key + index, _extends({}, sub, { + if (!this.vm.$isServer) { + var subs = options.subscribeToMore; + if (subs) { + if (Array.isArray(subs)) { + subs.forEach(function (sub, index) { + _this3.addSmartSubscription('' + key + index, _extends({}, sub, { + linkedQuery: smart + })); + }); + } else { + this.addSmartSubscription(key, _extends({}, subs, { linkedQuery: smart })); - }); - } else { - this.addSmartSubscription(key, _extends({}, subs, { - linkedQuery: smart - })); + } } } @@ -3118,12 +3126,14 @@ var DollarApollo = function () { }, { key: 'addSmartSubscription', value: function addSmartSubscription(key, options) { - options = reapply(options, this.vm); + if (!this.vm.$isServer) { + options = reapply(options, this.vm); - var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false); - smart.autostart(); + var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false); + smart.autostart(); - return smart; + return smart; + } } }, { key: 'defineReactiveSetter', @@ -3357,8 +3367,9 @@ var ApolloProvider$1 = function () { // Query return new Promise(function (resolve, reject) { - var options = lodash_omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS); + var options = lodash_omit(queryOptions, [].concat(toConsumableArray(VUE_APOLLO_QUERY_KEYWORDS), ['fetchPolicy'])); options.variables = variables; + options.fetchPolicy = 'network-only'; client.query(options).then(resolve, reject); }); } diff --git a/package.json b/package.json index 5ac7437..120ec87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-apollo", - "version": "2.1.0-rc.5", + "version": "2.1.0-rc.8", "description": "Vue apollo integration", "main": "dist/vue-apollo.umd.js", "module": "dist/vue-apollo.esm.js", diff --git a/src/apollo-provider.js b/src/apollo-provider.js index 65b0176..7f01887 100644 --- a/src/apollo-provider.js +++ b/src/apollo-provider.js @@ -130,8 +130,12 @@ export class ApolloProvider { // Query return new Promise((resolve, reject) => { - const options = omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS) + const options = omit(queryOptions, [ + ...VUE_APOLLO_QUERY_KEYWORDS, + 'fetchPolicy', + ]) options.variables = variables + options.fetchPolicy = 'network-only' client.query(options).then(resolve, reject) }) } diff --git a/src/consts.js b/src/consts.js index 0a097b4..dc96615 100644 --- a/src/consts.js +++ b/src/consts.js @@ -10,4 +10,6 @@ export const VUE_APOLLO_QUERY_KEYWORDS = [ 'throttle', 'debounce', 'subscribeToMore', + 'prefetch', + 'manual', ] diff --git a/src/dollar-apollo.js b/src/dollar-apollo.js index 3921fa1..67e265e 100644 --- a/src/dollar-apollo.js +++ b/src/dollar-apollo.js @@ -63,14 +63,16 @@ export class DollarApollo { } subscribe (options) { - const observable = this.getClient(options).subscribe(options) - const _subscribe = observable.subscribe.bind(observable) - observable.subscribe = (options) => { - let sub = _subscribe(options) - this._apolloSubscriptions.push(sub) - return sub + if (!this.vm.$isServer) { + const observable = this.getClient(options).subscribe(options) + const _subscribe = observable.subscribe.bind(observable) + observable.subscribe = (options) => { + let sub = _subscribe(options) + this._apolloSubscriptions.push(sub) + return sub + } + return observable } - return observable } addSmartQuery (key, options) { @@ -79,20 +81,22 @@ export class DollarApollo { const smart = this.queries[key] = new SmartQuery(this.vm, key, options, false) smart.autostart() - const subs = options.subscribeToMore - if (subs) { - if (Array.isArray(subs)) { - subs.forEach((sub, index) => { - this.addSmartSubscription(`${key}${index}`, { - ...sub, + if (!this.vm.$isServer) { + const subs = options.subscribeToMore + if (subs) { + if (Array.isArray(subs)) { + subs.forEach((sub, index) => { + this.addSmartSubscription(`${key}${index}`, { + ...sub, + linkedQuery: smart, + }) + }) + } else { + this.addSmartSubscription(key, { + ...subs, linkedQuery: smart, }) - }) - } else { - this.addSmartSubscription(key, { - ...subs, - linkedQuery: smart, - }) + } } } @@ -100,12 +104,14 @@ export class DollarApollo { } addSmartSubscription (key, options) { - options = reapply(options, this.vm) + if (!this.vm.$isServer) { + options = reapply(options, this.vm) - const smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false) - smart.autostart() + const smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false) + smart.autostart() - return smart + return smart + } } defineReactiveSetter (key, func) { diff --git a/src/smart-apollo.js b/src/smart-apollo.js index 3c3435c..16aade0 100644 --- a/src/smart-apollo.js +++ b/src/smart-apollo.js @@ -32,6 +32,10 @@ class SmartApollo { })) } + if (this.vm.$isServer) { + this.options.fetchPolicy = 'cache-first' + } + if (autostart) { this.autostart() }