|
1 | 1 | # Apollo and GraphQL for Vue.js |
2 | 2 |
|
3 | 3 | [ ](https://www.npmjs.com/package/vue-apollo) |
4 | | -[](http://apollodata.com/) |
| 4 | +[](http://apollodata.com/) |
5 | 5 | [ ](https://vuejs.org/) |
6 | 6 |
|
7 | 7 |  |
8 | 8 |
|
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/) |
10 | 12 |
|
11 | 13 | [<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) |
12 | 14 |
|
@@ -50,20 +52,26 @@ Integrates [apollo](http://www.apollostack.com/) in your [Vue](http://vuejs.org) |
50 | 52 |
|
51 | 53 | Try and install these packages before server side set (of packages), add apollo to meteor.js before then, too. |
52 | 54 |
|
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 |
54 | 56 |
|
55 | 57 | In your app, create an `ApolloClient` instance and install the `VueApollo` plugin: |
56 | 58 |
|
57 | 59 | ```javascript |
58 | 60 | 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' |
60 | 64 | import VueApollo from 'vue-apollo' |
61 | 65 |
|
| 66 | +const httpLink = new HttpLink({ |
| 67 | + // You should use an absolute URL here |
| 68 | + uri: 'http://localhost:3020/graphql', |
| 69 | +}) |
| 70 | + |
62 | 71 | // Create the apollo client |
63 | 72 | const apolloClient = new ApolloClient({ |
64 | | - networkInterface: createBatchingNetworkInterface({ |
65 | | - uri: 'http://localhost:3020/graphql', |
66 | | - }), |
| 73 | + link: httpLink, |
| 74 | + cache: new InMemoryCache(), |
67 | 75 | connectToDevTools: true, |
68 | 76 | }) |
69 | 77 |
|
@@ -684,50 +692,57 @@ export const resolvers = { |
684 | 692 |
|
685 | 693 | To make enable the websocket-based subscription, a bit of additional setup is required: |
686 | 694 |
|
| 695 | +``` |
| 696 | +npm install --save apollo-link-ws apollo-utilities |
| 697 | +``` |
| 698 | + |
687 | 699 | ```javascript |
688 | 700 | 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' |
690 | 704 | // 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 | + |
692 | 709 | import VueApollo from 'vue-apollo' |
693 | 710 |
|
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', |
698 | 714 | }) |
699 | 715 |
|
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 | + }, |
703 | 722 | }) |
704 | 723 |
|
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 |
709 | 735 | ) |
710 | 736 |
|
711 | | -// Create the apollo client with the new network interface |
| 737 | +// Create the apollo client |
712 | 738 | const apolloClient = new ApolloClient({ |
713 | | - networkInterface: networkInterfaceWithSubscriptions, |
| 739 | + link, |
| 740 | + cache: new InMemoryCache(), |
714 | 741 | connectToDevTools: true, |
715 | 742 | }) |
716 | 743 |
|
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) |
731 | 746 | ``` |
732 | 747 |
|
733 | 748 | ### subscribeToMore |
@@ -1346,37 +1361,42 @@ Here is an example: |
1346 | 1361 | // src/api/apollo.js |
1347 | 1362 |
|
1348 | 1363 | 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' |
1350 | 1367 | import VueApollo from 'vue-apollo' |
1351 | 1368 |
|
1352 | 1369 | // Install the vue plugin |
1353 | 1370 | Vue.use(VueApollo) |
1354 | 1371 |
|
1355 | 1372 | // Create the apollo client |
1356 | 1373 | 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() |
1358 | 1380 |
|
1359 | 1381 | // 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 | + } |
1365 | 1390 | } |
1366 | 1391 | } |
1367 | 1392 |
|
1368 | 1393 | 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, |
1374 | 1396 | ...(ssr ? { |
1375 | 1397 | // Set this on the server to optimize queries when SSR |
1376 | 1398 | ssrMode: true, |
1377 | 1399 | } : { |
1378 | | - // Inject the state on the client |
1379 | | - initialState, |
1380 | 1400 | // This will temporary disable query force-fetching |
1381 | 1401 | ssrForceFetchDelay: 100, |
1382 | 1402 | }), |
|
0 commit comments