GraphQL 是否具有与 REST 相同的缓存能力 [英] Does GraphQL has the same caching ability as REST

查看:17
本文介绍了GraphQL 是否具有与 REST 相同的缓存能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个请求量很大的 API,现在正在考虑使用 GraphQL、REST 或 REST 和 GraphQL 的组合.我们喜欢前端可以决定返回哪些数据并查看其好处的 GraphQL 方法.但另一方面,由于我们存储的数据类型(产品和产品配置),我们担心缓存.目前我们看到以下选项:

We are building a heavily requested API and now considering using GraphQL, REST or a combination of REST and GraphQL. We like the approach of GraphQL that the frontend can decide, which data is returned and see its benefits. But on the other hand we are worried about caching because of the type of data we are storing (Product and Product Configurations). For the moment we see the following options:

  • 仅使用 GraphQL:允许我们加快前端开发并为我们的 API 提供更大的灵活性以供未来实施.但是通过大量的产品,我们希望使用基本的 http 缓存能力和我们的 CDN.

  • Only using GraphQL: Allows us to speed up our frontend developmentand gives our API more flexibility for future implementation. But through the high amount of products we want to use the basic http caching ability and our CDN.

仅使用 REST:允许我们对每个请求使用标准的 http 缓存,但需要为每个前端请求定义一个端点.

Only using REST: Allows us to use the standard http cache on every request but needs an defined endpoint for every frontend request.

所以基本上我想知道GraphQL的缓存能力是否和REST一样?

So basically I want to know if the caching ability of GraphQL is the same as REST?

作为一个加分项,我们考虑将其合并.这样做的原因是我们有一个后端缓存来缓存来自我们后端系统的数据:

As a plus we thought about combining it. The reason for this is that we have a backend cache which caches data from our backend-systems:

  • 结合两者:我们的想法是在 REST 端点 product/1 后面有一个产品 JSON,它将为产品数据和所有配置提供服务.然后将其保存在我们的后端缓存中.然后,前端开发人员可以使用 GraphQL 来整理特定视图需要的配置部分(例如:product/1?query=SomeGraphQLQuery').因此 REST-Endpoint 用于服务器缓存,GraphQL 用于客户端缓存.
  • Combining both: The idea is to have for example a product JSON behind the REST endpoint product/1 which will serve the product data and all configurations. This is then saved in our backend cache. GraphQL can then be used by the fontend developers to sort out the configuration parts the specific view needs (e.g.: product/1?query=SomeGraphQLQuery'). So the REST-Endpoint is for server-caching and the GraphQL for client-caching.

这种方法在GraphQL"世界中是否有意义,还是只是一个无用的抽象层而没有带来任何改进?

Does this approach make sense in the 'GraphQL' world or is it just an useless abstraction layer and brings no improvement?

推荐答案

GraphQL 的一些实现,比如 Apollo 客户端和服务器,都有缓存插件,我有一个结合 REST 和 graphql 的项目,所以我缓存了 REST 调用在内存中(redis)和 Graphql 查询结果由 apollo 客户端缓存在浏览器中,它工作得很好,但我认为你不需要像我那样结合 REST 和 graphql 因为你的项目是新的,我做了这个因为我没有选择终止 REST API 并在 graphql 中构建所有内容,所以我只将 REST 包装在 graphql 中以获得前端的好处.我的建议是您只继续使用 graphql,并在查询调用和客户端消费者中应用缓存技术.

Some implementations of GraphQL like Apollo client and server have plugins for cache, and i have a project that combine REST and graphql, so i'm caching the REST calls in memory (redis) and Graphql query results is cached in browser by apollo client, it is working great but i think you do not need to combine REST and graphql like i do cause your project is new, i made this cause i do not have choice to kill the REST API and build all in graphql, so i only wrap the REST inside graphql to get the benefits in front-end. My recommendation is to you go on with graphql only and apply cache techniques in query calls and client consumers.

这篇关于GraphQL 是否具有与 REST 相同的缓存能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