I have two graphql queries first one is getUser and second one is getChatHistory. I want getChatHistory to execute only after getUser is executed and user details are fetched.
export const getChatHistory = gql`{
chatHistory {
title
}
}
`;
export const getUser = gql`{
user {
name
email
}
}
`;
const Home = ()=> {
const {loading: chatLoading,error:chatError,data:chatData}=useQuery(getChatHistory);
const {data:userData} = useQuery(getUser);
...
2
Answers
You can’t do that in a single component using
useQuery
since it’s a hook and a hook can’t be called conditionally – make a separate nested component that grabs the chat history and don’t render it until you’ve got the user.It is possible. Based on useQuery hook signature
const { loading, error, data } = useQuery(getChatHistory);
I assume you are using
apollo-client
? If so,useQuery
hasskip
option, designed for the cases like this.Here is how you can use it to start fetching chat history only after user in a single component:
getChatHistory
won’t start fetching untiluser
is fetched.For other clients:
urql
similar option calledpause
.tanstack-query
hasenabled
.