TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Is GraphQL the Next Frontier for Web APIs?

85 点作者 clra大约 8 年前

17 条评论

throwaway2016a大约 8 年前
I&#x27;m a major REST advocate (spoken at conferences, written about it in books, etc) and I&#x27;ve been using GraphQL for a few months now and after an initial learning curve I love the flexibility.<p>But in my opinion, the answer is: write code once, expose both interfaces.<p>Here&#x27;s what I mean...<p>I&#x27;ve been writing my APIs as GraphQL first. Then putting a very light wrapper around GraphQL to expose a REST interface. The REST interface is gold standard (HATEOAS, Json-API, etc) yet it is only a few lines of code. I just have a dictionary of REST -&gt; GraphQL mappings and transform the GraphQL result to Json-API.<p>Then I also expose the GraphQL interface. So users that want to use GraphQL can.<p>It&#x27;s a win-win and it didn&#x27;t take me any more time to develop than just REST. As an added bonus, the GraphQL code is inherently easier to unit test by nature.<p>GraphQL has a lot of flaws (for one it has nothing to do with graphs -- nearest I can tell the name is only because it was first used for Facebooks graph database) and things like pagination and error handling have no standard way to do them. But it is very flexible.
评论 #14004497 未加载
评论 #14004645 未加载
评论 #14007881 未加载
cryptica大约 8 年前
I can see three contenders:<p>1. CRDTs&#x2F;sync solutions (like Firebase)<p>2. GraphQL with real-time subscriptions<p>3. A variant of REST over WebSockets with real-time subscriptions<p>I don&#x27;t think #1 will be able to cover all necessary use cases because it is much more computationally expensive (in terms of memory, CPU and bandwidth). Both #2 and #3 are good solutions but I think #3 will prevail in the end because it&#x27;s a lot simpler.<p>You can break-up RESTful resources into individual fields which each have their own independent real-time subscriptions (so each observable model on the front end can hold a single property of a resource). With GraphQL, you may end up with overlapping fields between different &#x27;views&#x27; and it makes it harder to track&#x2F;manage subscriptions.
评论 #14004848 未加载
评论 #14006634 未加载
gbog大约 8 年前
I don&#x27;t know for web external APIs but for an internal network of services talking to each other I&#x27;ve found GraphQL to be very convenient. The &quot;single entry point&quot; and &quot;describe what you want, you&#x27;ll get what you described&quot; mindset makes it powerful, when used with the graphiql trial and error interface. We barely have to document anything anymore. It seems a bit weird to use a twisted json for the queries but it works well after getting used to it. If someone wants to go this road, I&#x27;d suggest enforcing use of variables in queries (so high level caching works better) and do directly to the connection modelfor pagination.<p><a href="http:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;queries&#x2F;#variables" rel="nofollow">http:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;queries&#x2F;#variables</a> <a href="http:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;pagination&#x2F;" rel="nofollow">http:&#x2F;&#x2F;graphql.org&#x2F;learn&#x2F;pagination&#x2F;</a>
评论 #14004177 未加载
anon1253大约 8 年前
We tried to use GraphQL, but it in our opinion it has a major design flaw: not everything that can be expressed in JSON can be expressed in GraphQL. Since our application makes heavy use of Semantic Web (if you haven&#x27;t already, checkout SPARQL and RDF which made similar promises). But this means we often end up with responses that contain URIs as map keys. This is not allowed as a filter in GraphQL, even though it is valid JSON. Other parts of our stack contain Clojure(Script), and we make use of namespaced keywords that serialize to &quot;foo&#x2F;bar&quot; as map keys. Again something that is not allowed in the language, and would break our application. I find it a particularly odd design choice to not make GraphQL spec match the JSON spec. As there are now many things that are impossible to specify as a query, which would otherwise be valid JSON.
评论 #14004248 未加载
lioeters大约 8 年前
Personally, I found the description of (G)RPC intriguing, since the API that I&#x27;ve been building recently for a project resembles this most closely. I had started by trying to follow REST, then over time in an effort to reduce and simplify network requests, the API became a collection of, basically, remote function calls - organized as a map of &quot;actions&quot; on both client and server. I&#x27;m still pretty much a novice at building APIs, though, so the article was educational for me to compare the pros and cons of various approaches.
awinder大约 8 年前
&quot;Roy Fielding’s original ideas around REST are elegant and now quite widespread&quot;<p>I don&#x27;t think this is actually true. I think you see some ideas plucked from Fielding&#x27;s paper, but describing REST in terms of performing CRUD on resources is not what Fielding&#x27;s paper is largely about. There&#x27;s some good RESTful APIs out there, and Stripe does have a pretty nice API for the current state of API affairs. But I&#x27;d say we&#x27;re still decently early in figuring out how to more widely leverage concepts from Fielding&#x27;s paper, so declaring that REST isn&#x27;t fulfilling needs seems a little short to me. I&#x27;d rather see what happens if more people commit to understanding &amp; implementing other beneficial concepts in their APIs.<p>&quot;REST also has other problems. Resource payloads can be quite large because they return everything instead of just what you need, and in many cases they don’t map well to the kind of information that clients actually want, forcing expensive N + 1 query situations.&quot;<p>So maybe the idea here is that it&#x27;s harder to implement, but you can absolutely structure REST APIs to have these same benefits, and actually you may find it to offer benefits over graphql here. There is nothing un-REST-like about returning different message formats based on the client sending you a specified request header, or providing a url parameter, or what have you. You can actually leverage this to you (&amp; your API client&#x27;s benefit) by providing a small subset of different response &quot;shapes&quot;, or you could just allow them to specify exactly what they need for a request in a free-for-all sort of fashion. It&#x27;s all up to you, but I can&#x27;t buy this argument for GraphQL, it&#x27;s a symptom of writing inflexible REST-ish APIs, not a damning fact of REST as a concept.
brandur大约 8 年前
It&#x27;s probably not super popular to suggest that REST isn&#x27;t the be-all and end-all when it comes to web APIs, but I&#x27;m interested in a future that lets us integrate with APIs more quickly and more safely. I&#x27;m interested to hear about what people think about the subject.<p>(I&#x27;m the author.)
评论 #14004244 未加载
评论 #14004096 未加载
dboreham大约 8 年前
Yes it probably is. Back when I worked on LDAP, we found that people were using the protocol in unexpected ways, basically as a standardized general purpose record&#x2F;field&#x2F;value&#x2F;query access protocol. They were doing this quite strange thing because it appeared to them better than the alternatives which were : use the underlying database access protocol (usually proprietary and undocumented) or roll their own HTTP-based thing, inventing most of the rules and encoding mechanisms themselves (sound familiar??), or use some invented-by-committee thing such as CORBA.<p>GrahpQL I think is meeting a similar need but with the added advantage that it was designed for the task at hand, mostly not by a committee.
评论 #14003630 未加载
otto_ortega大约 8 年前
Does anyone knows if there is a REST variant that allows to send&#x2F;get associated resources nested on a single call?<p>Taking the customers+charges example on the article, something like:<p><pre><code> POST &#x2F;api&#x2F;customers&#x2F;null&#x2F;charges { name: &#x27;John Cash&#x27;, dob: &#x27;1990-01-01&#x27;, charges:[ {type:1, amount: 12.5}, {type:1, amount: 22.8} ] } </code></pre> I think the option to bundle things on a single call is wonderful, but GraphSQL seems much more complicated to implement than REST.
评论 #14007336 未加载
marwanad大约 8 年前
GraphQL was not meant to be a replacement for REST. It doesn&#x27;t seem to right to compare an architectural concept to a query language.<p>The main power of GraphQL is for client developers and lies in the decoupling it provides between the client and server and the ability to fulfill the client needs in a single round trip. This is great for mobile devices with slower networks.<p>Introspection is a really powerful tool and you can build tools around that to do compile time checks and automatically generate class definitions based on your schema.
daxfohl大约 8 年前
REST is horrible. It encourages new developers to think in terms of CRUD, which is also horrible. (<a href="https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;ms978509.aspx" rel="nofollow">https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;ms978509.aspx</a>).<p>RPC is great. It encourages developers to think in terms of functions and leverage more creative ways of getting things done. Event Sourcing, CQRS, etc.<p>GraphQL is really neither here nor there. It&#x27;s more of a convention on top of those layers. I think it&#x27;s a good idea, and a good convention for libraries in different languages to rally around. Though it can be abused too: don&#x27;t use it to replace a fairly simple set of REST-style queries just because you can.<p>Is GraphQL the future? It may be part of it. But, it&#x27;s not everything. It&#x27;s just a query language. Okay there&#x27;s a mutation part of the spec too, and it&#x27;s CRUD-like. I&#x27;d like GraphQL better if there was a more RPC-like mutation.<p>I hope the future includes some more interesting streaming-style protocols. The whole request&#x2F;response thing is pretty boring anymore no matter how you do it.
评论 #14004026 未加载
评论 #14004020 未加载
tantan大约 8 年前
I Kaltura.com we auto generate all client libraries in all different coding languages based on auto-generated API descriptor XML that generated using reflection from the server code. It solves most compilation, maintenance and usability issues for integrators. See our clients generator: <a href="https:&#x2F;&#x2F;github.com&#x2F;kaltura&#x2F;clients-generator" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kaltura&#x2F;clients-generator</a> See few more server side examples: <a href="https:&#x2F;&#x2F;restafar.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;restafar.com&#x2F;</a>
daxfohl大约 8 年前
Funny thing is GraphQL isn&#x27;t a graph query language. It returns lists. I&#x27;m working on a similar concept for a project right now, but that actually queries graphs &#x2F; trees. (Sorry, not open source). As I&#x27;ve been working on this, it&#x27;s struck me as odd how much query technology is built around tables and how hard it is to fit graphs and trees into it with reasonable performance.
评论 #14004161 未加载
JBReefer大约 8 年前
Nah, the next frontier is more likely everyone fully consolidating around REST as it stands today. This looks like movement for movement&#x27;s sake, instead of improving developer ergonomics in an increasingly predictable world.<p>This is like everyone finally agreed on a plug type, and someone starts to push 3 phase. Let&#x27;s get all the houses wired up, instead.
评论 #14003564 未加载
评论 #14003722 未加载
评论 #14003563 未加载
agumonkey大约 8 年前
Clojurescripters liked to leverage GraphQL (david nolen)
评论 #14004765 未加载
jaequery大约 8 年前
rpc looks to be the most flexible of the bunch.<p>what are the downsides of rpc approach as compared to rest and graphql?
daliwali大约 8 年前
By Betteridge&#x27;s Law of Headlines, the answer is &quot;No&quot;.<p>REST describes the architecture of the web, which most websites already conform to. Developers could design web APIs more like web pages, but standard media types must be adopted first. JSON-LD is a strong contender at that.
评论 #14003587 未加载