diff --git a/.gitignore b/.gitignore index c2658d7d..88edb628 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +lib/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 00000000..8eba6c8d --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +src/ diff --git a/lib/vue-plugin.js b/lib/vue-plugin.js index 4cb49b0d..f66e9b16 100644 --- a/lib/vue-plugin.js +++ b/lib/vue-plugin.js @@ -1,160 +1,198 @@ -const omit = require('lodash.omit'); +'use strict'; -let apolloClient = null; +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); -class DollarApollo { - constructor(vm) { - this.vm = vm; - this.querySubscriptions = {}; - } +var _lodash = require('lodash.omit'); - get client() { - return apolloClient; - } +var _lodash2 = _interopRequireDefault(_lodash); - get query() { - return this.client.query; - } +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - watchQuery(options) { - const vm = this.vm; - const observable = this.client.watchQuery(options); - const _subscribe = observable.subscribe.bind(observable); - observable.subscribe = (function(options) { - let sub = _subscribe(options); - vm._apolloSubscriptions.push(sub); - return sub; - }).bind(observable); - return observable; - } +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - get mutate() { - return this.client.mutate; - } +var apolloClient = null; - option(key, options) { - const vm = this.vm; - const $apollo = this; +var DollarApollo = function () { + function DollarApollo(vm) { + _classCallCheck(this, DollarApollo); - let query, observer, sub; + this.vm = vm; + this.querySubscriptions = {}; + } - let firstLoadingDone = false; + _createClass(DollarApollo, [{ + key: 'watchQuery', + value: function watchQuery(options) { + var vm = this.vm; + var observable = this.client.watchQuery(options); + var _subscribe = observable.subscribe.bind(observable); + observable.subscribe = function (options) { + var sub = _subscribe(options); + vm._apolloSubscriptions.push(sub); + return sub; + }.bind(observable); + return observable; + } + }, { + key: 'option', + value: function option(key, options) { + var vm = this.vm; + var $apollo = this; - let loadingKey = options.loadingKey; - let loadingChangeCb = options.watchLoading; + var query = void 0, + observer = void 0, + sub = void 0; - if (typeof loadingChangeCb === 'function') { - loadingChangeCb = loadingChangeCb.bind(vm); - } + var firstLoadingDone = false; - // Simple query - if (!options.query) { - query = options; - } + var loadingKey = options.loadingKey; + var loadingChangeCb = options.watchLoading; - function generateApolloOptions(variables) { - const apolloOptions = omit(options, [ - 'variables', - 'watch', - 'update', - 'result', - 'error', - 'loadingKey', - 'watchLoading', - ]); - apolloOptions.variables = variables; - return apolloOptions; - } + if (typeof loadingChangeCb === 'function') { + loadingChangeCb = loadingChangeCb.bind(vm); + } - function q(variables) { - applyLoadingModifier(1); + // Simple query + if (!options.query) { + query = options; + } - if (options.forceFetch && observer) { - // Refresh query - observer.refetch(variables, { - forceFetch: !!options.forceFetch - }); - } else { - if (sub) { - sub.unsubscribe(); - } + function generateApolloOptions(variables) { + var apolloOptions = (0, _lodash2.default)(options, ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading']); + apolloOptions.variables = variables; + return apolloOptions; + } - // Create observer - observer = $apollo.watchQuery(generateApolloOptions(variables)); + function q(variables) { + applyLoadingModifier(1); + + if (options.forceFetch && observer) { + // Refresh query + observer.refetch(variables, { + forceFetch: !!options.forceFetch + }); + } else { + if (sub) { + sub.unsubscribe(); + } + + // Create observer + observer = $apollo.watchQuery(generateApolloOptions(variables)); + + // Create subscription + sub = observer.subscribe({ + next: nextResult, + error: catchError + }); + } + } - // Create subscription - sub = observer.subscribe({ - next: nextResult, - error: catchError + if (typeof options.variables === 'function') { + vm.$watch(options.variables.bind(vm), q, { + immediate: true }); + } else { + q(options.variables); } - } - if (typeof options.variables === 'function') { - vm.$watch(options.variables.bind(vm), q, { - immediate: true - }); - } else { - q(options.variables); - } + function nextResult(_ref) { + var data = _ref.data; - function nextResult({ data }) { - applyData(data); - } + applyData(data); + } - function applyData(data) { - loadingDone(); + function applyData(data) { + loadingDone(); - if (typeof options.update === 'function') { - vm[key] = options.update.call(vm, data); - } else if (data[key] === undefined) { - console.error(`Missing ${key} attribute on result`, data); - } else { - vm[key] = data[key]; - } + if (typeof options.update === 'function') { + vm[key] = options.update.call(vm, data); + } else if (data[key] === undefined) { + console.error('Missing ' + key + ' attribute on result', data); + } else { + vm[key] = data[key]; + } - if (typeof options.result === 'function') { - options.result.call(vm, data); + if (typeof options.result === 'function') { + options.result.call(vm, data); + } } - } - function applyLoadingModifier(value) { - if (loadingKey) { - vm[loadingKey] += value; - } + function applyLoadingModifier(value) { + if (loadingKey) { + vm[loadingKey] += value; + } - if (loadingChangeCb) { - loadingChangeCb(value === 1, value); + if (loadingChangeCb) { + loadingChangeCb(value === 1, value); + } } - } - function loadingDone() { - if (!firstLoadingDone) { - applyLoadingModifier(-1); - firstLoadingDone = true; + function loadingDone() { + if (!firstLoadingDone) { + applyLoadingModifier(-1); + firstLoadingDone = true; + } } - } - - function catchError(error) { - loadingDone(); - if (error.graphQLErrors && error.graphQLErrors.length !== 0) { - console.error(`GraphQL execution errors for query ${query}`); - for (let e of error.graphQLErrors) { - console.error(e); + function catchError(error) { + loadingDone(); + + if (error.graphQLErrors && error.graphQLErrors.length !== 0) { + console.error('GraphQL execution errors for query ' + query); + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = error.graphQLErrors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var e = _step.value; + + console.error(e); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + } else if (error.networkError) { + console.error('Error sending the query ' + query, error.networkError); + } else { + console.error(error); } - } else if (error.networkError) { - console.error(`Error sending the query ${query}`, error.networkError); - } else { - console.error(error); - } - if (typeof options.error === 'function') { - options.error(error); + if (typeof options.error === 'function') { + options.error(error); + } } } - } -} + }, { + key: 'client', + get: function get() { + return apolloClient; + } + }, { + key: 'query', + get: function get() { + return this.client.query; + } + }, { + key: 'mutate', + get: function get() { + return this.client.mutate; + } + }]); + + return DollarApollo; +}(); function prepare() { this._apolloSubscriptions = []; @@ -163,15 +201,13 @@ function prepare() { } function launch() { - let apollo = this.$options.apollo; + var apollo = this.$options.apollo; if (apollo) { - const queries = omit(apollo, [ - 'subscribe', - ]); + var queries = (0, _lodash2.default)(apollo, ['subscribe']); // watchQuery - for (let key in queries) { + for (var key in queries) { this.$apollo.option(key, queries[key]); } @@ -183,7 +219,7 @@ function launch() { } module.exports = { - install(Vue, options) { + install: function install(Vue, options) { apolloClient = options.apolloClient; @@ -196,8 +232,8 @@ module.exports = { created: launch, - destroyed: function() { - this._apolloSubscriptions.forEach((sub) => { + destroyed: function destroyed() { + this._apolloSubscriptions.forEach(function (sub) { sub.unsubscribe(); }); this._apolloSubscriptions = null; @@ -207,6 +243,5 @@ module.exports = { } }); - } -}; +}; \ No newline at end of file diff --git a/package.json b/package.json index 64b69463..d7ceaac8 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,11 @@ { "name": "vue-apollo", - "version": "1.0.0-beta10", + "version": "1.0.0-beta11", "description": "Vue apollo integration", "main": "index.js", "scripts": { + "compile": "babel --presets es2015 -d lib/ src/", + "prepublish": "npm run compile", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -26,5 +28,9 @@ }, "dependencies": { "lodash.omit": "^4.5.0" + }, + "devDependencies": { + "babel-cli": "^6.14.0", + "babel-preset-es2015": "^6.14.0" } } diff --git a/src/vue-plugin.js b/src/vue-plugin.js new file mode 100644 index 00000000..842caaf5 --- /dev/null +++ b/src/vue-plugin.js @@ -0,0 +1,212 @@ +import omit from 'lodash.omit'; + +let apolloClient = null; + +class DollarApollo { + constructor(vm) { + this.vm = vm; + this.querySubscriptions = {}; + } + + get client() { + return apolloClient; + } + + get query() { + return this.client.query; + } + + watchQuery(options) { + const vm = this.vm; + const observable = this.client.watchQuery(options); + const _subscribe = observable.subscribe.bind(observable); + observable.subscribe = (function(options) { + let sub = _subscribe(options); + vm._apolloSubscriptions.push(sub); + return sub; + }).bind(observable); + return observable; + } + + get mutate() { + return this.client.mutate; + } + + option(key, options) { + const vm = this.vm; + const $apollo = this; + + let query, observer, sub; + + let firstLoadingDone = false; + + let loadingKey = options.loadingKey; + let loadingChangeCb = options.watchLoading; + + if (typeof loadingChangeCb === 'function') { + loadingChangeCb = loadingChangeCb.bind(vm); + } + + // Simple query + if (!options.query) { + query = options; + } + + function generateApolloOptions(variables) { + const apolloOptions = omit(options, [ + 'variables', + 'watch', + 'update', + 'result', + 'error', + 'loadingKey', + 'watchLoading', + ]); + apolloOptions.variables = variables; + return apolloOptions; + } + + function q(variables) { + applyLoadingModifier(1); + + if (options.forceFetch && observer) { + // Refresh query + observer.refetch(variables, { + forceFetch: !!options.forceFetch + }); + } else { + if (sub) { + sub.unsubscribe(); + } + + // Create observer + observer = $apollo.watchQuery(generateApolloOptions(variables)); + + // Create subscription + sub = observer.subscribe({ + next: nextResult, + error: catchError + }); + } + } + + if (typeof options.variables === 'function') { + vm.$watch(options.variables.bind(vm), q, { + immediate: true + }); + } else { + q(options.variables); + } + + function nextResult({ data }) { + applyData(data); + } + + function applyData(data) { + loadingDone(); + + if (typeof options.update === 'function') { + vm[key] = options.update.call(vm, data); + } else if (data[key] === undefined) { + console.error(`Missing ${key} attribute on result`, data); + } else { + vm[key] = data[key]; + } + + if (typeof options.result === 'function') { + options.result.call(vm, data); + } + } + + function applyLoadingModifier(value) { + if (loadingKey) { + vm[loadingKey] += value; + } + + if (loadingChangeCb) { + loadingChangeCb(value === 1, value); + } + } + + function loadingDone() { + if (!firstLoadingDone) { + applyLoadingModifier(-1); + firstLoadingDone = true; + } + } + + function catchError(error) { + loadingDone(); + + if (error.graphQLErrors && error.graphQLErrors.length !== 0) { + console.error(`GraphQL execution errors for query ${query}`); + for (let e of error.graphQLErrors) { + console.error(e); + } + } else if (error.networkError) { + console.error(`Error sending the query ${query}`, error.networkError); + } else { + console.error(error); + } + + if (typeof options.error === 'function') { + options.error(error); + } + } + } +} + +function prepare() { + this._apolloSubscriptions = []; + + this.$apollo = new DollarApollo(this); +} + +function launch() { + let apollo = this.$options.apollo; + + if (apollo) { + const queries = omit(apollo, [ + 'subscribe', + ]); + + // watchQuery + for (let key in queries) { + this.$apollo.option(key, queries[key]); + } + + // subscribe + if (apollo.subscribe) { + // TODO + } + } +} + +module.exports = { + install(Vue, options) { + + apolloClient = options.apolloClient; + + Vue.mixin({ + + // Vue 1.x + init: prepare, + // Vue 2.x + beforeCreate: prepare, + + created: launch, + + destroyed: function() { + this._apolloSubscriptions.forEach((sub) => { + sub.unsubscribe(); + }); + this._apolloSubscriptions = null; + if (this.$apollo) { + this.$apollo = null; + } + } + + }); + + } +};