-
-
Notifications
You must be signed in to change notification settings - Fork 522
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot query on button click? (in v4 composition API) #909
Comments
Hi, i searched for it a lot too, but it is writtin in their documentation vue apollo v4e
just add the "refetch" to the useQuery line and call it from the button click event. Best Regards |
@lTimeless, thanks for the answer. Does the example query the server two times (one in setup and one in refetch) or only one time (in refetch)? Thanks. |
@lTimeless thanks for the answer. |
Hello @ozum I'm experiencing the same issue. |
@aaronschweig currently I have to switch back to old API, because some of the libraries I use did not support composition API yet. Using old API, I could not find a solution other than |
Hello guys, any solution for it yet? same problem. |
Hello @mtsmachado8 i've found a working solution. const queryOptions = ref({
// whatever options you need
enabled: false
})
const {result} = useQuery(<query>, <variables>,queryOptions)
// Do with the result what you need
const onClick = () => {
queryOptions.value.enabled = true
} This works for me and queries only once, when the Button is clicked. For me, instead of only doing the const {result, refetch} = useQuery(<query>, <variables>,queryOptions)
const onClick = () => {
if (!queryOptions.value.enabled) {
queryOptions.value.enabled = true
return
}
refetch()
} I hope this helps! |
Thankss @aaronschweig, it really helped!!! |
This seems very inelegant. Executing a query on click (or whatever event) should work as expected imo. |
Is there a solution for this if you arent even using an event to trigger query? I'm using quasar ssr and am getting There is nothing special triggering the query its just within the setup function. |
Is there a way to prevent the initial query from executing with useQuery? My code is written as follows: export default function useUserQuery() {
const { refetch } = useQuery(UserQuery);
return async function fetchUser(userId) {
const { data } = await refetch({ userId });
return data.user;
};
} This works as intended, but the initial query is being run (without variables and thus is throwing an error in the console, but all subsequent queries with |
Can you get this disable/enable demo to work with the latest alpha version? |
Both |
Also, seems like there's no clean way to perform a one-shot request or reuse the same one on-demand (without loading it first). In Options API you could do this const result = await this.$apollo.query({
query: gql`...`,
variables: { param: 'hello' },
}) You still can, via const result = await root.$apollo.query({
query: gql`...`,
variables: { param: 'hello' },
}) Using I guess I could create a Promise which wraps the result ref and resolve when it emits, but it feels a bit hacky. |
Besides |
@paulhaem Getting that error as well. And example-code that triggers the error above. It is in a vuex-module. |
I tried to do this instead now @Akryum: And I am getting this error in console (catching it in a try/catch): Update: I saw that I missed to pass variable, but I changed that and I'm getting the error anyway. |
Might be on track to something. This issue pointed me in right direction: #1029 (comment) I do not use Vite-plugin, so I removed it manually... |
The right way to do it is not to use import { useApolloClient } from '@vue/apollo-composable'
const { client } = useApolloClient()
const onClick = async () => {
const { data } = await client.query({
query: someQuery,
variables: someVariablesIfNeeded,
})
} |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
withNuxt
. (Not@nuxtjs/apollo
, because it doesn't supportvue-apollo
v4 yet.)I tried below methods:
Many thanks,
The text was updated successfully, but these errors were encountered: