Skip to content

Commit c53127d

Browse files
author
Guillaume Chau
committed
feat: deep options fix #219
1 parent 0a3cc3a commit c53127d

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ These are the available advanced options you can use:
442442
- `loadingKey` will update the component data property you pass as the value. You should initialize this property to `0` in the component `data()` hook. When the query is loading, this property will be incremented by 1; when it is no longer loading, it will be decremented by 1. That way, the property can represent a counter of currently loading queries.
443443
- `watchLoading(isLoading, countModifier)` is a hook called when the loading state of the query changes. The `countModifier` parameter is either equal to `1` when the query is loading, or `-1` when the query is no longer loading.
444444
- `manual` is a boolean to disable the automatic property update. If you use it, you then need to specify a `result` callback (see example below).
445+
- `deep` is a boolean to use `deep: true` on Vue watchers.
445446

446447

447448
```javascript
@@ -460,6 +461,8 @@ apollo: {
460461
message: this.pingInput,
461462
}
462463
},
464+
// Variables: deep object watch
465+
deep: false,
463466
// We use a custom update callback because
464467
// the field names don't match
465468
// By default, the 'pingMessage' attribute
@@ -1106,9 +1109,9 @@ The special options begin with `$` in the `apollo` object.
11061109
- `$skip` to disable all queries and subscriptions (see below)
11071110
- `$skipAllQueries` to disable all queries (see below)
11081111
- `$skipAllSubscriptions` to disable all subscriptions (see below)
1109-
- `$client` to use a client by default (see below)
1110-
- `$loadingKey` for a default loading key (see `loadingKey` advanced options for smart queries)
1112+
- `$deep` to watch with `deep: true` on the properties above when a function is provided
11111113
- `$error` to catch errors in a default handler (see `error` advanced options for smart queries)
1114+
- `$query` to apply default options to all the queries in the component
11121115

11131116
Example:
11141117

@@ -1121,7 +1124,9 @@ export default {
11211124
}
11221125
},
11231126
apollo: {
1124-
$loadingKey: 'loading',
1127+
$query: {
1128+
loadingKey: 'loading',
1129+
},
11251130
query1: { ... },
11261131
query2: { ... },
11271132
},
@@ -1135,10 +1140,9 @@ You can define in the apollo provider a default set of options to apply to the `
11351140
const apolloProvider = new VueApollo({
11361141
defaultClient: apolloClient,
11371142
defaultOptions: {
1138-
// apollo options applied to all components that are using apollo
1139-
$loadingKey: 'loading',
11401143
// apollo options applied to all queries in components
11411144
$query: {
1145+
loadingKey: 'loading',
11421146
fetchPolicy: 'cache-and-network',
11431147
},
11441148
},

src/dollar-apollo.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ export class DollarApollo {
123123
}
124124
}
125125

126-
defineReactiveSetter (key, func) {
126+
defineReactiveSetter (key, func, deep) {
127127
this._watchers.push(this.vm.$watch(func, value => {
128128
this[key] = value
129129
}, {
130130
immediate: true,
131+
deep,
131132
}))
132133
}
133134

src/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ const launch = function launch () {
3232
}
3333
}
3434

35-
defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll)
36-
defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries)
37-
defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions)
38-
defineReactiveSetter(this.$apollo, 'client', apollo.$client)
39-
defineReactiveSetter(this.$apollo, 'loadingKey', apollo.$loadingKey)
40-
defineReactiveSetter(this.$apollo, 'error', apollo.$error)
41-
defineReactiveSetter(this.$apollo, 'watchLoading', apollo.$watchLoading)
35+
defineReactiveSetter(this.$apollo, 'skipAll', apollo.$skipAll, apollo.$deep)
36+
defineReactiveSetter(this.$apollo, 'skipAllQueries', apollo.$skipAllQueries, apollo.$deep)
37+
defineReactiveSetter(this.$apollo, 'skipAllSubscriptions', apollo.$skipAllSubscriptions, apollo.$deep)
38+
defineReactiveSetter(this.$apollo, 'client', apollo.$client, apollo.$deep)
39+
defineReactiveSetter(this.$apollo, 'loadingKey', apollo.$loadingKey, apollo.$deep)
40+
defineReactiveSetter(this.$apollo, 'error', apollo.$error, apollo.$deep)
41+
defineReactiveSetter(this.$apollo, 'watchLoading', apollo.$watchLoading, apollo.$deep)
4242

4343
// Apollo Data
4444
Object.defineProperty(this, '$apolloData', {
@@ -51,9 +51,11 @@ const launch = function launch () {
5151
for (let key in apollo) {
5252
if (key.charAt(0) !== '$') {
5353
let options = apollo[key]
54+
// Default options from component
5455
if (apollo.$query) {
5556
options = Object.assign({}, apollo.$query, options)
5657
}
58+
// Property proxy
5759
if (!hasProperty(this, key) && !hasProperty(this.$props, key) && !hasProperty(this.$data, key)) {
5860
Object.defineProperty(this, key, {
5961
get: () => this.$data.$apolloData.data[key],

src/smart-apollo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default class SmartApollo {
1919
this._watchers.push(this.vm.$watch(queryCb, query => {
2020
this.options.query = query
2121
this.refresh()
22+
}, {
23+
deep: this.options.deep,
2224
}))
2325
}
2426
// Query callback
@@ -28,6 +30,8 @@ export default class SmartApollo {
2830
this._watchers.push(this.vm.$watch(queryCb, document => {
2931
this.options.document = document
3032
this.refresh()
33+
}, {
34+
deep: this.options.deep,
3135
}))
3236
}
3337

@@ -38,6 +42,8 @@ export default class SmartApollo {
3842
this._watchers.push(this.vm.$watch(cb, context => {
3943
this.options.context = context
4044
this.refresh()
45+
}, {
46+
deep: this.options.deep,
4147
}))
4248
}
4349

@@ -54,6 +60,7 @@ export default class SmartApollo {
5460
if (typeof this.options.skip === 'function') {
5561
this._watchers.push(this.vm.$watch(this.options.skip.bind(this.vm), this.skipChanged.bind(this), {
5662
immediate: true,
63+
deep: this.options.deep,
5764
}))
5865
} else if (!this.options.skip) {
5966
this.start()
@@ -96,6 +103,7 @@ export default class SmartApollo {
96103
cb = this.options.debounce ? debounce(cb, this.options.debounce) : cb
97104
this.unwatchVariables = this.vm.$watch(() => this.options.variables.call(this.vm), cb, {
98105
immediate: true,
106+
deep: this.options.deep,
99107
})
100108
} else {
101109
this.executeApollo(this.options.variables)

0 commit comments

Comments
 (0)