【发布时间】:2022-03-01 01:33:14
【问题描述】:
我将 React 与 Apollo Client 3 和 Hasura 一起用作 GraphQL 服务器。
组件ProductList 使用一次get_products 查询。
然后该查询的两个精确副本被存储在 Apollo 缓存中,如 Apollo DevTools 中所示。
我的问题是 - 为什么在缓存中生成两个相同的查询而不是一个?
Apollo DevTools 结果
我的代码
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;
【问题讨论】:
标签: reactjs graphql apollo apollo-client hasura