I'm using https://countries.trevorblades.com/ api.
Using React to do query
export const COUNTRIES = gql`
query {
countries {
code
name
}
}
`;
export const COUNTRIES_FILTER = gql`
query ListCountries($filter: String) {
countries(filter: {
code: { eq: $filter }
}) {
code
name
}
}
`;
import { useQuery } from '@apollo/client';
function App() {
const { data } = useQuery(COUNTRIES);
return (
<div className="App">
</div>
);
}
Is there any better apporche then just add if statement to choose querty in
const { data } = useQuery(COUNTRIES);
Like
if some value == true ? COUNTRIES : COUNTRIES_FILTER
I'm new to graphQL, current api do not show any results if passed variable is null.
Please advise.
Instead of using an if statement to conditionally choose which query to use based on a value, you can dynamically define the query based on whether the filter variable is provided or not. You can do this by utilizing GraphQL variables directly within the query itself. Something like this,
You can also consider the option to pass some default value for the filter when it's null