Skip to content

Commit b1f7090

Browse files
committed
BEtter subscribe to more inside query def + Readme changes
1 parent f9517bb commit b1f7090

File tree

6 files changed

+87
-34
lines changed

6 files changed

+87
-34
lines changed

README.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -568,17 +568,15 @@ methods: {
568568
label: newTag,
569569
},
570570
// Update the cache with the result
571-
// 'tagList' is the name of the query declared before
572-
// that will be updated with the optimistic response
573-
// and the result of the mutation
574-
updateQueries: {
575-
tagList: (previousResult, { mutationResult }) => {
576-
// We incorporate any received result (either optimistic or real)
577-
// into the 'tagList' query we set up earlier
578-
return {
579-
tags: [...previousResult.tags, mutationResult.data.addTag],
580-
}
581-
},
571+
// The query will be updated with the optimistic response
572+
// and then with the real result of the mutation
573+
update: (store, { data: { newTag } }) => {
574+
// Read the data from our cache for this query.
575+
const data = store.readQuery({ query: TAGS_QUERY })
576+
// Add our tag from the mutation to the end
577+
data.tags.push(newTag)
578+
// Write our data back to the cache.
579+
store.writeQuery({ query: TAGS_QUERY, data })
582580
},
583581
// Optimistic UI
584582
// Will be treated as a 'fake' result as soon as the request is made
@@ -715,7 +713,38 @@ new Vue({
715713

716714
### subscribeToMore
717715

718-
If you need to update a query result from a subscription, the best way is using the `subscribeToMore` query method. You can access the queries you defined in the `apollo` option with `this.$apollo.queries.<name>`, so it would look like this:
716+
If you need to update a query result from a subscription, the best way is using the `subscribeToMore` query method. Just add a `subscribeToMore` to your query:
717+
718+
```javascript
719+
apollo: {
720+
tags: {
721+
query: TAGS_QUERY,
722+
subscribeToMore: {
723+
document: gql`subscription name($param: String!) {
724+
itemAdded(param: $param) {
725+
id
726+
label
727+
}
728+
}`,
729+
// Variables passed to the subscription
730+
variables () {
731+
// Reactive if a function (like previously)
732+
return {
733+
param: this.param,
734+
}
735+
},
736+
// Mutate the previous result
737+
updateQuery: (previousResult, { subscriptionData }) => {
738+
// Here, return the new result from the previous with the new data
739+
},
740+
}
741+
}
742+
}
743+
```
744+
745+
#### Alternate usage
746+
747+
You can access the queries you defined in the `apollo` option with `this.$apollo.queries.<name>`, so it would look like this:
719748

720749
```javascript
721750
this.$apollo.queries.tags.subscribeToMore({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-apollo",
3-
"version": "2.1.0-beta.8",
3+
"version": "2.1.0-beta.9",
44
"description": "Vue apollo integration",
55
"main": "lib/index.js",
66
"scripts": {

src/consts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export const VUE_APOLLO_QUERY_KEYWORDS = [
99
'skip',
1010
'throttle',
1111
'debounce',
12+
'subscribeToMore',
1213
]

src/dollar-apollo.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SmartQuery, SmartSubscription } from './smart-apollo'
2+
import { reapply } from './utils'
23

34
export class DollarApollo {
45
constructor (vm) {
@@ -71,14 +72,27 @@ export class DollarApollo {
7172
}
7273

7374
addSmartQuery (key, options) {
75+
options = reapply(options, this.vm)
76+
7477
const smart = this.queries[key] = new SmartQuery(this.vm, key, options, false)
7578
smart.autostart()
79+
80+
if (options.subscribeToMore) {
81+
this.addSmartSubscription(key, {
82+
...options.subscribeToMore,
83+
linkedQuery: smart,
84+
})
85+
}
86+
7687
return smart
7788
}
7889

7990
addSmartSubscription (key, options) {
91+
options = reapply(options, this.vm)
92+
8093
const smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false)
8194
smart.autostart()
95+
8296
return smart
8397
}
8498

src/smart-apollo.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ class SmartApollo {
2222
this.refresh()
2323
}))
2424
}
25+
// Query callback
26+
if (typeof this.options.document === 'function') {
27+
const queryCb = this.options.document.bind(this.vm)
28+
this.options.document = queryCb()
29+
this._watchers.push(this.vm.$watch(queryCb, document => {
30+
this.options.document = document
31+
this.refresh()
32+
}))
33+
}
2534

2635
if (autostart) {
2736
this.autostart()
@@ -142,11 +151,6 @@ export class SmartQuery extends SmartApollo {
142151
loading = false
143152

144153
constructor (vm, key, options, autostart = true) {
145-
// Options object callback
146-
while (typeof options === 'function') {
147-
options = options.call(vm)
148-
}
149-
150154
// Simple query
151155
if (!options.query) {
152156
const query = options
@@ -309,30 +313,28 @@ export class SmartSubscription extends SmartApollo {
309313
'error',
310314
'throttle',
311315
'debounce',
316+
'linkedQuery',
312317
]
313318

314-
constructor (vm, key, options, autostart = true) {
315-
// Options object callback
316-
while (typeof options === 'function') {
317-
options = options.call(vm)
318-
}
319-
320-
super(vm, key, options, autostart)
321-
}
322-
323319
executeApollo (variables) {
324320
if (this.sub) {
325321
this.sub.unsubscribe()
326322
}
327323

328-
// Create observer
329-
this.observer = this.vm.$apollo.subscribe(this.generateApolloOptions(variables))
324+
const apolloOptions = this.generateApolloOptions(variables)
330325

331-
// Create subscription
332-
this.sub = this.observer.subscribe({
333-
next: this.nextResult.bind(this),
334-
error: this.catchError.bind(this),
335-
})
326+
if (this.options.linkedQuery) {
327+
this.sub = this.options.linkedQuery.subscribeToMore(apolloOptions)
328+
} else {
329+
// Create observer
330+
this.observer = this.vm.$apollo.subscribe(apolloOptions)
331+
332+
// Create subscription
333+
this.sub = this.observer.subscribe({
334+
next: this.nextResult.bind(this),
335+
error: this.catchError.bind(this),
336+
})
337+
}
336338

337339
super.executeApollo(variables)
338340
}

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ export const debounce = factory(loDebounce)
2020
export function getMergedDefinition (def) {
2121
return Globals.Vue.util.mergeOptions({}, def)
2222
}
23+
24+
export function reapply (options, context) {
25+
while (typeof options === 'function') {
26+
options = options.call(context)
27+
}
28+
return options
29+
}

0 commit comments

Comments
 (0)