Examples of using Pinia Colada in Pinia stores? #214
-
Hello! I was wondering if there are any good examples of using Pinia Colada with Pinia? The main reason I want to use it in Pinia is for when I need global state. As a pseudo code example, let's say I want to fetch a user from a GraphQL endpoint. Would a Pinia setup store look something like this: export const useUserStore = defineStore('user-store', () => {
const { data, error, isLoading, isPending } = useQuery({
key: ['user'],
query: () => client.request(document),
})
return {
data,
error,
isLoading,
isPending,
}
}) Additionally, when would the actual API call be made? Do I need to manually call Appreciate any help and insight! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Hey, maybe this helps: https://pinia-colada.esm.dev/guide/queries.html#Caveat-SSR-and-defineQuery- |
Beta Was this translation helpful? Give feedback.
-
@onmax thanks for the reply! Looking at that example, I'm still a bit confused... I'm looking to use this in a Vue app without SSR (not Nuxt), so I removed the SSR portion for simplicity. return {
search,
...query,
} import { defineStore, storeToRefs } from 'pinia'
const useLocalStore = defineStore('query-search-store', () => {
const search = ref('')
return { search }
})
defineQuery(() => {
const { search } = storeToRefs(useLocalStore())
const query = useQuery({
key: ['todos', search.value],
query: () => fetch(`/api/todos?search=${search.value}`).then((res) => res.json()),
})
return {
search,
...query,
}
}) |
Beta Was this translation helpful? Give feedback.
-
Regarding using queries in stores, the warning at https://pinia-colada.esm.dev/guide/queries.html#Pausing-queries is relevant:
|
Beta Was this translation helpful? Give feedback.
For that you can use: