|
| 1 | +// app.js |
| 2 | + |
| 3 | +import VueApollo from 'vue-apollo' |
| 4 | + |
| 5 | +Vue.use(VueApollo) |
| 6 | + |
| 7 | +export default function createApp ({ createApolloClients }) { |
| 8 | + const router = new VueRouter({}) |
| 9 | + const store = new Vuex.Store({}) |
| 10 | + const { apolloClientA, apolloClientB } = createApolloClients() |
| 11 | + const apollo = new VueApollo({ |
| 12 | + clients: { |
| 13 | + a: apolloClientA, |
| 14 | + b: apolloClientB, |
| 15 | + }, |
| 16 | + defaultClient: apolloClientA, |
| 17 | + }) |
| 18 | + |
| 19 | + const ensureReady = apollo.collect() |
| 20 | + |
| 21 | + const app = new Vue({ |
| 22 | + el: '#app', |
| 23 | + router, |
| 24 | + store, |
| 25 | + apollo, |
| 26 | + ...App, |
| 27 | + }) |
| 28 | + |
| 29 | + return { app, router, store, apollo, ensureReady } |
| 30 | +} |
| 31 | + |
| 32 | +// server.js |
| 33 | + |
| 34 | +import { ApolloClient, createNetworkInterface } from 'apollo-client' |
| 35 | +import { createLocalInterface } from 'apollo-local-query' |
| 36 | +import graphql from 'graphql' |
| 37 | +import { schema } from './graphql/schema' |
| 38 | + |
| 39 | +function createApolloClients() { |
| 40 | + const apolloClientA = new ApolloClient({ |
| 41 | + ssrMode: true, |
| 42 | + networkInterface: createLocalInterface(graphql, schema) |
| 43 | + }) |
| 44 | + |
| 45 | + const apolloClientB = new ApolloClient({ |
| 46 | + ssrMode: true, |
| 47 | + networkInterface: createNetworkInterface({ |
| 48 | + uri: 'http://my-awesome-api/graphql', |
| 49 | + transportBatching: true, |
| 50 | + }), |
| 51 | + }) |
| 52 | + |
| 53 | + return { |
| 54 | + apolloClientA, |
| 55 | + apolloClientB, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export default function render (context) { |
| 60 | + const { app, router, store, apollo, ensureReady } = createApp({ createApolloClients }) |
| 61 | + router.push(context.url) |
| 62 | + router.onReady(async () => { |
| 63 | + // Wait for apollo queries to be loaded |
| 64 | + await ensureReady() |
| 65 | + |
| 66 | + // Inject initial apollo states |
| 67 | + let js = `window.__APOLLO_STATE__ = {` |
| 68 | + for (const key in apollo.clients) { |
| 69 | + const client = apollo.clients[key] |
| 70 | + const state = {[client.reduxRootKey]: client.getInitialState() } |
| 71 | + js += `['${key}']:${JSON.stringify(state)},` |
| 72 | + } |
| 73 | + js += `};` |
| 74 | + |
| 75 | + // TODO Render here |
| 76 | + |
| 77 | + // TODO Add the js to the response |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +// client.js |
| 82 | + |
| 83 | +import { ApolloClient, createNetworkInterface } from 'apollo-client' |
| 84 | + |
| 85 | +function createApolloClients() { |
| 86 | + const state = window.__APOLLO_STATE__ |
| 87 | + |
| 88 | + const apolloClientA = new ApolloClient({ |
| 89 | + networkInterface: createNetworkInterface({ |
| 90 | + uri: '/graphql', |
| 91 | + transportBatching: true, |
| 92 | + }), |
| 93 | + initialState: state.a, |
| 94 | + ssrForceFetchDelay: 100, |
| 95 | + }) |
| 96 | + |
| 97 | + const apolloClientB = new ApolloClient({ |
| 98 | + networkInterface: createNetworkInterface({ |
| 99 | + uri: 'http://my-awesome-api/graphql', |
| 100 | + transportBatching: true, |
| 101 | + }), |
| 102 | + initialState: state.b, |
| 103 | + ssrForceFetchDelay: 100, |
| 104 | + }) |
| 105 | + |
| 106 | + export { |
| 107 | + apolloClientA, |
| 108 | + apolloClientB, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +createApp({ createApolloClients }) |
0 commit comments