Question about using GraphQL and REST

When building an application, should you stick to either GraphQL or REST or is it okay to use both together? When would you use both together or should you just stick to one or the other? What are the pros and cons of each option? Thanks.
3 Replies
ἔρως
ἔρως2mo ago
if rest is good enough, i stick with that because it's a lot easier if the rest api sucks, and there's a graphql version available, then i will dable on it and maybe use it
Martin Bavio
Martin Bavio2mo ago
This article pretty much covers most of your questions. In my personal experience, the only strong use case for GraphQL is when you have multiple API consumers, where each can tailor their requests to the data they need. The schema introspection feature of GraphQL is also pretty cool.
Solomon Eseme
Kinsta®
GraphQL vs REST: Everything You Need To Know
Not sure how GraphQL and REST differ? We explain everything you need to know, from how they each work to which you should be using.
13eck
13eck2mo ago
Best practice is to use ReST if you can. GraphQL is, as the name implies, a query language. You send your query to one endpoint, it executes it, and sends the data back to you. It's like opening up your SQL database and asking your users to send you a SQL query and sending back the result. ReST, instead, is a simple, "gimme X" request. There's nothing special or magical about it, it's a simple request for data. This means that GraphQL is more complex to run as it's running dynamic queries with each request. Makes it difficult to cache results, and you have to super-sanitize the user queries. ReST, on the other hand, is a much simplier request/response cycle. Requests to the same endpoint will (almost) always return the same data—making it more cache friendly. Of course, the nice thing about GraphQL is that you can pass complex queries to it and get all the data back in one chunk. With a ReST API you'd need to either make multiple requests to multiple endpoints or make one "uber-endpoint" that returns multiple data. But at that point it's GraphQL with more steps lol. Big businesses that requires large and custom data returned can benefit from a GraphQL endpoint so they can customize their requests and get everything all at once. But that's more of a large-scale company need and not something you'll have to worry about as a solo dev or even if you're on a small team. At the end of the day, GraphQL is more complex to build, run, and maintain but allows for more granularity and specificity of content returned. ReST is more generic, but much easier to build and maintain. Which is best depends on your use case and how much work you're willing to put in to maintain and update your API. TL; DR: Start with ReST. If you get to the point where you're needing to either make 3+ requests to get the data you want or feel like you need to add a "combo" endpoint then you can move on to GraphQL.

Did you find this page helpful?