Skip to content

Commit 6ebaa13

Browse files
author
Guillaume Chau
committed
Apollo 2 docs update
1 parent 2745e3d commit 6ebaa13

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

README.md

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Apollo and GraphQL for Vue.js
22

33
[![npm](https://img.shields.io/npm/v/vue-apollo.svg) ![npm](https://img.shields.io/npm/dm/vue-apollo.svg)](https://www.npmjs.com/package/vue-apollo)
4-
[![vue1](https://img.shields.io/badge/apollo-1.x-blue.svg)](http://apollodata.com/)
4+
[![vue1](https://img.shields.io/badge/apollo-2.x-blue.svg)](http://apollodata.com/)
55
[![vue1](https://img.shields.io/badge/vue-1.x-brightgreen.svg) ![vue2](https://img.shields.io/badge/vue-2.x-brightgreen.svg)](https://vuejs.org/)
66

77
![schema](https://cdn-images-1.medium.com/max/800/1*H9AANoofLqjS10Xd5TwRYw.png)
88

9-
Integrates [apollo](http://www.apollostack.com/) in your [Vue](http://vuejs.org) components with declarative queries. Compatible with Vue 1.0+ and 2.0+. [Live demo](https://jsfiddle.net/Akryum/oyejk2qL/)
9+
**Warning! This README is related to the next version of vue-apollo. For the stable release, see [here](https://github.com/Akryum/vue-apollo/tree/master).**
10+
11+
Integrates [apollo](http://www.apollodata.com/) in your [Vue](http://vuejs.org) components with declarative queries. Compatible with Vue 1.0+ and 2.0+. [Live demo](https://jsfiddle.net/Akryum/oyejk2qL/)
1012

1113
[<img src="https://assets-cdn.github.com/favicon.ico" alt="icon" width="16" height="16"/> More vue-apollo examples](https://github.com/Akryum/vue-apollo-example)
1214

@@ -50,20 +52,26 @@ Integrates [apollo](http://www.apollostack.com/) in your [Vue](http://vuejs.org)
5052

5153
Try and install these packages before server side set (of packages), add apollo to meteor.js before then, too.
5254

53-
npm install --save vue-apollo apollo-client
55+
npm install --save vue-apollo@next graphql apollo-client apollo-link apollo-link-http apollo-cache-inmemory graphql-tag
5456

5557
In your app, create an `ApolloClient` instance and install the `VueApollo` plugin:
5658

5759
```javascript
5860
import Vue from 'vue'
59-
import { ApolloClient, createBatchingNetworkInterface } from 'apollo-client'
61+
import { ApolloClient } from 'apollo-client'
62+
import { HttpLink } from 'apollo-link-http'
63+
import { InMemoryCache } from 'apollo-cache-inmemory'
6064
import VueApollo from 'vue-apollo'
6165

66+
const httpLink = new HttpLink({
67+
// You should use an absolute URL here
68+
uri: 'http://localhost:3020/graphql',
69+
})
70+
6271
// Create the apollo client
6372
const apolloClient = new ApolloClient({
64-
networkInterface: createBatchingNetworkInterface({
65-
uri: 'http://localhost:3020/graphql',
66-
}),
73+
link: httpLink,
74+
cache: new InMemoryCache(),
6775
connectToDevTools: true,
6876
})
6977

@@ -684,50 +692,57 @@ export const resolvers = {
684692

685693
To make enable the websocket-based subscription, a bit of additional setup is required:
686694

695+
```
696+
npm install --save apollo-link-ws apollo-utilities
697+
```
698+
687699
```javascript
688700
import Vue from 'vue'
689-
import { ApolloClient, createNetworkInterface } from 'apollo-client'
701+
import { ApolloClient } from 'apollo-client'
702+
import { HttpLink } from 'apollo-link-http'
703+
import { InMemoryCache } from 'apollo-cache-inmemory'
690704
// New Imports
691-
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'
705+
import { split } from 'apollo-link'
706+
import { WebSocketLink } from 'apollo-link-ws'
707+
import { getMainDefinition } from 'apollo-utilities'
708+
692709
import VueApollo from 'vue-apollo'
693710

694-
// Create the network interface
695-
const networkInterface = createNetworkInterface({
696-
uri: 'http://localhost:3000/graphql',
697-
transportBatching: true,
711+
const httpLink = new HttpLink({
712+
// You should use an absolute URL here
713+
uri: 'http://localhost:3020/graphql',
698714
})
699715

700-
// Create the subscription websocket client
701-
const wsClient = new SubscriptionClient('ws://localhost:3000/subscriptions', {
702-
reconnect: true,
716+
// Create the subscription websocket link
717+
const wsLink = new WebSocketLink({
718+
uri: 'ws://localhost:3000/subscriptions',
719+
options: {
720+
reconnect: true,
721+
},
703722
})
704723

705-
// Extend the network interface with the subscription client
706-
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
707-
networkInterface,
708-
wsClient,
724+
// using the ability to split links, you can send data to each link
725+
// depending on what kind of operation is being sent
726+
const link = split(
727+
// split based on operation type
728+
({ query }) => {
729+
const { kind, operation } = getMainDefinition(query)
730+
return kind === 'OperationDefinition' &&
731+
operation === 'subscription'
732+
},
733+
wsLink,
734+
httpLink
709735
)
710736

711-
// Create the apollo client with the new network interface
737+
// Create the apollo client
712738
const apolloClient = new ApolloClient({
713-
networkInterface: networkInterfaceWithSubscriptions,
739+
link,
740+
cache: new InMemoryCache(),
714741
connectToDevTools: true,
715742
})
716743

717-
// Install the plugin like before
718-
Vue.use(VueApollo, {
719-
apolloClient,
720-
})
721-
722-
// Your app is now subscription-ready!
723-
724-
import App from './App.vue'
725-
726-
new Vue({
727-
el: '#app',
728-
render: h => h(App)
729-
})
730-
744+
// Install the vue plugin like before
745+
Vue.use(VueApollo)
731746
```
732747

733748
### subscribeToMore
@@ -1346,37 +1361,42 @@ Here is an example:
13461361
// src/api/apollo.js
13471362

13481363
import Vue from 'vue'
1349-
import { ApolloClient, createNetworkInterface } from 'apollo-client'
1364+
import { ApolloClient } from 'apollo-client'
1365+
import { HttpLink } from 'apollo-link-http'
1366+
import { InMemoryCache } from 'apollo-cache-inmemory'
13501367
import VueApollo from 'vue-apollo'
13511368

13521369
// Install the vue plugin
13531370
Vue.use(VueApollo)
13541371

13551372
// Create the apollo client
13561373
export function createApolloClient (ssr = false) {
1357-
let initialState
1374+
const httpLink = new HttpLink({
1375+
// You should use an absolute URL here
1376+
uri: ENDPOINT + '/graphql',
1377+
})
1378+
1379+
const cache = new InMemoryCache()
13581380

13591381
// If on the client, recover the injected state
1360-
if (!ssr && typeof window !== 'undefined') {
1361-
const state = window.__APOLLO_STATE__
1362-
if (state) {
1363-
// If you have multiple clients, use `state.<client_id>`
1364-
initialState = state.defaultClient
1382+
if (!ssr) {
1383+
// If on the client, recover the injected state
1384+
if (typeof window !== 'undefined') {
1385+
const state = window.__APOLLO_STATE__
1386+
if (state) {
1387+
// If you have multiple clients, use `state.<client_id>`
1388+
cache.restore(state.defaultClient)
1389+
}
13651390
}
13661391
}
13671392

13681393
const apolloClient = new ApolloClient({
1369-
networkInterface: createNetworkInterface({
1370-
// You should use an absolute URL here
1371-
uri: 'https://api.graph.cool/simple/v1/cj1jvw20v3n310152sv0sirl7',
1372-
transportBatching: true,
1373-
}),
1394+
link: httpLink,
1395+
cache,
13741396
...(ssr ? {
13751397
// Set this on the server to optimize queries when SSR
13761398
ssrMode: true,
13771399
} : {
1378-
// Inject the state on the client
1379-
initialState,
13801400
// This will temporary disable query force-fetching
13811401
ssrForceFetchDelay: 100,
13821402
}),

0 commit comments

Comments
 (0)