Live data from Hacker News

REST vs. GraphQL – A search for evidence on which is better

42papers.com

81–90 of 243 posts

Re: REST vs. GraphQL – A search for evidence on which is better

#81

Does this actually pass as an academic study? It is presented in the format of a study (it looks like one) but to suggest GraphQL is better than REST based on the expenditure of effort for building out the first iteration of code for cherry picked scenarios is a first-year undergraduate CS101 study at best. Also, who funded this study? I want to know the actual costs of GraphQL. What are the tradeoffs with REST? What…

For me this is the point that I feel isn't well argued against by GraphQL proponents. Why if I'm a Javascript developer with probably a similar learning curve I couldn't just whip up a simple stateless frontend (in Node since its the same lang) and write the query in SQL? More to the point the skills learnt by doing so are more transferable. You also give your data store a chance to be more performant (e.g. better query plans for a typical DB) using that DSL to query the data store and there's a lot less complexity (components, libraries) in your solution.

Also from what I've seen the approaches that may result in single ideal query plans in your database increase the complexity of GraphQL significantly with "GraphQL to SQL" compilers which still may not give tuned SQL especially with large graphs. Reminds me of using ORM's where often the generated SQL wasn't performant/slow. Even if it does work the complexity of your solution just increased - all for what? To translate one query language into another? There's also times where it may pay NOT to expose too much of your internal domain structure to public API's which I feel from a naive developer's perspective GraphQL could encourage (direct internal API structure to contract mappings).

It feels, at least to me, that it is another abstraction layer that solves a problem that could also be solved at the org level. If there's suddenly a use case that requires a tuned query/algorithm/index as well (happens a lot from my experience) then its easy to add as a separate endpoint and test for regression test against other endpoints/use cases supported.

Re: REST vs. GraphQL – A search for evidence on which is better

#82
post #6

Are there any performance benchmarks comparing REST and GraphQL?

I think they're too different for that to be meaningful. GraphQL lets you do way more in a query, which means you need fewer queries. It would be like comparing PostgreSQL with Redis, or something.

Yes, GraphQL would require fewer round trips than corresponding individual queries via REST, but that logic of mapping a GraphQL query to individual SQL queries from the DB resides in the GraphQL layer.

So, the question is how does that overhead compare to the overhead caused by multiple REST api calls? And, in addition, how do the GraphQL layers scale as the GraphQL queries become complicated and larger.

Re: REST vs. GraphQL – A search for evidence on which is better

#83
post #39

Earlier quoted context omitted.

The solution for this is to use something like dataloader btw. It essentially waits for all these queries (I believe it uses queueMicrotask under the hood) and batches them. Not unlike other db batching proxies.

Wouldn't it be more resource efficient to traverse the graph ahead of time and prefetch all related resources with a single query?

This is more or less what dataloader accomplishes. Generally speaking when creating a gql service, graph traversal is not app code but library code.

Re: REST vs. GraphQL – A search for evidence on which is better

#84
GraphQL has two notable advantages, neither of which are measured here. First by allowing the client to specify precisely what to retrieve the backend isn't catering to a specific client. This allows for decoupling so each team can develop quicker and with fewer changes down the road.

Of course REST can also be decoupled only needing to follow conventions and defining the resource formats. What it doesn't provide is the second benefit of GraphQL which is eliminating round trips for accessing related resources. This is sometimes done by creating an intermediate 'BFF' service on the back end to serve requests as the client would like them but now that's sensitive to both client and REST API changes.

In short, it's not about the development speed of the first client/server. If that were the case use gRPC etc, generate the interface, implement and you're done. It's about the supporting ongoing changes. Perhaps gRPC or similar will become prevalent enough to replace both REST and GraphQL.

Re: REST vs. GraphQL – A search for evidence on which is better

#85
post #83

Earlier quoted context omitted.

Wouldn't it be more resource efficient to traverse the graph ahead of time and prefetch all related resources with a single query?

This is more or less what dataloader accomplishes. Generally speaking when creating a gql service, graph traversal is not app code but library code.

Dataloader avoids the N+1 but I wouldn't call it a good solution, you have to twist your app code to make it work. Its an inelegant solution, imo.

Re: REST vs. GraphQL – A search for evidence on which is better

#86

Earlier quoted context omitted.

My sense is that GraphQL makes the impossible stuff hard and the easy stuff hard. So its relative merits have a lot to do with how complicated a thing you're trying to do in the first place.

What would you say are the hard things about GraphQL?

Keeping database load under control, for example.

