Skip to content

Commit 635f0f5

Browse files
committed
Add option mergin, fix #34
1 parent c9a4107 commit 635f0f5

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "vue-apollo",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Vue apollo integration",
5-
"main": "index.js",
5+
"main": "lib/vue-plugin.js",
66
"scripts": {
77
"compile": "babel --presets es2015,stage-0 -d lib/ src/",
88
"prepublish": "npm run compile",

src/vue-plugin.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ import { SmartQuery, SmartSubscription } from './smart-apollo'
55
let Vue
66
let apolloClient = null
77

8+
const keywords = [
9+
'subscribe',
10+
'$subscribe',
11+
'$skipAll',
12+
'$skipAllQueries',
13+
'$skipAllSubscriptions',
14+
]
15+
816
let defineReactive = function () {}
917

1018
// quick way to add the subscribe and unsubscribe functions to the network interface
11-
function addGraphQLSubscriptions (networkInterface, wsClient) {
19+
export function addGraphQLSubscriptions (networkInterface, wsClient) {
1220
return Object.assign(networkInterface, {
1321
subscribe (request, handler) {
1422
return wsClient.subscribe({
@@ -76,7 +84,6 @@ class DollarApollo {
7684

7785
defineReactiveSetter (key, func) {
7886
this._watchers.push(this.vm.$watch(func, value => {
79-
console.log(value)
8087
this[key] = value
8188
}, {
8289
immediate: true,
@@ -119,6 +126,9 @@ class DollarApollo {
119126
}
120127

121128
const prepare = function prepare () {
129+
if (this._apolloPrepared) return
130+
this._apolloPrepared = true
131+
122132
// Lazy creation
123133
Object.defineProperty(this, '$apollo', {
124134
get: () => {
@@ -132,13 +142,7 @@ const prepare = function prepare () {
132142
// Prepare properties
133143
let apollo = this.$options.apollo
134144
if (apollo) {
135-
this._apolloQueries = omit(apollo, [
136-
'subscribe',
137-
'$subscribe',
138-
'$skipAll',
139-
'$skipAllQueries',
140-
'$skipAllSubscriptions',
141-
])
145+
this._apolloQueries = omit(apollo, keywords)
142146

143147
// watchQuery
144148
for (let key in this._apolloQueries) {
@@ -149,6 +153,9 @@ const prepare = function prepare () {
149153
}
150154

151155
const launch = function launch () {
156+
if (this._apolloLaunched) return
157+
this._apolloLaunched = true
158+
152159
if (this._apolloQueries) {
153160
// watchQuery
154161
for (let key in this._apolloQueries) {
@@ -187,30 +194,46 @@ function defineReactiveSetter ($apollo, key, value) {
187194
}
188195
}
189196

190-
module.exports = {
191-
addGraphQLSubscriptions,
197+
export default function install (pVue, options) {
198+
if (install.installed) return
199+
install.installed = true
192200

193-
install (pVue, options) {
194-
Vue = pVue
195-
defineReactive = Vue.util.defineReactive
196-
apolloClient = options.apolloClient
201+
Vue = pVue
202+
defineReactive = Vue.util.defineReactive
203+
apolloClient = options.apolloClient
197204

198-
Vue.mixin({
205+
const merge = Vue.config.optionMergeStrategies.methods
206+
Vue.config.optionMergeStrategies.apollo = function (toVal, fromVal, vm) {
207+
if (!toVal) return fromVal
208+
if (!fromVal) return toVal
199209

200-
// Vue 1.x
201-
init: prepare,
202-
// Vue 2.x
203-
beforeCreate: prepare,
210+
const toData = Object.assign({}, omit(toVal, keywords), toVal.data)
211+
const fromData = Object.assign({}, omit(fromVal, keywords), fromVal.data)
204212

205-
created: launch,
213+
const map = {}
214+
for (let i = 0; i < keywords.length; i++) {
215+
const key = keywords[i]
216+
map[key] = merge(toVal[key], fromVal[key])
217+
}
206218

207-
destroyed: function () {
208-
if (this._apollo) {
209-
this._apollo.destroy()
210-
this._apollo = null
211-
}
212-
},
219+
return Object.assign(map, merge(toData, fromData))
220+
}
213221

214-
})
215-
},
222+
Vue.mixin({
223+
224+
// Vue 1.x
225+
init: prepare,
226+
// Vue 2.x
227+
beforeCreate: prepare,
228+
229+
created: launch,
230+
231+
destroyed: function () {
232+
if (this._apollo) {
233+
this._apollo.destroy()
234+
this._apollo = null
235+
}
236+
},
237+
238+
})
216239
}

0 commit comments

Comments
 (0)