Replies: 16 comments
-
As far as I can see, each time you click a link it will call if (activated.length) {
apolloProvider.prefetchQueries = [];
await Promise.all([
...activated.map(
({ asyncData }) => asyncData && asyncData({ axios, store, route: to }),
),
apolloProvider.prefetchAll({ route: to }, activated),
])
} I'm not sure though if this is the right way but it seems to work for me. I think a better solution would be to have an API method to prefetch a single route rather than calling |
Beta Was this translation helpful? Give feedback.
-
An issue with this implementation in general is that, a network request is always sent even if the data is in apollo store. A solution or a direction to do this right would really be very helpful. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure what's the use case for |
Beta Was this translation helpful? Give feedback.
-
@Akryum Isn't this issue clear? I want to prefetch data before jumping into next route like |
Beta Was this translation helpful? Give feedback.
-
Here are two different solutions I can think of for prefetching data before moving to next route. These solutions don't depend on Option 1: Using Vuex export default {
computed: {
product() {
return this.$store.state.product;
},
},
async asyncData({ apolloProvider, route, store }) {
const { data } = await apolloProvider.defaultClient.query({
query: GET_PRODUCT,
variables: {
id: parseInt(route.params.id, 10),
},
});
store.commit("UPDATE_PRODUCT", data.getProduct);
},
}; Option 2: Using Apollo Store const buildQuery = route => ({
query: GET_PRODUCT,
variables: {
id: parseInt(route.params.id, 10),
},
});
export default {
computed: {
product() {
return this.$apolloProvider.defaultClient.readQuery(
buildQuery(this.$route),
).getProduct;
},
},
async asyncData({ apolloProvider, route }) {
await apolloProvider.defaultClient.query(buildQuery(route));
},
}; I'm not in favor of using Vuex since the same data is already inside Apollo store. But I'm not a fan of the way we have to read data from Apollo store. Any other ideas are welcomed. I still think it'd be really cool if Vue Apollo provided a method to achieve this. I think this issue is more of a feature request. |
Beta Was this translation helpful? Give feedback.
-
Yes, I'm using Apollo store in |
Beta Was this translation helpful? Give feedback.
-
Just realized that, in my Apollo Store example, computed property @JounQin since you said you're using a similar approach, do you have this problem? |
Beta Was this translation helpful? Give feedback.
-
@ozguruysal My App is so simple which calls GitHub GraphQL API inside, so I'm manually using https://github.com/JounQin/blog/blob/master/src/views/Home.vue |
Beta Was this translation helpful? Give feedback.
-
Thanks @JounQin. |
Beta Was this translation helpful? Give feedback.
-
@ozguruysal your proposed |
Beta Was this translation helpful? Give feedback.
-
@dohomi yes it worked for me with SSR (I have a set up very similar to vue-hackernews-2.0 example) but the data was not reactive as discussed above. Eventually I ended up with a solution like this: const buildQuery = route => ({
query: GET_PRODUCT,
variables: {
id: parseInt(route.params.id, 10),
},
});
export default {
data() {
return {
product: {},
};
},
created() {
this.fetchLocalData();
},
async asyncData({ apolloProvider, route }) {
await apolloProvider.defaultClient.query(buildQuery(route));
},
methods: {
fetchLocalData() {
this.$apollo.addSmartQuery("getProduct", {
...buildQuery(this.$route),
update(data) {
this.product = data.getProduct;
},
fetchPolicy: "cache-only",
});
},
},
}; Alternatively you can use |
Beta Was this translation helpful? Give feedback.
-
@ozguruysal one question: how do you asign anything via asyncData? and another one: if you do fetchPolicy: "cache-only" this means you pulled already all data right? Because in my opinion on a normal search where you only publish like 20 items on one query you would need to re-fetch the data from the server. I was playing around today and came up with a very similar solution: |
Beta Was this translation helpful? Give feedback.
-
@dohomi regarding your first question, if you mean something like updating component data, I don't do anything like that because Second question; yes all data must have been already fetched and available in apollo store at that point since If you need to call |
Beta Was this translation helpful? Give feedback.
-
@ozguruysal ok thanks. I am using Nuxt as you can see in my gist so thats the solution I'm doing right now and asyncData is setting the context on the component directly. So I guess we both came up with the same path even though different setup. |
Beta Was this translation helpful? Give feedback.
-
To fix this, I did the same thing as @ozguruysal of initializing The reason for querying twice is that To use the cache instead of always making a request, I patched https://gist.github.com/tfoxy/03beda1d680a2644fb90cdcedacc8305 |
Beta Was this translation helpful? Give feedback.
-
Should this really be tagged a const options = omit(localQueryOptions, VUE_APOLLO_QUERY_KEYWORDS);
//...
if (process.env.VUE_ENV === 'server') {
options.fetchPolicy = 'network-only';
} Shouldn't that change make it into the library itself? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
After reading document,
prefetchAll
is only used on server, but like vue-hackernews-2.0asyncData
can also be called inbeforeResolve
hook, and I tried to addprefetchAll
on client also like following:However it seems it will try to query twice, and also when I leave current route,
prefetch
will also be called although I've passed correct activated routes.Is that working as expected?
Beta Was this translation helpful? Give feedback.
All reactions