News for hackers. Stuff that matters.
Re:API Design Guide by e1g on Wednesday February 22, 2017 @01:45PM (#13707996)
We've been using GraphQL for everything since late 2015. All recent code is GraphQL-first, and all old code is proxied by a GraphQL layer in front of it.

Our application helps BigCos to understand if they pay people fairly and to run smart pay reviews. It's a relatively small codebase, ~100k LOC, but it's essential complexity is in managing and connecting dispersed data about employees and markets. GraphQL allows us to represent the natural links within this data, then the app frontends can present whatever business information is helpful in that page/sidebar/widget/card without separate endpoints.

With REST, we had the same problems with every feature: over/under-fetching, can't express relationships well, and can't evolve the schema easily. When we tried to work around these issues (e.g. "v2?fields=a,b,c"), we ended up with a poorly implemented subsection of GraphQL that's not benefiting from Facebook's experience. To compare to the world of databases, I view REST as a Key-Value protocol and GraphQL as an SQL with joins and functions. If all you need is to lookup a document, don't overcomplicate it. But if you need to express relations, you don't want to do that in userland.

The only advantage of REST is using a widely known standard with rich tooling and well-published "best practices" (that just try to work around REST limitations).

Re:API Design Guide by netghost on Wednesday February 22, 2017 @07:38PM (#13710642)
Do you expose your GraphQL endpoint to customers, or just use it internally?
Re:API Design Guide by e1g on Wednesday February 22, 2017 @09:54PM (#13711253)
We expose GraphQL to two high value customers, but the pains of having a stable public API are so great that I wouldn't recommend it to any startup without a damn good reason.
Re:API Design Guide by ako on Wednesday February 22, 2017 @03:23PM (#13708831)
Any idea how it compares to oData?
Re:API Design Guide by e1g on Wednesday February 22, 2017 @10:02PM (#13711286)
Sorry, no experience with oData so can't give a useful comparison. One obvious difference is that GraphQL requires a structured query, which will always guarantee the structure of the response data. It's like static typing for business data objects. oData seems to embrace REST, so when I request a resource /person/james I have no assurances about what the response will look like. What fields are present? Is "email" a string or an array of strings? What's deprecated? RTFM. GraphQL is explicit so I know exactly what data is used in what views - makes change and debugging that much easier.
Re:API Design Guide by lenkite on Thursday February 23, 2017 @12:44AM (#13711902)
OData requests and responses have schemas, so you definitely have assurances on the field types. You _do_ know that email is a string or array of strings. http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part3...
Re:API Design Guide by daliwali on Wednesday February 22, 2017 @01:56PM (#13708129)
REST describes relationships very well, via hyperlinks. You navigated to this page via a hyperlink. If your API doesn't do that, it's not REST, this is what people usually mean when they point out that an API isn't complying to the REST style.
Re:API Design Guide by e1g on Wednesday February 22, 2017 @03:01PM (#13708671)
REST limits my knowledge only to the primary key of the relationship.

Given a trivial question like "here's a user, I need her friends and their countries of birth" and the REST answer is a separate endpoint or an O(n) operation on the client because these are _separate resources_. You want the country flag too? I'm sorry, I can't support that requirement.

With GraphQL, it's -

    user(handle: "daliwali") { 
      name,
      friends {
         country {
           name, population, flag
         }
      }
    }
No separate endpoint. You want to show to the client how many dogs those friends have, and if any of them play frisbee? No problems, and no backend engineers involved.

This is why I said REST is a key-value protocol like in redis. With redis you can either embed a small objects (which is not a relationship) or keep a PK of the relationships (which means many queries). The more consumers your API has, and the higher the latency, the more expensive either of those choices becomes.

Re:API Design Guide by daliwali on Wednesday February 22, 2017 @03:15PM (#13708776)
You are correct that in a REST system, every resource has a "primary key", that is the URL.

Where you are wrong is that REST doesn't mandate that a resource can only be accessed by its "primary key". There is nothing stopping me from requesting the following URL:

GET /users/daliwali?fields=name&include=friends,friends.country

Any client would be able to follow that link and get something out of it, whether it's JSON or HTML, without needing specialized tooling such as a GraphQL client. A hyperlink makes that query widely accessible and interoperable with any HTTP client.

Re:API Design Guide by fixermark on Wednesday February 22, 2017 @03:32PM (#13708897)
We're reasoning about a simple case, however. GraphQL is recursive, and HTTP parameters are generally not. You can use them to wrap a recursive language, but at that point you're just using the URL as a transport layer for the recursive language, and if the language is expressive enough it won't fit in a GETtable URL anyway... Past a threshold of complexity, you get into wanting that GraphQL client.
Re:API Design Guide by daliwali on Wednesday February 22, 2017 @03:46PM (#13708985)
In reply to fixermark, there is also nothing stopping you from very complicated queries in REST. There is not even a requirement that the server must respond immediately. For example I could request:

POST /queries

With some raw database query as the payload (please don't actually do this) and the server could respond with HTTP 202 Accepted, meaning that it's going to take some time to process, meanwhile check back at the URL in the Location header when it's finished.

REST does not mandate any upper bound on complexity, that's up to you to decide.

Re:API Design Guide by dobs on Wednesday February 22, 2017 @06:57PM (#13710414)
> In reply to fixermark, there is also nothing stopping you from very complicated queries in REST.

What I believe others are getting at is that those complicated queries themselves need to be expressed somehow even within a REST request. GraphQL provides a convenient mechanism for doing so.

In theory you can even do GraphQL RESTfully, with each REST endpoint exposing its own schema. But in doing so it quickly becomes apparent that it would be easier just to forego REST conventions and expose a single consolidated schema.

Speaking for my own GraphQL experiences, on one project we had a GraphQL query generating reports that was around 50 lines long, with half a dozen fragments. Flattened out to match the approaches you've suggested for POST data and GET params it would have been hundreds of attributes. It's unclear that there's any open standard that would have provided as simple of a solution for expressing and executing those requests.

Re:API Design Guide by squeaky-clean on Wednesday February 22, 2017 @04:29PM (#13709323)
But at this point, it's so different and you've added so much work to it, I doubt anyone would recognize it as originally REST. You're basically rewriting GraphQL yourself. And sure, you're allowed to do that, but why?
Re:API Design Guide by daliwali on Wednesday February 22, 2017 @04:55PM (#13709553)
>it's so different and you've added so much work to it

With web pages, it's standard and effortlessly handled by the browser. Given a form with inputs specified by the server, a client sends a request with media type application/x-www-form-urlencoded or multipart/form-data. There is a vast number of implementations for practically every language and platform. A machine client can send a form too, or JSON for that matter.

You have it backwards, Facebook is attempting to rewrite web standards by themselves. And that is undermining the open web.

Re:API Design Guide by jauco on Wednesday February 22, 2017 @04:39PM (#13709409)
that's almost exactly what a graphql get looks like

    http://myapi/graphql?query={me{name}}
http://graphql.org/learn/serving-over-http/#get-request
Re:API Design Guide by dragonwriter on Wednesday February 22, 2017 @07:52PM (#13710709)
> Given a trivial question like "here's a user, I need her friends and their countries of birth" and the REST answer is a separate endpoint or an O(n) operation on the client because these are _separate resources_.

Actually, I think that the answer following the REST architectural style, using HTTP, and not resorting to a custom HTTP method (which, actually, consistent with the REST architectural style would arguably be justified in this case), the correct approach would be to POST a representation of a resource representing the query to an appropriate query endpoint (which could be the user endpoint for queries specific to that user, since the query could reasonably be seen as a subordinate entity to the user to which it applied), and then GET the result of that query.

(In fact, the given GraphQL could well be the representation of the query.)

Ideally, HTTP needs a generalized safe method (like GET) that takes a payload (like POST) named something like SEARCH (there are HTTP-based technologies with domain-specific versions of this, but no generally accepted representation-neutral version) so that this can be, when the server architecture supports synchronous exchanges, a one-round-trip process.

> This is why I said REST is a key-value protocol like in redis.

But this is simply false. Its trivial to make key-value protocols that follow REST (especially the REST-minus-HATEOAS that is common these days), but REST isn't limited to that, or even specialized to it.

Re:API Design Guide by justinsaccount on Wednesday February 22, 2017 @02:37PM (#13708495)
REST describes relationships just fine. Now return a list of 100 documents that each have a list of related comments. Your client just needs to request

  GET /documents
  GET /documents/1/comments
  GET /documents/2/comments
  GET /documents/3/comments
  GET /documents/4/comments
  GET /documents/5/comments
  ..
  GET /documents/99/comments
  GET /documents/100/comments

Easy, right?
Re:API Design Guide by daliwali on Wednesday February 22, 2017 @02:55PM (#13708638)
Easy mode: GET /documents?include=comments

Think of how this can be done on a web page. The documents page has a link to follow, "include comments", which links to "?include=comments". The exact query doesn't matter, what's important is that a client can discover new information without needing any out-of-band information.

Re:API Design Guide by e1g on Wednesday February 22, 2017 @03:07PM (#13708725)
That's where we started.

The next natural step will be - "Thanks for the comments, now the user needs to sort them by votes/date, ok?" and the URL starts looking like a query anyway, so you're on the way of re-implementing GraphQL. Then do you send avatars/timestamps/changeflags for all comments, or only on main body in this endpoint? Now you have the over/under-fetching problems.

The step after that will be "Oh there are 300 of them - I just want the top 3 comments first, and a pager for the rest ok?" and you simply can't do that with the structure REST mandates.

Re:API Design Guide by daliwali on Wednesday February 22, 2017 @03:24PM (#13708837)
>the structure REST mandates.

There is no structure that REST mandates, if there were it would be a specification and not an architectural style. There is nothing that says that you can't query for that:

GET /comments?sort=votes&limit=3

The exact string doesn't matter, what matters is the client can discover this. How HTML does this is by generating query parameters based on form inputs.