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.

A REST View of GraphQL

121 pointsby wawhalalmost 5 years ago

20 comments

katetalmost 5 years ago
I&#x27;m resistant to GraphQL, although I take the caveat that I was also initially resistant to JSX and CSS-in-JS and my thinking has since evolved.<p>My two main annoyances are a) GraphQL could be thought of as a custom media type that almost &quot;slots in&quot; to the REST model by defining a request content-type and a response content-type with almost entirely optional fields, and b) the incessant idea that REST has to replicate the n+1 query problem.<p>For a) &quot;it&#x27;s strongly typed&quot; has never struck me as much of an argument. It&#x27;s appealing if you&#x27;re already lacking a strong definition of your data model, but GraphQL doesn&#x27;t inherently fix that, it just forces you to confront something you could fix within your existing architecture anyway.<p>For b), it seems that people tend to map something like &#x2F;users to `SELECT * FROM users`, and 12 months later, complain that it&#x27;s retuning too much data, and they have to make N queries to &#x2F;groups&#x2F;memberOf.<p>Am I alone in thinking the obvious solutions are to i) add a comma-separated, dot-notation `?fields=` param to reduce unnecessary data transfer, ii) add an endpoint&#x2F;content-type switch for `usersWithGroups` and iii) realise this is a problem with your data model, not your API architecture?<p>As an additional c), my other concern is GraphQL touts &quot;one query language for all your data&quot;, but tends to skip over the N+1 problem when _implementing_ the queries to disparate data sources. If your existing queries are bolted into `foreach (input) { do query }` then GraphQl isn&#x27;t going to give you any speed up, it&#x27;s just going to give you slightly more simplicity to an already-slow backend.<p>Granted, I work with &quot;legacy&quot; (i.e. old but functional) code, and I secretly like the idea that adopting GraphQL would force us to fix our query layer, but why can&#x27;t we fix it within the REST model?<p>(I happen to be about to sleep, but I am genuinely interested in waking up to see what HN thinks)
评论 #23769442 未加载
评论 #23769469 未加载
评论 #23770962 未加载
评论 #23770225 未加载
评论 #23769451 未加载
评论 #23771634 未加载
评论 #23770819 未加载
评论 #23760816 未加载
评论 #23769552 未加载
评论 #23769554 未加载
mumblemumblealmost 5 years ago
I&#x27;m becoming somewhat suspicious of <i>both</i> REST and GraphQL for APIs. Not in the sense of thinking that they&#x27;re bad or antipatterns or anything so hyperbolic as that, but in that they make some fundamental assumptions about how APIs are going to work that I don&#x27;t think we should ever just take for granted.<p>On the REST side, it&#x27;s the assumption that the API is for state <i>transfers</i>. Transfer is a heavy word there. You&#x27;re not just assuming that the service is stateful, and you&#x27;re not just assuming that clients can ask for state changes. You&#x27;re assuming that the dominant model for effectuating those state changes is that the client determines what the new state should look like, and transfers a representation of that state to the server.<p>And then, on the GraphQL side, you&#x27;re assuming that the service is basically just a big database. Perhaps one with lots and lots and lots of behavior that, from a high level, looks a lot like triggers. But still, a database.<p>Both these assumptions may work well for a whole lot of applications. If CRUD&#x27;s your ticket, then both can serve you well. But choosing either might force you to make some compromises if you were instead hoping to do come up with, for example, a more domain-driven design.
评论 #23770175 未加载
bedersalmost 5 years ago
One consideration I rarely see mentioned when discussing REST vs. TechDuJour is:<p><pre><code> who are your users? </code></pre> If you develop an API for your front-end team, use the fastest, most efficient protocol. Use something that you can change quickly. Optimize for speed in all regards.<p>If you develop an API for other users and they expect that API to be around for a while and gradually grow, then you need to think about how much pain are you going to subject your users to when your API changes.<p>GraphQL: You will not get your domain model right on the first attempt. So you will need to change your &quot;Graph&quot;. Your &quot;Graph&quot; might change substantially. How do you deal with that?<p>REST: Same thing: There are well-known standards how to grow and evolve a true RESTful API. Yet, you may still mess with your users, declare a &#x2F;v2&#x2F; endpoint and discontinue &#x2F;v1&#x2F;
评论 #23770583 未加载
评论 #23771790 未加载
lpellisalmost 5 years ago
I&#x27;ve used GraphQL and Hasura extensively for my project (<a href="https:&#x2F;&#x2F;pagewatch.dev" rel="nofollow">https:&#x2F;&#x2F;pagewatch.dev</a>) and it has been a huge timesaver to get a realtime service up and running. BUT, I&#x27;m still using REST for mutations (The POST&#x2F;PUT part really). At least for my use case I find that almost every time you are modifying data there is some other component also involved (eg starting a background task), so it might as well be a REST server endpoint. (Also I find the mutations language of GraphQL is really the weakest&#x2F;hardest to really understand part)
评论 #23771058 未加载
评论 #23772324 未加载
recursivedoubtsalmost 5 years ago
Great article.<p>REST with JSON has always been a category error: JSON is not a hypertext and thus, as the author says, violates the most important and distinct aspect of the REST architecture.<p>I have written quite a bit about this:<p><a href="http:&#x2F;&#x2F;intercoolerjs.org&#x2F;2016&#x2F;01&#x2F;18&#x2F;rescuing-rest.html" rel="nofollow">http:&#x2F;&#x2F;intercoolerjs.org&#x2F;2016&#x2F;01&#x2F;18&#x2F;rescuing-rest.html</a><p><a href="http:&#x2F;&#x2F;intercoolerjs.org&#x2F;2016&#x2F;03&#x2F;15&#x2F;hypertext-hypotext.html" rel="nofollow">http:&#x2F;&#x2F;intercoolerjs.org&#x2F;2016&#x2F;03&#x2F;15&#x2F;hypertext-hypotext.html</a><p><a href="https:&#x2F;&#x2F;threadreaderapp.com&#x2F;thread&#x2F;1276556432385531906.html" rel="nofollow">https:&#x2F;&#x2F;threadreaderapp.com&#x2F;thread&#x2F;1276556432385531906.html</a>
coding123almost 5 years ago
My main qualm about REST is the number of inputs for doing something: Path variables, query variables, POST input, headers and for shits and giggles the Http METHOD. To top it off, because people want &quot;beautiful&quot; REST APIs everyone has a playground to do these kinds of things differently. And boy are they done very differently from company to company. And that leads to massive opinionated people. Finally, REST is tied to HTTP too tightly. It can&#x27;t be independent of it, ever. I know a lot of people believe that&#x27;s a good thing, in that there are metrics, logs, and a variety of data that assists architecturally. But it has only reached that status because we&#x27;re sitting at 15 years of REST toolchains.<p>I&#x27;m not saying GraphQL is the long term answer, but personally I&#x27;d rather be in a project using GraphQL (and hopefully not backed by REST at all)
评论 #23771008 未加载
评论 #23771729 未加载
评论 #23771168 未加载
ralmidanialmost 5 years ago
We should resist attempts to turn technology choices into religious devotions. Here&#x27;s an example of a project that brings some of the most significant benefits of GraphQL to an established, mature framework for building REST APIs:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;yezyilomo&#x2F;django-restql" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;yezyilomo&#x2F;django-restql</a><p>It&#x27;s tempting to think your new paradigm&#x2F;framework&#x2F;whatever is so groundbreaking, so purely beneficial, that it warrants asking people to demolish everything and start over. Those kinds of breakthroughs have happened and will continue to happen, but the signal&#x2F;noise ratio, especially in the world of Web Development, is incredibly low.
评论 #23770414 未加载
评论 #23769943 未加载
ksrialmost 5 years ago
I have been working on an idea to leverage SQL + REST APIs instead of GraphQL to solve the similar problems, and I would love to get some feedback on it.<p>GraphQL is great during development, but in production you often use &quot;persisted queries&quot; to prevent abuse. Persisted queries are also a way to use HTTP GET instead of POST, and thereby leverage HTTP caching. As such, if you swap out graphql and use sql during the development phase, you perhaps can get similar benefits.<p>My solution (<a href="https:&#x2F;&#x2F;github.com&#x2F;hashedin&#x2F;squealy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hashedin&#x2F;squealy</a>) uses SQL queries to generate the API response. Squealy uses a template language for the sql queries, so you can directly embed where clause from the API parameters. The library internally binds the parameters, and is free from SQL injection.<p>Handling many-to-many relation, or many one-to-many relations is done in-memory after fetching data from the relational database. This provides the same flexibility as GraphQL, but of course requires you to know SQL.<p>Here is an example that builds various StackOverflow APIs using just a YAML+SQL based DSL - <a href="https:&#x2F;&#x2F;github.com&#x2F;hashedin&#x2F;squealy&#x2F;blob&#x2F;master&#x2F;squealy-app&#x2F;squealy-home&#x2F;dba.se.com.yml" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hashedin&#x2F;squealy&#x2F;blob&#x2F;master&#x2F;squealy-app&#x2F;...</a><p>Squealy is still work in progress, so don&#x27;t use it in production. But I would love to get some feedback!
评论 #23770823 未加载
agustifalmost 5 years ago
Im right now building my backend on GraphQL and TypeScript.<p>Great DX.<p>GraphQL Modules + TypeGraphQL, Apollo Server, TypeORM (pg for prod and sqlite3 for dev&#x2F;test)<p>Works pretty nicely auto-generating both graphql fields&#x2F;querys&#x2F;mutations&#x2F;schema and entities&#x2F;models from database from a single source of turth. I made a template from type-graphql&#x27;s graphql-modules example to work as a separate codebase, if someone is interested I could switch it from private to public and share the link here
评论 #23770222 未加载
评论 #23771149 未加载
0xBE5Aalmost 5 years ago
Thanks for this article! I&#x27;m currently writing my Bachelor thesis, designing a new API for an existing architecture is part of my project. I&#x27;ve been considering going with GraphQL instead of just overhauling the current REST API and this is a pretty good starting point.
stuntalmost 5 years ago
Use Graphql if you have multiple teams that don&#x27;t own their back-end, and you have multiple clients (Web, Mobile, SPA, Desktop...) consuming your data, and your front-end always has to consume multiple APIs and resources, and you don&#x27;t count every milliseconds.
honkycatalmost 5 years ago
I love GraphQL because it is a standard people can agree on, if nothing else.<p>It is a very long list of decisions your team does not have to make. How to handle mutations and queries, how to represent your domain objects, all of this can be decided through referencing the spec and best practice.<p>Throw in the fact that it REQUIRES you to define a strict schema that has validation, and I am sold. The tooling is great, you get great documentation out of the box, the architecture is good enough, it is totally win-win in my book.<p>I feel like with REST you have to reinvent the wheel over and over again, and the lack of standards leaves the burden on the team, who may not be senior level engineers.. ( If I have to write another validation library I am going to flip out. )
aogailialmost 5 years ago
GraphQL == Modern SOAP
gengstrandalmost 5 years ago
Having written <a href="https:&#x2F;&#x2F;glennengstrand.info&#x2F;software&#x2F;architecture&#x2F;microservice&#x2F;graphql" rel="nofollow">https:&#x2F;&#x2F;glennengstrand.info&#x2F;software&#x2F;architecture&#x2F;microservi...</a> which is a blog entitled &quot;GraphQL vs REST&quot; I must admit that I was initially on guard with this blog. In then end, they did compare and contrast GraphQL with REST. I don&#x27;t wish to go over this article point-by-point but their main arguments against REST seems to me to be this...<p>1. too many endpoints<p>2. too much data retrieval<p>3. too much coupling between clients and servers<p>The blog does not discuss when to choose REST over GraphQL since that would be in conflict of interest with those who hold Hasura equity.<p>Technically, GraphQL does decrease the amount of endpoints to one but at the cost of increasing client-side complexity because now client devs have to write what is essentially a query. To do so, they have to understand the backend schema. I would argue that this is not the greatest in terms of clear separation of concerns. I would also argue that this means that there would be even more coupling between client and server.<p>I won&#x27;t cover the n+1 point since others here have already done so.
theonethingalmost 5 years ago
GraphQL with Apollo on the front-end provides very nice front-end global state and cache management, but at the cost of increased complexity. For example, fetching data updates the global state so that any page accessing that data is updated automatically.<p>Are there any lighter weight RESTful JS client state&#x2F;cache management solutions out there people can recommend?
评论 #23772405 未加载
ericlsalmost 5 years ago
I think REST works if you need to fetch data but when you want to query data, I prefer a well defined language instead of hacking my http headers into a query language that I need to define, implement and maintain.
superfreekalmost 5 years ago
Check out <a href="https:&#x2F;&#x2F;open-rpc.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;open-rpc.org&#x2F;</a> if you haven&#x27;t seen it. Brings GraphQL-like tooling to JSON-RPC.
rswailalmost 5 years ago
The primary difference here is that REST is an actual architectural style with constraints.<p>GraphQL isn&#x27;t. It&#x27;s a data query mechanism expressed in JSON.
the_arunalmost 5 years ago
GraphQL makes sense if it is consumed by UI(frontend). But REST still the best for server side consumption. That is one application interacting with another application in the backend. I have seen people interacting with GraphQL for server side as well - but it is over-engineering and increases complexity. IMHO GraphQL is for humans, REST is for machines.
k__almost 5 years ago
I like what AWS built on top od GraphQL.<p>AppSync + Amplify DataStore is pretty nice tech!