More special options, default options, provider options, better doc (wip)

This commit is contained in:
Guillaume Chau
2017-08-28 18:59:20 +02:00
parent 086b486bd7
commit d07900cf72
5 changed files with 216 additions and 25 deletions
+3
View File
@@ -9,6 +9,9 @@ export class ApolloProvider {
}
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 = []
}
+2
View File
@@ -10,6 +10,8 @@ export class DollarApollo {
this.queries = {}
this.subscriptions = {}
this.client = undefined
this.loadingKey = undefined
this.error = undefined
}
get provider () {
+25 -17
View File
@@ -8,8 +8,11 @@ const keywords = [
]
const prepare = function prepare () {
let apolloProvider
if (this.$options.apolloProvider) {
this._apolloProvider = this.$options.apolloProvider
apolloProvider = this._apolloProvider = this.$options.apolloProvider
} else {
apolloProvider = this.$root._apolloProvider
}
if (this._apolloPrepared) return
@@ -17,9 +20,20 @@ const prepare = function prepare () {
// Prepare properties
let 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 (let key in apollo) {
@@ -35,6 +49,16 @@ const launch = function launch () {
if (this._apolloLaunched) return
this._apolloLaunched = true
let 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 (let key in this._apolloQueries) {
@@ -42,7 +66,6 @@ const launch = function launch () {
}
}
let apollo = this.$options.apollo
if (apollo) {
if (apollo.subscribe) {
Globals.Vue.util.warn('vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.')
@@ -53,11 +76,6 @@ const 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)
}
}
@@ -95,16 +113,6 @@ export function install (Vue, options) {
return Object.assign(map, merge(toData, fromData))
}
// Lazy creation
Object.defineProperty(Vue.prototype, '$apollo', {
get () {
if (!this._apollo) {
this._apollo = new DollarApollo(this)
}
return this._apollo
},
})
Vue.mixin({
// Vue 1.x
+21 -8
View File
@@ -115,6 +115,12 @@ class SmartApollo {
throw new Error('Not implemented')
}
errorHandler (...args) {
this.options.error && this.options.error.call(this.vm, ...args)
this.vm.$apollo.error && this.vm.$apollo.error.call(this.vm, ...args)
this.vm.$apollo.provider.errorHandler && this.vm.$apollo.provider.errorHandler.call(this.vm, ...args)
}
catchError (error) {
if (error.graphQLErrors && error.graphQLErrors.length !== 0) {
console.error(`GraphQL execution errors for ${this.type} '${this.key}'`)
@@ -132,9 +138,7 @@ class SmartApollo {
}
}
if (typeof this.options.error === 'function') {
this.options.error.call(this.vm, error)
}
this.errorHandler(error)
}
destroy () {
@@ -233,14 +237,23 @@ export class SmartQuery extends SmartApollo {
this.loadingDone()
}
get loadingKey () {
return this.options.loadingKey || this.vm.$apollo.loadingKey
}
watchLoading (...args) {
this.options.watchLoading && this.options.watchLoading.call(this.vm, ...args)
this.vm.$apollo.watchLoading && this.vm.$apollo.watchLoading.call(this.vm, ...args)
this.vm.$apollo.provider.watchLoading && this.vm.$apollo.provider.watchLoading.call(this.vm, ...args)
}
applyLoadingModifier (value) {
if (this.options.loadingKey) {
this.vm[this.options.loadingKey] += value
const 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)
}
loadingDone () {