Skip to content

Commit c17f722

Browse files
author
Guillaume Chau
committed
Migration path
1 parent e32a5af commit c17f722

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

README.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,199 @@ router.onReady(() => {
14911491

14921492
---
14931493

1494+
# Migration
1495+
1496+
## Migrating from vue-apollo 2.x and apollo 1.x
1497+
1498+
The main changes are related to the apollo client setup. Your components code shouldn't be affected. Apollo now uses a more flexible [apollo-link](https://github.com/apollographql/apollo-link) system that allows compositing multiple links together to add more features (like batching, offline support and more).
1499+
1500+
### Installation
1501+
1502+
#### Packages
1503+
1504+
**Before**
1505+
1506+
```
1507+
npm install --save vue-apollo apollo-client
1508+
```
1509+
1510+
**After**
1511+
1512+
```
1513+
npm install --save vue-apollo@next graphql apollo-client apollo-link apollo-link-http apollo-cache-inmemory graphql-tag
1514+
```
1515+
1516+
#### Imports
1517+
1518+
**Before**
1519+
1520+
```js
1521+
import Vue from 'vue'
1522+
import { ApolloClient, createBatchingNetworkInterface } from 'apollo-client'
1523+
import VueApollo from 'vue-apollo'
1524+
```
1525+
1526+
**After**
1527+
1528+
```js
1529+
import Vue from 'vue'
1530+
import { ApolloClient } from 'apollo-client'
1531+
import { HttpLink } from 'apollo-link-http'
1532+
import { InMemoryCache } from 'apollo-cache-inmemory'
1533+
import VueApollo from 'vue-apollo'
1534+
```
1535+
1536+
#### Plugin Setup
1537+
1538+
**Before**
1539+
1540+
```js
1541+
// Create the apollo client
1542+
const apolloClient = new ApolloClient({
1543+
networkInterface: createBatchingNetworkInterface({
1544+
uri: 'http://localhost:3020/graphql',
1545+
}),
1546+
connectToDevTools: true,
1547+
})
1548+
1549+
// Install the vue plugin
1550+
Vue.use(VueApollo)
1551+
```
1552+
1553+
**After**
1554+
1555+
```js
1556+
const httpLink = new HttpLink({
1557+
// You should use an absolute URL here
1558+
uri: 'http://localhost:3020/graphql',
1559+
})
1560+
1561+
// Create the apollo client
1562+
const apolloClient = new ApolloClient({
1563+
link: httpLink,
1564+
cache: new InMemoryCache(),
1565+
connectToDevTools: true,
1566+
})
1567+
1568+
// Install the vue plugin
1569+
Vue.use(VueApollo)
1570+
```
1571+
1572+
### Mutations
1573+
1574+
Query reducers have been removed. Use the `update` API to update the cache now.
1575+
1576+
### Subscriptions
1577+
1578+
#### Packages
1579+
1580+
**Before**
1581+
1582+
```
1583+
npm install --save subscriptions-transport-ws
1584+
```
1585+
1586+
**After**
1587+
1588+
```
1589+
npm install --save apollo-link-ws apollo-utilities
1590+
```
1591+
1592+
#### Imports
1593+
1594+
**Before**
1595+
1596+
```js
1597+
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'
1598+
```
1599+
1600+
**After**
1601+
1602+
```js
1603+
import { split } from 'apollo-link'
1604+
import { WebSocketLink } from 'apollo-link-ws'
1605+
import { getMainDefinition } from 'apollo-utilities'
1606+
```
1607+
1608+
#### Apollo Setup
1609+
1610+
**Before**
1611+
1612+
```js
1613+
// Create the network interface
1614+
const networkInterface = createNetworkInterface({
1615+
uri: 'http://localhost:3000/graphql',
1616+
transportBatching: true,
1617+
})
1618+
1619+
// Create the subscription websocket client
1620+
const wsClient = new SubscriptionClient('ws://localhost:3000/subscriptions', {
1621+
reconnect: true,
1622+
})
1623+
1624+
// Extend the network interface with the subscription client
1625+
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
1626+
networkInterface,
1627+
wsClient,
1628+
)
1629+
1630+
// Create the apollo client with the new network interface
1631+
const apolloClient = new ApolloClient({
1632+
networkInterface: networkInterfaceWithSubscriptions,
1633+
connectToDevTools: true,
1634+
})
1635+
1636+
// Install the plugin like before
1637+
Vue.use(VueApollo)
1638+
```
1639+
1640+
**After**
1641+
1642+
```js
1643+
const httpLink = new HttpLink({
1644+
// You should use an absolute URL here
1645+
uri: 'http://localhost:3020/graphql',
1646+
})
1647+
1648+
// Create the subscription websocket link
1649+
const wsLink = new WebSocketLink({
1650+
uri: 'ws://localhost:3000/subscriptions',
1651+
options: {
1652+
reconnect: true,
1653+
},
1654+
})
1655+
1656+
// using the ability to split links, you can send data to each link
1657+
// depending on what kind of operation is being sent
1658+
const link = split(
1659+
// split based on operation type
1660+
({ query }) => {
1661+
const { kind, operation } = getMainDefinition(query)
1662+
return kind === 'OperationDefinition' &&
1663+
operation === 'subscription'
1664+
},
1665+
wsLink,
1666+
httpLink
1667+
)
1668+
1669+
// Create the apollo client
1670+
const apolloClient = new ApolloClient({
1671+
link,
1672+
cache: new InMemoryCache(),
1673+
connectToDevTools: true,
1674+
})
1675+
1676+
// Install the vue plugin like before
1677+
Vue.use(VueApollo)
1678+
```
1679+
1680+
1681+
<br>
1682+
1683+
Learn more at the [official apollo documentation](https://www.apollographql.com/docs/react/2.0-migration.html).
1684+
1685+
---
1686+
14941687
# API Reference
14951688

14961689
WIP (PR welcome!)

0 commit comments

Comments
 (0)