You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -11,7 +11,7 @@ Integrates [apollo](http://www.apollostack.com/) in your [Vue](http://vuejs.org)
11
11
12
12
[<imgsrc="https://assets-cdn.github.com/favicon.ico"alt="icon"width="16"height="16"/> Apollo graphql server example](https://github.com/Akryum/apollo-server-example)
13
13
14
-
[<imgsrc="https://assets-cdn.github.com/favicon.ico"alt="icon"width="16"height="16"/> Apollo "hello world" example app](https://github.com/Akryum/frontpage-vue-app)
14
+
[<imgsrc="https://assets-cdn.github.com/favicon.ico"alt="icon"width="16"height="16"/> Apollo "hello world" example app](https://github.com/Akryum/frontpage-vue-app) (outdated)
15
15
16
16
[<imgsrc="https://cdn-static-1.medium.com/_/fp/icons/favicon-medium.TAS6uQ-Y7kcKgi0xjcYHXw.ico"alt="icon"width="16"height="16"/> Howto on Medium](https://dev-blog.apollodata.com/use-apollo-in-your-vuejs-app-89812429d8b2#.pdd4hmcrc)
17
17
@@ -1068,28 +1068,298 @@ tags: {
1068
1068
1069
1069
## Server-Side Rendering
1070
1070
1071
-
(Work in progress)
1071
+
### Prefetch components
1072
1072
1073
-
Use `apolloProvider.collect()` to being collect queries made against this provider. You get a function that returns a promise when all queries collected are ready. Note that the provider stops collecting queries when you call the function.
1073
+
On the queries you want to prefetch on the server, add the `prefetch` option. It can either be:
1074
+
- a variables object,
1075
+
- a function that gets the context object (which can contain the URL for example) and return a variables object,
1076
+
-`true` (query's `variables` is reused).
1077
+
1078
+
**Warning! You don't have access to the component instance when doing prefetching on the server. Don't use `this` in `prefetch`!**
1079
+
1080
+
Example:
1081
+
1082
+
```javascript
1083
+
exportdefault {
1084
+
apollo: {
1085
+
allPosts: {
1086
+
query:gql`queryAllPosts {
1087
+
allPosts {
1088
+
id
1089
+
imageUrl
1090
+
description
1091
+
}
1092
+
}`,
1093
+
prefetch:true,
1094
+
}
1095
+
}
1096
+
}
1097
+
```
1098
+
1099
+
Example 2:
1100
+
1101
+
```javascript
1102
+
exportdefault {
1103
+
apollo: {
1104
+
post: {
1105
+
query:gql`queryPost($id: ID!) {
1106
+
post (id: $id) {
1107
+
id
1108
+
imageUrl
1109
+
description
1110
+
}
1111
+
}`,
1112
+
prefetch: ({ route }) => {
1113
+
return {
1114
+
id:route.params.id,
1115
+
}
1116
+
},
1117
+
variables () {
1118
+
return {
1119
+
id:this.id,
1120
+
}
1121
+
},
1122
+
}
1123
+
}
1124
+
}
1125
+
```
1126
+
1127
+
You can also tell vue-apollo that some components not used in a `router-view` (and thus not in vue-router `matchedComponents`) need to be prefetched, with the `willPrefetch` method:
1074
1128
1075
1129
```javascript
1076
-
constensureReady=apolloProvider.collect({
1077
-
// If set to false, may resolve when partial/cache result is emitted
1078
-
waitForLoaded:true, // default
1130
+
import { willPrefetch } from'vue-apollo'
1131
+
1132
+
exportdefaultwillPrefetch({
1133
+
apollo: {
1134
+
allPosts: {
1135
+
query:gql`queryAllPosts {
1136
+
allPosts {
1137
+
id
1138
+
imageUrl
1139
+
description
1140
+
}
1141
+
}`,
1142
+
prefetch:true, // Don't forget this
1143
+
}
1144
+
}
1079
1145
})
1146
+
```
1080
1147
1081
-
newVue({
1082
-
el:'#app',
1083
-
apolloProvider,
1084
-
render:h=>h(App),
1148
+
### On the server
1149
+
1150
+
To prefetch all the apollo queries you marked, use the `apolloProvider.prefetchAll` method. The first argument is the context object passed to the `prefetch` hooks (see above). It is recommended to pass the vue-router `currentRoute` object. The second argument is the array of component definition to include (e.g. from `router.getMatchedComponents` method). The third argument is an optional `options` object. It returns a promise resolved when all the apollo queries are loaded.
1151
+
1152
+
Here is an example with vue-router and a Vuex store:
Use the `apolloProvider.exportStates` method to get the JavaScript code you need to inject into the generated page to pass the apollo cache data to the client.
1217
+
1218
+
It takes an `options` argument which defaults to:
1219
+
1220
+
```javascript
1221
+
{
1222
+
// Global variable name
1223
+
globalName:'__APOLLO_STATE__',
1224
+
// Global object on which the variable is set
1225
+
attachTo:'window',
1226
+
// Prefix for the keys of each apollo client state
1227
+
exportNamespace:'',
1228
+
}
1229
+
```
1230
+
1231
+
### Creating the Apollo Clients
1232
+
1233
+
It is recommended to create the apollo clients inside a function with an `ssr` argument, which is `true` on the server and `false` on the client.
0 commit comments