Imperatively Invoke Queries and Mutations

We provide

invoke for imperatively invoking queries or mutations from your components. We also provide invokeWithMiddleware for imperatively invoking queries/mutations on the server with middleware.

invoke

This is for imperatively invoking queries or mutations from your components.

Example

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

API

const results = await invoke(resolver, resolverInputArguments)

Arguments

  • resolver: A Blitz query resolver or mutation resolver
    • Required
  • resolverInputArguments
    • Required
    • The arguments that will be passed to resolver

Returns

  • results
    • The exact results returned from the resolver

invokeWithMiddleware

This is for imperatively invoking queries/mutations on the server. It ensures appropriate middleware is run before/after your resolver.

Example

import {invokeWithMiddleware, GetServerSideProps, Link, BlitzPage, PromiseReturnType} from "blitz"
import getProducts from "app/products/queries/getProducts"
type PageProps = {
products: PromiseReturnType<typeof getProducts>
}
export const getServerSideProps: GetServerSideProps<PageProps> = async ({req, res}) => {
const products = await invokeWithMiddleware(getProducts, {orderBy: {id: "desc"}}, {req, res})
return {
props: {products},
}
}
const Page: BlitzPage<PageProps> = function ({products}) {
return (
<div>
<h1>Products</h1>
<div id="products">
{products.map((product) => (
<p key={product.id}>
<Link href="/products/[handle]" as={`/products/${product.handle}`}>
<a>{product.name}</a>
</Link>
</p>
))}
</div>
</div>
)
}
export default Page

API

const results = await invokeWithMiddleware(resolver, resolverInputArguments, {req, res}))

Arguments

  • resolver: A Blitz query resolver or mutation resolver
    • Required
  • resolverInputArguments
    • Required
    • The arguments that will be passed to resolver
  • Other
    • req
      • Required
      • The BlitzApiRequest object
    • res
      • Required
      • The BlitzApiResponse object
    • middleware
      • Optional
      • Array of middleware that will be ran in addition to global middleware and any local middleware defined on the query/mutation.

Returns

  • results
    • The exact results returned from the resolver
Idea for improving this page?Edit it on GitHub