invoke
This is for imperatively invoking queries or mutations from your components.
import {invoke} from "blitz"
import getProducts from "app/products/queries/getProducts"
const Page = function ({products}) {
return (
<div>
<button
onClick={async () => {
const products = await invoke(getProducts, {where: {orgId: 1}})
}}
>
Get Products
</button>
</div>
)
}
export default Page
const results = await invoke(resolver, resolverInputArguments)
resolver:
A Blitz query resolver or
mutation resolverresolverInputArguments
resolver
results
invalidateQuery
This allows you to invalidate a specific query.
import {invalidateQuery} from "blitz" // highlight-line
import getProducts from "app/products/queries/getProducts"
const Page = function ({products}) {
return (
<div>
<button
onClick={() => {
invalidateQuery(getProducts) // highlight-line
}}
>
Invalidate
</button>
</div>
)
}
export default Page
const refetchedQueries = await invalidateQuery(resolver, inputArgs?)
resolver:
A Blitz query resolver or
mutation resolverinputArgs
resolver
resolver
queriesresolver
with matching inputArgs
This function returns a promise that will resolve when all of the queries are done being refetched.
getQueryKey
This allows you to get the query key for a query
import {getQueryKey} from "blitz"
import getProducts from "app/products/queries/getProducts"
const Page = function ({products}) {
return (
<div>
<button
onClick={async () => {
const queryKey = getQueryKey(getProducts)
queryCache.invalidateQueries(queryKey)
const queryKey = getQueryKey(getProducts, {where: {ordId: 1}})
queryCache.invalidateQueries(queryKey)
}}
>
Invalidate
</button>
</div>
)
}
export default Page
const queryKey = getQueryKey(resolver, inputArgs?)
resolver:
A Blitz query resolver or
mutation resolverinputArgs
resolver
resolver
queriesresolver
and inputArgs
combinationresults
setQueryData
Works the same as the
setQueryData returned by useQuery. The
difference is that it's not bound to the useQuery and expects the
resolver
and the inputArgs
instead. It will also invalidate the query
causing a refetch. Disable refetch by passing an options object
{refetch: false}
as the opts
argument.
import {setQueryData} from "blitz"
export default function (props: {query: {id: number}}) {
const [product] = useQuery(getProduct, {where: {id: props.query.id}})
const [updateProjectMutation] = useMutation(updateProject)
return (
<Formik
initialValues={product}
onSubmit={async (values) => {
try {
const product = await updateProjectMutation(values)
setQueryData(getProduct, {where: {id: props.query.id}}, product)
} catch (error) {
alert("Error saving product")
}
}}
>
{/* ... */}
</Formik>
)
}
const promise = setQueryData(resolver, inputArgs, newData, opts?)
resolver:
A Blitz query resolver.inputArgs
resolver
resolver
and inputArgs
combinationnewData
opts
{refetch: false}
.results