I would like to ask for your guidance, with regards to this GraphQL query. I would like it to check if the query parameter is null and not to fire the query. Currently despite the fact that the query is null, it retrieves all the data, which means that we use some computational power unnecessary.
const search_reviews= gql`
query SearchReviews ($my_query: String) {
reviews (filters: {or: [{body: {contains: $my_query} }, {title: {contains: $my_query}}]}) {
data{
id
attributes{
title
rating
body
categories{
data{
id
attributes
{
name
}
}
}
}
}
}
}
`
And the code from React
const [ first, setFirst ] = useState(false)
const [ query, setQuery] = useState("")
const { loading, error, data } = useQuery(search_reviews, {variables: {my_query: query}})
if (loading) return <p>Loading data...</p>
if (error) return <p>Error fetching data</p>
console.log(data)
CodePudding user response:
You can use skip key of useQuery hook options to achieve this:
const { data, loading, error } = useQuery(SEARCH_REVIEWS, {
variables: /* snip */,
skip: !param,
});
CodePudding user response:
This was the answer:
const { loading, error, data } = useQuery(search_reviews, {skip: !first,variables: {my_query: query}})
