Resolver Utilities

invalidateQuery

This allows you to invalidate a specific query.

Example

  1. import {invalidateQuery} from "blitz" import getProducts from "app/products/queries/getProducts"const Page = function ({products}) { return ( <div> <button onClick={() => { invalidateQuery(getProducts) }} > Invalidate </button> </div> )}export default Page

API

  1. const refetchedQueries = await invalidateQuery(resolver, inputArgs?)

Arguments

  • resolver: A Blitz query resolver or mutation resolver
    • Required
  • inputArgs
    • Optional
    • The arguments that will be passed to resolver
    • If you don’t provide this, it will invalidate all resolver queries
    • If you do provide this, it will only invalidate queries for resolver with matching inputArgs

Returns

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

Example

  1. 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

API

  1. const queryKey = getQueryKey(resolver, inputArgs?)

Arguments

  • resolver: A Blitz query resolver or mutation resolver
    • Required
  • inputArgs
    • Optional
    • The arguments that will be passed to resolver
    • If you don’t provide this, it will return the base query key for all resolver queries
    • If you do provide this, it will return the specific query key for the resolver and inputArgs combination

Returns

  • results
    • The exact results returned from the resolver

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.

Example

  1. 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> )}

API

  1. const promise = setQueryData(resolver, inputArgs, newData, opts?)

Arguments

  • resolver: A Blitz query resolver.
    • Required
  • inputArgs
    • Required
    • The arguments that will be passed to resolver
    • It will set the data for the specified resolver and inputArgs combination
  • newData
    • Required
    • If non-function is passed, the data will be updated to this value
    • If a function is passed, it will receive the old data value and be expected to return a new one.
  • opts
    • Optional
    • An object with additional options.
    • Disable refetch by passing an options object {refetch: false}.

Returns

  • results
    • Returns a promise that will be resolved when the new data is refetched.