apollo: Cannot query on button click? (in v4 composition API)
Thanks for the library and effort given.
Similar to #121, I need an example how to query after an event (such as button click) in v4 composition API?
Further info:
I’m using vue-apollo with Nuxt. (Not @nuxtjs/apollo, because it doesn’t support vue-apollo v4 yet.)
I tried below methods:
setup(props, context) {
const enabled = ref(false)
const q1 = useQuery(someQuery); // This is OK.
// When I add below line, browser hangs during page load and waits forever.
const q2 = useQuery(someQuery, null, { enabled: enabled.value });
// Click handler throws when executed on click event.
// Cannot read property '$isServer' of null at useQuery (useQuery.js?a2fd:25)
function onClick() {
const q3 = useQuery(someQuery);
}
}
Many thanks,
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 22 (5 by maintainers)
Hello @mtsmachado8 i’ve found a working solution.
This works for me and queries only once, when the Button is clicked. For me, instead of only doing the
enabledas aref, using the whole options-obejct as arefdid the trick.After that, if you want to requery on every click, you can use therefetch-Option from theuseQuerylikeI hope this helps!
This seems very inelegant. Executing a query on click (or whatever event) should work as expected imo.
useLazyQuery
The right way to do it is not to use
useQueryneitheruseLazyQuerybut insteaduseApolloClientlike this:Thankss @aaronschweig, it really helped!!!