defineQuery in composables with parameters #100
-
Is it necessary to use // outside
const todoId = ref('my-id')
const {data} = useTodo(todoId)
// composable to group all functionality for a single todo
const useTodo = (id: Ref<string>) => {
const query = useQuery({
key: () => ['todo', id.value],
query: () => {
return fetch(`/api/todos/${id}`)
}
})
// other function regarding a todo, e.g. a mutation
const otherFunction = () => {...}
return {...query, otherFunction}
} The |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
In this case of scenario, the // composable to group all functionality for a single todo
const useTodo = defineQuery(() => {
const id = ref('my-id')
const query = useQuery({
key: () => ['todo', id.value],
query: () => {
return fetch(`/api/todos/${id}`)
},
})
// other function regarding a todo, e.g. a mutation
const otherFunction = () => {}
return { ...query, otherFunction, id }
}) This makes |
Beta Was this translation helpful? Give feedback.
-
This can be achieved now with You can do dynamic queries with keys too: https://pinia-colada.esm.dev/guide/query-keys.html#Typing-query-keys |
Beta Was this translation helpful? Give feedback.
This can be achieved now with
defineQueryOptions
: https://pinia-colada.esm.dev/guide/queries.html#Organizing-Queries 🎉In your specific case, you can use the query within a composable
You can do dynamic queries with keys too: https://pinia-colada.esm.dev/guide/query-keys.html#Typing-query-keys