Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does GraphQL has the same caching ability as REST

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:

  • 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.

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

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:

  • 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.

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

like image 666
Jan Hommes Avatar asked Jan 09 '17 17:01

Jan Hommes


People also ask

Does GraphQL do caching?

Now, caching in GraphQL can be done server-side. This means that the GraphQL server can be configured to fetch the response from its cache in the server without running the resolvers.

Does GraphQL support server-side caching like REST?

At this point, we know that GraphQL can actually be a thin layer over our existing servers, and that in no way GraphQL prevents us to cache on the server-side, sometimes referred as Application Caching.

Does GraphQL server cache data?

GraphQL Clients cache data with an in-memory cache. Control the number of times a resource should be considered fresh with response headers like Cache-Control.

How does GraphQL differ from REST?

GraphQL is an application layer server-side technology that is used for executing queries with existing data while REST is a software architectural style that defines a set of constraints for creating Web services. GraphQL can be organized in terms of a schema whereas REST can be arranged in terms of endpoints.


1 Answers

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.

like image 192
renatomattos2912 Avatar answered Oct 01 '22 07:10

renatomattos2912