Tho*_*ude 5 apollo reactjs graphql apollo-client hasura
I\xe2\x80\x99m 使用 React 与 Apollo Client 3 和 Hasura 作为 GraphQL 服务器。
\n该组件ProductList
使用该get_products
查询一次。\n然后该查询的两个精确副本将存储在 Apollo 缓存中,如 Apollo DevTools 中所示。
我的问题是 - 为什么缓存中会生成两个相同的查询而不是一个?
\nApollo 开发工具结果
\n\n我的代码
\nimport {\n ApolloClient,\n ApolloProvider,\n InMemoryCache,\n HttpLink,\n gql,\n useQuery,\n} from "@apollo/client";\n\nconst client = new ApolloClient({\n link: new HttpLink({\n uri: "http://localhost:8080/v1/graphql",\n }),\n cache: new InMemoryCache(),\n});\n\nfunction App() {\n return (\n <div className="App">\n <ApolloProvider client={client}>\n <ProductList />\n </ApolloProvider>\n </div>\n );\n}\n\nconst ProductList = () => {\n const GET_PRODUCTS = gql`\n query get_products {\n product {\n id\n name\n __typename\n }\n }\n `;\n\n const { loading, error, data } = useQuery(GET_PRODUCTS);\n if (loading) return <p>Loading ...</p>;\n if (error) return <p> {error.message}</p>;\n return (\n <>\n <h1>ProductList</h1>\n <ul>\n {data?.product.map((product: any) => {\n return <li key={product.id}>{product.name}</li>;\n })}\n </ul>\n </>\n );\n};\n\nexport default App;\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
1395 次 |
最近记录: |