Skip to content

Commit 1e91fee

Browse files
committed
Readme: subscriptions
1 parent 52cd068 commit 1e91fee

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ const apolloClient = new ApolloClient({
3030
Vue.use(VueApollo, {
3131
apolloClient,
3232
});
33+
34+
// The Vue app is now Apollo-enabled!
35+
36+
import App from './App.vue'
37+
38+
new Vue({
39+
el: '#app',
40+
render: h => h(App)
41+
});
3342
```
3443

3544
### Usage in components
@@ -514,6 +523,120 @@ export const resolvers = {
514523
};
515524
```
516525

526+
### Subscriptions
527+
528+
To make enable the websocket-based subscription, a bit of additional setup is required:
529+
530+
```javascript
531+
import Vue from 'vue'
532+
import ApolloClient, { createNetworkInterface } from 'apollo-client';
533+
// New Imports
534+
import { Client } from 'subscriptions-transport-ws';
535+
import VueApollo, { addGraphQLSubscriptions } from 'vue-apollo';
536+
537+
// Create the network interface
538+
const networkInterface = createNetworkInterface({
539+
uri: 'http://localhost:3000/graphql',
540+
transportBatching: true,
541+
});
542+
543+
// Create the subscription websocket client
544+
const wsClient = new Client('ws://localhost:3030');
545+
546+
// Extend the network interface with the subscription client
547+
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
548+
networkInterface,
549+
wsClient,
550+
);
551+
552+
// Create the apollo client with the new network interface
553+
const apolloClient = new ApolloClient({
554+
networkInterface: networkInterfaceWithSubscriptions,
555+
});
556+
557+
// Install the plugin like before
558+
Vue.use(VueApollo, {
559+
apolloClient,
560+
});
561+
562+
// Your app is now subscription-ready!
563+
564+
import App from './App.vue'
565+
566+
new Vue({
567+
el: '#app',
568+
render: h => h(App)
569+
});
570+
571+
```
572+
573+
Use the `$apollo.subscribe()` method to subscribe to a GraphQL subscription that will get killed automatically when the component is destroyed:
574+
575+
```javascript
576+
mounted() {
577+
const subQuery = gql`subscription tags($type: String!) {
578+
tagAdded(type: $type) {
579+
id
580+
label
581+
type
582+
}
583+
}`;
584+
585+
const observer = this.$apollo.subscribe({
586+
query: subQuery,
587+
variables: {
588+
type: 'City',
589+
},
590+
});
591+
592+
observer.subscribe({
593+
next(data) {
594+
console.log(data);
595+
},
596+
error(error) {
597+
console.error(error);
598+
},
599+
});
600+
},
601+
```
602+
603+
You can declare subscriptions in the `apollo` option with the `subscribe` keyword:
604+
605+
```javascript
606+
apollo: {
607+
// Subscriptions
608+
subscribe: {
609+
// When a tag is added
610+
tags: {
611+
query: gql`subscription tags($type: String!) {
612+
tagAdded(type: $type) {
613+
id
614+
label
615+
type
616+
}
617+
}`,
618+
// Reactive variables
619+
variables() {
620+
// This works just like regular queries
621+
// and will re-subscribe with the right variables
622+
// each time the values change
623+
return {
624+
type: this.type,
625+
};
626+
},
627+
// Result hook
628+
result(data) {
629+
console.log(data);
630+
// Let's update the local data
631+
this.tags.push(data.tagAdded);
632+
},
633+
},
634+
},
635+
},
636+
```
637+
For the server implementation, you can take a look at [this simple example](https://github.com/Akryum/apollo-server-example)
638+
639+
517640
---
518641

519642
LICENCE ISC - Created by Guillaume CHAU (@Akryum)

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

0 commit comments

Comments
 (0)