Skip to content

Commit 1824dce

Browse files
author
Guillaume Chau
committed
docs(ApolloQuery): query operations example, closes #423
1 parent d385637 commit 1824dce

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

docs/guide/components/query.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,59 @@ An alternative way of using the component is by creating separate `.gql` files.
165165
</template>
166166
```
167167

168+
## Query operations
169+
170+
You can access the smart query object with the `query` slot prop. Here is an example component paginating data with `fetchMore`:
171+
172+
```vue
173+
<template>
174+
<ApolloQuery
175+
:query="/* query */"
176+
:variables="{
177+
limit: $options.pageSize
178+
}"
179+
v-slot="{ result: { loading, error, data }, query }"
180+
>
181+
<!-- Display data here -->
182+
<button v-if="hasMore" @click="loadMore(query)">Load more</button>
183+
</ApolloQuery>
184+
</template>
185+
186+
<script>
187+
export default {
188+
pageSize: 10,
189+
190+
data: {
191+
return {
192+
page: 1,
193+
hasMore: true
194+
}
195+
},
196+
197+
methods: {
198+
async loadMore (query) {
199+
await query.fetchMore({
200+
variables: {
201+
offset: this.page++ * this.$options.pageSize
202+
},
203+
updateQuery: (prev, { fetchMoreResult }) => {
204+
if (!fetchMoreResult || fetchMoreResult.product.length === 0) {
205+
this.hasMore = false
206+
return prev
207+
}
208+
return Object.assign({}, prev, {
209+
product: [...prev.product, ...fetchMoreResult.product]
210+
})
211+
}
212+
})
213+
}
214+
}
215+
}
216+
</script>
217+
```
218+
219+
See the [API reference](../../api/smart-query.md#methods) for all possible smart query methods.
220+
168221
## Using fragments
169222

170223
Fragments are useful to share parts of GraphQL documents in other documents to retrieve the same data consistently and also to not copy-paste code.

0 commit comments

Comments
 (0)