TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

GraphQL vs. REST APIs: a complete guide

106 pointsby makaimcabout 2 years ago

21 comments

jensneuseabout 2 years ago
Here&#x27;s how you do it:<p>- REST&#x2F;Hypermedia when you&#x27;re actually building a website (not app) and your &quot;site&quot; is a state machine<p>- OpenAPI when you expose and API to 3rd parties<p>- Isomorphic TypeScript APIs when you&#x27;re using TS on both backend and frontend [0]<p>- GraphQL when the frontend needs to talk to an API layer provided by different teams<p>- OpenAPI when one backend talks to another backend<p>- gRPC might be an option, but most teams don&#x27;t need it<p>[0]: <a href="https:&#x2F;&#x2F;wundergraph.com&#x2F;blog&#x2F;isomorphic_typescript_apis_end_to_end_type_safety_between_client_and_server" rel="nofollow">https:&#x2F;&#x2F;wundergraph.com&#x2F;blog&#x2F;isomorphic_typescript_apis_end_...</a>
评论 #35015777 未加载
评论 #35015540 未加载
评论 #35015894 未加载
评论 #35015881 未加载
评论 #35015559 未加载
评论 #35015259 未加载
hbrnabout 2 years ago
I really hate these bland, barely actionable advices with no examples, created for SEO purposes.<p>Nobody knows how to measure the &quot;complexity&quot; and &quot;evolvability&quot; of the API and where exactly is the threshold that justifies going from REST to GraphQL.<p>So here&#x27;s my opinionated alternative:<p>Use REST everywhere, unless you can justify the exception.<p>A good exception is when you don&#x27;t know the audience and use cases for your API. E.g. if you have hundreds of different apps using the same API and you have no idea <i>how</i> they are going to use it. You know who does it? Facebook.<p>If you&#x27;re just dogfooding an API for your website, you don&#x27;t need GraphQL. If you need one service to talk to another, you don&#x27;t need GraphQL.
评论 #35020749 未加载
gtramontabout 2 years ago
I take not mentioning hypermedia in a post discussing REST as a huge red flag. The author mentions Roy Fielding&#x27;s dissertation, but apparenly they didn&#x27;t read it thoroughly.<p>Furthermore, I believe that a huge confusion comes from not understanding what a &quot;resource&quot; is: it is not a database table*; it is not your domain model*; it is a _representation of state_. Like this very orange page you&#x27;re looking at. This whole page is the resource identified by <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35014395" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35014395</a>. Just like the stylesheet for this website is a resource identified by <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;news.css" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;news.css</a>.<p>* It may be, if that&#x27;s the _representation_ you want.
评论 #35016341 未加载
评论 #35016322 未加载
评论 #35016088 未加载
twodaveabout 2 years ago
I like GraphQL because it allows me to describe my API like a domain rather than just a collection of resources. REST is difficult to map to a true domain model because domains aren&#x27;t just collections of resources. They have nuance. And with GraphQL your API matches your domain, so there&#x27;s no limit to what your application can accomplish. For example, adding another feature to your front-end no longer requires going back to your API to add a tailored endpoint nearly as often. At worst you&#x27;re adding a property somewhere that is now available to all your use cases. GraphQL embraces mutations in a way that REST frankly can&#x27;t without being glorified RPC, and it doesn&#x27;t have the same perverse incentives such as re-using an endpoint because it exists even though it may be suboptimal.<p>So, in my opinion, unless your application is very resource&#x2F;entity-intensive (and you don&#x27;t mind a little RPC-style bloat) then GraphQL is often a better choice than REST. And if you actually have a rich domain to model (this is the case even for many small applications), then GraphQL is often a better choice than RPC.
评论 #35016264 未加载
评论 #35015554 未加载
b212about 2 years ago
I had the pleasure of working with Apollo + GraphQL for the last 2 years and I don’t understand the hype. I haven’t seen anything so over engineered and complex in my whole 20 year long career. To make things worse I quickly found out all the problems GraphQL tries to solve are still there. Like over-fetching is around the corner if you don’t pay attention.<p>REST on the other hand feels really straightforward, maybe easier to mess up but also much easier to fix. I’d take Redux over Apollo anytime even though Redux is dying according to many.
coolgooseabout 2 years ago
Well, seems somebody doesn&#x27;t care about jsonapi&#x2F;openapi and the rest of the family.<p>Sparse field sets &#x2F; compound documents are nothing new and I&#x27;d argue its easy to overlook that there&#x27;s no magic in the backend for graphql either.
评论 #35015716 未加载
blagieabout 2 years ago
I don&#x27;t like GraphQL due to the complexity. It&#x27;s not JSON or YAML, and requires a lot of custom stuff on both ends.<p>I&#x27;d like something like GraphQL, but built on standard data formats.
评论 #35032340 未加载
评论 #35018411 未加载
satvikpendemabout 2 years ago
GraphQL is extremely useful, if only to have strong type validation between disparate languages, via code generation. It can be a universal typed schema of your data against which you can use whatever server and client combo you want. It&#x27;s like tRPC but you&#x27;re not forced to use only TypeScript.<p>I also have to plug this video [0], since many people think GraphQL somehow is a competitor to SQL and that it goes between the server and the database; it does not and has not ever been intended to be a replacement for a database query language and in fact should never be used there at all. It is only for client&#x2F;server communication, not server&#x2F;database communication.<p>[0] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=ZfccwYUD8H0">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=ZfccwYUD8H0</a>
评论 #35032358 未加载
WirelessGigabitabout 2 years ago
I don&#x27;t like that GraphQL is called GraphQL.<p>Its output cannot represent a graph. Its output is finite.<p>Consider an API that queries people. You query a married person and include their spouse. And then you include the spouse&#x27;s spouse, and so on.<p>GraphQL will render as many levels as you request without deduplication.<p>JSONAPI will do this much better, you&#x27;ll get 2 entities in &#x27;data&#x27;, and they&#x27;ll refer to each other in their relationships block.<p>Not that I like JSONAPI. Having built larger system with it there is no universal way of supporting pagination in the includes section which makes it a binary proposition about includes. You get ALL the ones you request, or none.
评论 #35015809 未加载
mostertoasterabout 2 years ago
The main issue that happens with both REST and GraphQL, is that people spend more time thinking of how to implement their thing, than what the API should be.<p>The complexity GraphQL adds _can_ be worth it for its ability to better represent a business domain via an API. If you don’t care how well your API represents your business domain, then likely your API is going to suck, and you might as well have a sucky REST api and avoid the complexities of GQL.
chrsjxnabout 2 years ago
The article talks about how GraphQL is &quot;flexible&quot; a lot, but I kind of wish they&#x27;d spend more words talking about why that matters.<p>Eg, if you&#x27;re working on a new product, the odds are very good that your front ends will change pretty rapidly. At the very least, we&#x27;re way more likely to A&#x2F;B test application features than we are API schemas, regardless of what methodology or technology our APIs are using.<p>Because that&#x27;s why I would pick GraphQL. It&#x27;s a choice that reduces the barrier to rapid iteration in the clients, but it does add backend and infra work to support it.
评论 #35020762 未加载
bedersabout 2 years ago
Nowhere near a complete guide or even balanced comparison<p>&gt; GraphQL is a highly flexible option that’s better suited for building APIs that require complex data fetching and manipulation. It deftly avoids over-fetching and under-fetching data, leading to lower bandwidth usage and faster response times.<p>Yes, but have you tried implementing one? It is not trivial at all to write performance GraphQL backends.<p>Often, it&#x27;s a n+1 query you didn&#x27;t write a batching version for that messes you up.
ishjohabout 2 years ago
I have used GraphQL and REST extensively.<p>I prefer REST. It is easier to cache server side which for certain types of data can be extremely useful. It works well with monitoring software. It is extremely easy to understand with just using curl.<p>Graphql works extremely well with React. The Graphql Apollo React client fits like a glove and is a pleasure to work with. I have not found a REST library that fits as well although there are likely some now.
ElijahLynnabout 2 years ago
Article could be improved by mentioning that the smaller rest requests will have a much higher cache hit ratio when used with HTTP&#x2F;2.
评论 #35017284 未加载
nonethewiserabout 2 years ago
The anti GQL circle jerk goes too far. GraphQL is often overkill but it’s awesome if you take advantage of the powers of federation and field specification. If you don’t see a difference between retrieving all fields and just the ones you need then you just haven’t encountered the cases where it makes a difference.
doctobogganabout 2 years ago
I&#x27;ve been writing flask apps for a while (using sqlalchemy as my ORM) and find myself repeating code all the time when I create GET and POST end points for all my models.<p>Is there a better, more automatic way to generate GET and POST endpoints automatically when I create a new model?
denysoniqueabout 2 years ago
For JavaScript based projects TeleFunc[1] can make development way simpler by &#x27;removing&#x27; the need for an API.<p>[1]: <a href="https:&#x2F;&#x2F;telefunc.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;telefunc.com&#x2F;</a>
评论 #35020796 未加载
IceDaneabout 2 years ago
There seem to be a lot of people pushing back on GraphQL who often don&#x27;t seem to understand what it really is, or what it can do. They often seem to think that it&#x27;s super complicated to build and use, and that REST(-ish) APIs are somehow magically simpler and easier.<p>Are RESTish APIs simple to build? I mean, yeah. You can have a server running serving an endpoint in like a minute in most programming languages. But FWIW, this also applies to GQL. Take a gander at any GQL resolver tutorial. But is that really what makes it simple?<p>When you build APIs, they aren&#x27;t made to exist in isolation. They&#x27;re made to be consumed. Ideally, they should be easy to consume, which can mean a lot of things.. but &quot;it&#x27;s easy to send an HTTP request&quot; is not one of those things.<p>They&#x27;re easy to consume if they are documented and come with schemas. They are easy to consume if there is tooling to generate fully typed clients for them. Arguably, OpenAPI schemas could fit this description, but in my experience, OAPI support is really varied, both on the server and for clients. Generated clients tend to be <i>lots</i> of code and incredibly clunky. Support server-side is heavily reliant on the server libraries being capable of exposing schemas in some way. In some languages this is trivial, but in e.g. javascript or typescript, you will need some way to expose your types. Just having typescript interfaces isn&#x27;t enough(without extra tooling or reflection).<p>GraphQL is typed by definition. The schema <i>is</i> the API, and the types, and the documentation. We have great tooling to generate code in any language, and code-first resolver libraries(like the excellent pothos-graphql.dev) make building your schema along with your resolvers an absolute joy.<p>Then there&#x27;s things like the N+1 problem. RESTish APIs are sort of &quot;procedural&quot;. Each endpoint is usually a resource, and if you want to fetch a resource and its related resources, you will often have to fetch it and them separately. You can also embed them in the resource, or you can resort to all sorts of not-very-standardized methods of conditionally expanding relationships to do it, which is already just sort of doing what GQL does, except much worse(and is I think not supported in OpenAPI at all?). In GQL, you can also run into N+1, but modern libraries make this super easy to solve. Either they can tightly integrate with something like the underlying ORM, or you can write dataloaders for your <i>types</i>, so that every time a specific resource is requested en-masse, it is always loaded efficiently. Not so much for REST, unless you apply very careful engineering and design.<p>GraphQL just makes all these things such a breeze. There are some things you need to think about, of course. Query complexity is one thing; you don&#x27;t want someone to bring down your server using 30-level nested query. Thankfully, people have been using GQL for years at this point and most of these problems are solved, and many of them incredibly neatly.
braindead_inabout 2 years ago
REST is basically a subset of GraphQL.
评论 #35015512 未加载
revskillabout 2 years ago
Just like OOP vs FP. REST is OOP (you start with resource), Graphql deals with function (query and mutation).
评论 #35014952 未加载
评论 #35015063 未加载
recursivedoubtsabout 2 years ago
imma let u finish, but a hypermedia is a necessary constraint for an API to be RESTful...<p>edit: more usefully, you need to be really careful w&#x2F; your GraphQL end points because they are being access in an untrusted computing environment:<p><a href="https:&#x2F;&#x2F;intercoolerjs.org&#x2F;2016&#x2F;02&#x2F;17&#x2F;api-churn-vs-security.html" rel="nofollow">https:&#x2F;&#x2F;intercoolerjs.org&#x2F;2016&#x2F;02&#x2F;17&#x2F;api-churn-vs-security.h...</a><p>my understanding is that facebook whitelists their graphql queries but, last I looked, I didn&#x27;t see security getting much attention around it...