I’m using React with Apollo Client 3 and Hasura as a GraphQL server.
The component ProductList
use the get_products
query once.
Then two exact copies of this query are memorized in the Apollo Cache as shown in the Apollo DevTools.
My question is - Why two identical queries get generated in the cache instead of one?
Apollo DevTools results
My code
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
HttpLink,
gql,
useQuery,
} from "@apollo/client";
const client = new ApolloClient({
link: new HttpLink({
uri: "http://localhost:8080/v1/graphql",
}),
cache: new InMemoryCache(),
});
function App() {
return (
<div className="App">
<ApolloProvider client={client}>
<ProductList />
</ApolloProvider>
</div>
);
}
const ProductList = () => {
const GET_PRODUCTS = gql`
query get_products {
product {
id
name
__typename
}
}
`;
const { loading, error, data } = useQuery(GET_PRODUCTS);
if (loading) return <p>Loading ...</p>;
if (error) return <p> {error.message}</p>;
return (
<>
<h1>ProductList</h1>
<ul>
{data?.product.map((product: any) => {
return <li key={product.id}>{product.name}</li>;
})}
</ul>
</>
);
};
export default App;