To use one of your Blitz queries, call the useQuery
hook with:
It returns this tuple array,[queryResult, queryExtras], where
queryResult is exactly what's returned from your query function.
useQuery is built on
react-query, so it
automatically provides awesome features such as automatic caching and
revalidation.
import {useQuery} from "blitz"
import getProject from "app/projects/queries/getProject"
function App() {
const [project] = useQuery(getProject, {where: {id: 1}})
}For complete details, see the useQuery documentation.
You may be wondering how that can work since it's importing server code into your component: At build time, the direct function import is swapped out with a network call. So the query function code is never included in your client code.
Out of the box, useQuery and the other query hooks are configured with
aggressive but reasonable defaults. Sometimes these defaults can
catch new users off guard or make learning/debugging difficult if they are
unknown by the user.:
staleTime for queries to
something other than 0 milliseconds.cacheTime for queries to something other
than 1000 * 60 * 5 milliseconds.refetchOnWindowFocus and
refetchOnReconnect options in queries.retry and retryDelay options for queries.config.isDataEqual) only supports comparing JSON-compatible
primitives. If you are dealing with any non-json compatible values in
your query responses OR are seeing performance issues with the deep
compare function, you should probably disable it
(config.isDataEqual = () => false) or customize it to better fit your
needs.See the useQuery documentation for a full list of
possible options.
Dependent queries are queries that depend on previous ones to finish before they can execute. To do this, use the enabled option to tell a query when it is ready to turn on:
const [user] = useQuery(getUser, {where: {id: props.query.id}})
const [projects] = useQuery(
getProjects,
() => ({where: {userId: user.id}}, {enabled: user}),
)Use the usePaginatedQuery hook
Use the useInfiniteQuery hook
Both of the following will cache the getProject query.
const project = await getProject({where: {id: props.id}})<a onMouseEnter={() => getProject({where: {id: projectId}})}>
View Project
</a>getStaticPropsIn getStaticProps, a query function can be called directly without
useQuery
import getProject from "app/projects/queries/getProject"
export const getStaticProps = async (context) => {
const project = await getProject({
where: {id: context.params?.projectId},
})
return {props: {project}}
}
function ProjectPage({project}) {
return <div>{project.name}</div>
}
export default ProjectPagegetServerSidePropsIn getServerSideProps, pass a query function to
invokeWithMiddleware
which will ensure appropriate middleware is run before/after your query
function.
import {invokeWithMiddleware} from 'blitz'
import getProject from 'app/projects/queries/getProject'
export const getServerSideProps = async ({params, req, res}) => {
const project = await invokeWithMiddleware(getProject, {where: {id: params.projectId}}, {req, res}))
return {props: {project}}
}
function ProjectPage ({project}) {
return <div>{project.name}</div>
}
export default ProjectPage