Skip to content

Commit 807215d

Browse files
committed
$apollo.queries/subscriptions, Pagination with fetchMore
1 parent 6c1c4ea commit 807215d

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ Integrates [apollo](http://www.apollostack.com/) in your vue components with dec
44

55
[Apollo "hello world" app](https://github.com/Akryum/frontpage-vue-app)
66

7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [Configuration](#configuration)
10+
- [Usage in components](#usage-in-components)
11+
- [Queries](#queries)
12+
- [Simple query](#simple-query)
13+
- [Query with parameters](#query-with-parameters)
14+
- [Reactive parameters](#reactive-parameters)
15+
- [Advanced options](#advanced-options)
16+
- [Reactive Query Example](#reactive-query-example)
17+
- [Mutations](#mutations)
18+
- [Subscriptions](#subscriptions)
19+
- [Pagination with `fetchMore`](#pagination-with-fetchMore)
20+
721
## Installation
822

923

@@ -69,6 +83,8 @@ apollo: {
6983
},
7084
```
7185

86+
You can then access the subscription `ObservableQuery` object with `this.$apollo.queries.<name>`.
87+
7288
You can initialize the property in your vue component's `data` hook:
7389

7490
```javascript
@@ -627,8 +643,95 @@ apollo: {
627643
},
628644
},
629645
```
646+
647+
You can then access the subscription `ObservableQuery` object with `this.$apollo.subscriptions.<name>`.
648+
630649
For the server implementation, you can take a look at [this simple example](https://github.com/Akryum/apollo-server-example).
631650

651+
### Pagination with `fetchMore`
652+
653+
Use the `fetchMore()` method on the query:
654+
655+
```javascript
656+
<template>
657+
<div id="app">
658+
<h2>Pagination</h2>
659+
<div class="tag-list" v-if="tagsPage">
660+
<div class="tag-list-item" v-for="tag in tagsPage.tags">
661+
{{ tag.id }} - {{ tag.label }} - {{ tag.type }}
662+
</div>
663+
<div class="actions">
664+
<button v-if="showMoreEnabled" @click="showMore">Show more</button>
665+
</div>
666+
</div>
667+
</div>
668+
</template>
669+
670+
<script>
671+
import gql from 'graphql-tag';
672+
673+
const pageSize = 10;
674+
675+
export default {
676+
name: 'app',
677+
data: () => ({
678+
page: 0,
679+
showMoreEnabled: true,
680+
}),
681+
apollo: {
682+
// Pages
683+
tagsPage: {
684+
// GraphQL Query
685+
query: gql`query tagsPage ($page: Int!, $pageSize: Int!) {
686+
tagsPage(page: $page, size: $pageSize) {
687+
tags {
688+
id
689+
label
690+
type
691+
}
692+
hasMore
693+
}
694+
}`,
695+
// Initial variables
696+
variables: {
697+
page: 0,
698+
pageSize,
699+
},
700+
},
701+
},
702+
methods: {
703+
showMore() {
704+
this.page ++;
705+
// Fetch more data and transform the original result
706+
this.$apollo.queries.tagsPage.fetchMore({
707+
// New variables
708+
variables: {
709+
page: this.page,
710+
pageSize,
711+
},
712+
// Transform the previous result with new data
713+
updateQuery: (previousResult, { fetchMoreResult }) => {
714+
const newTags = fetchMoreResult.data.tagsPage.tags;
715+
const hasMore = fetchMoreResult.data.tagsPage.hasMore;
716+
717+
this.showMoreEnabled = hasMore;
718+
719+
return {
720+
tagsPage: {
721+
// Merging the tag list
722+
tags: [...previousResult.tagsPage.tags, ...newTags],
723+
hasMore,
724+
},
725+
};
726+
},
727+
});
728+
},
729+
},
730+
};
731+
</script>
732+
```
733+
734+
[Here](https://github.com/Akryum/apollo-server-example/blob/master/schema.js#L21) is a simple example for the server.
632735

633736
---
634737

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": "1.1.0-beta.1",
3+
"version": "1.1.0-beta.2",
44
"description": "Vue apollo integration",
55
"main": "index.js",
66
"scripts": {

src/vue-plugin.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function addGraphQLSubscriptions(networkInterface, wsClient) {
2323
class DollarApollo {
2424
constructor(vm) {
2525
this.vm = vm;
26-
this.querySubscriptions = {};
26+
this.queries = {};
27+
this.subscriptions = {};
2728
}
2829

2930
get client() {
@@ -117,6 +118,7 @@ class DollarApollo {
117118

118119
// Create observer
119120
observer = $apollo.watchQuery(generateApolloOptions(variables));
121+
$apollo.queries[key] = observer;
120122

121123
// Create subscription
122124
sub = observer.subscribe({
@@ -208,14 +210,13 @@ class DollarApollo {
208210
}
209211

210212
function q(variables) {
211-
console.log(variables);
212-
213213
if (sub) {
214214
sub.unsubscribe();
215215
}
216216

217217
// Create observer
218218
observer = $apollo.subscribe(generateApolloOptions(variables));
219+
$apollo.subscriptions[key] = observer;
219220

220221
// Create subscription
221222
sub = observer.subscribe({

0 commit comments

Comments
 (0)