With an API that keeps tighter control over access patterns, you've got a more predictable target for optimizing your indexing strategy. With GraphQL, you've got to worry about the possibility that some client figures out how to craft a query that slips between all your indexes and causes the database engine to resort to doing things the hard way. So, worrying about that stuff is hard, where it tends to be easier to manage with REST or gRPC because you can just force your worldview onto consumers and get on with your life.

In theory, though, a well-crafted GraphQL API can be more performant over a wider variety of use cases than a well-crafted REST API, for all the oft-cited reasons. So it does make the impossible possible. But not (necessarily) easy.

Re: REST vs. GraphQL – A search for evidence on which is better

#87

Earlier quoted context omitted.

It has a hypermedia spec: https://www.w3.org/TR/html4/ And a bunch of things called "browsers" that implement that spec. More or less. I mean, come to that, aren't XML and HTML both just text? Isn't text, then, a hypertext? One can embed URLs in text, after all...

Does this mean that your argument that json can't be a hypertext is because it doesn't already have a universal hypermedia spec?

The language here is getting a little squirrly. "Can't be a hypertext" is too restrictive. "Isn't a hypertext" is how I would say it.

JSON as JSON isn't a hypertext in the same way that XML as XML isn't a hypertext. XHTML is a hypertext based on XML. (HTML itself isn't proper XML.)

There are hypertext specs based on JSON, such as: https://json-schema.org/draft-04/json-schema-hypermedia.html

But, without clients that treat the data as a uniform interface, it's all mostly pointless, which is why most JSON APIs stop at Level 2 of the Richardson Maturity Model, and why things like GraphQL are becoming more and more popular.

In the original model of the web (which is what Fielding was describing w/ REST) where HTML was being delivered to a browser, the uniform interface works: a browser has no idea what the HTML document means but can render it to a form that the human looking at it can make sense of.

Re: REST vs. GraphQL – A search for evidence on which is better

#88
post #8

Earlier quoted context omitted.

Very lazy thinking. It doesn’t have types, do introspection, batching, granular caching, access control... I suggest reading the GraphQL introduction post for a start: https://engineering.fb.com/2015/09/14/core-data/graphql-a-da...

These things have overhead and access control is more difficult to reason about and implement with resolvers and nested query resolving.

That may be true, but has no bearing on the fact that being able to add a “fields” param is not by any measure an alternative to what GraphQL provides.

Re: REST vs. GraphQL – A search for evidence on which is better

#89
post #20

Earlier quoted context omitted.

You're not wrong, and to resolve this, I've implemented something of an in-between for APIs I work on, which I call a "Multi-Endpoint Query". It's a restful(-ish) API, but there's an additional endpoint called "query", which allows for an array of endpoints to gather information from sequentially. The most useful bit of this is that information gathered from earlier in the array can be used as referenced later in the…

Given your example request, what would be the schema of the response?

That query is completely contrived (doesn't match anything I'm working on), but here's what the response would look like:

    {
        "products": {
            "abc123": {
                "id": "abc123",
                "name": "Some Widget"
            },
            "def456": {
                "id": "def456",
                "name" "Some Other Widget"
            }
        },
        "images": {
            "xyzabc": {
                "id": "xyzabc",
                "product_id": "abc123"
                "url": "https://example.com/xyzabc"
            }
        },
        "reviews": {
            "4": {
                "id": "4",
                "product_id": "abc123",
                "user_id": "hjk453"
                "review": "This thing sucks!"
            }
            "15": {
                "id": "15",
                "product_id": "abc123"
                "user_id": "pos999"
                "review": "I love this thing!"
            }
        },
        "users": {
            "hjk453": {
                "id": "hjk453",
                "handle": "somebody",
                "name": "Some Body"
            },
            "pos999": {
                "id": "pos999",
                "handle": "someone",
                "name": "Some One"
            }
        }
    }

Re: REST vs. GraphQL – A search for evidence on which is better

#90
post #80
post #20

Earlier quoted context omitted.

You're not wrong, and to resolve this, I've implemented something of an in-between for APIs I work on, which I call a "Multi-Endpoint Query". It's a restful(-ish) API, but there's an additional endpoint called "query", which allows for an array of endpoints to gather information from sequentially. The most useful bit of this is that information gathered from earlier in the array can be used as referenced later in the…

This is an API gateway or backend-for-frontend.

Backend. It's essentially an API gateway, although on my API it's just another endpoint. Behind the scenes it calls the other endpoints.
Post reply on HN