Live data from Hacker News

Ask HN: Were you happy moving your API from REST to GraphQL?

news.ycombinator.com

141–150 of 182 posts

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#141
Eh... I'm torn on it and could write at length about this, but here's my off-the-cuff thoughts. For context, my co-founder and I are quite active in the GraphQL community. If you've ever used apollo-cache-persist[1] or graphql-crunch[2], we authored those libraries. Our startup, if you're curious, is a social podcasting app written in react-native (https://banter.fm). We learned a lot of GraphQL lessons building it.

The tl;dr; is that graphql gives you a lot of flexibility and typed schemas are nice, but it doesn't come for free and I miss the tooling around http.

Pros:

- Typed schema

- Custom queries retrieve all the data the client needs in one request

- Really easy to implement, both server-side and client-side

- Server-side, it's trivial to have any field resolved in any way you want (redis, memcache, postgres, some random service)

- Easy and arbitrary mutations. There's no pontificating over what verbs are the proper ones to us.

- If you use react, the community and ecosystem often assumes you're using GraphQL, so it may make sense to use graphql just so you don't swim against the current.

Cons:

- The payloads can quickly become huge because there is often a ton of duplication in a responses (depending on your query patterns). See this example on the SWAPI demo: https://bit.ly/2uOFZBP. The result is 1MB of JSON, ~97% of which is data that exists somewhere else in the response already.

- Refactoring types is often impossible to do in a backwards-compatible way, even if the shape of the data is the same.

- You don't know what data you'll need in advance, so you're basically doing all of your joins by going back and forth between the api resolver and your data sources (this can be alleviated with persistent queries, but those come with their own set of issues). A typical query to hydrate a response for a user's feed in our app requests ~1,100 objects. After caching and consolidating queries into multi-gets, it translates to about 50 distinct DB queries.

- Tooling: Working at the HTTP level simply has better tooling and tons of infrastructure around caching and serving content (varnish, nginx, etc...)

We found that graphql payloads were so large that older mobile phones were spending significant time parsing them. We created graphql-crunch to de-duplicate responses before sending them over the wire. This led to nice perf improvements on mobile platforms. It also gave us referential equality when persisting the results to cache, allowing us to reduce a lot of work client side.

If you're going to use GraphQL, embrace the javascript ecosystem. Also use Apollo[3]. Also use DataLoader[4]. Roughly 40% of our queries get resolved for "free" by data loader.

If I were to do it again, I'd at least prototype a REST-api with resources designed specifically for http cache-ability (that is, break out session-specific resources/attributes vs shared resources) and see if HTTP/2 multiplexing + nginx caching + etags results in a good client experience. But I also mostly work on the backend while my co-founder mostly works on the frontend, so we have different desires and constraints. Ideally, as few requests as possible would make it to code that I wrote. With GraphQL that's nearly impossible.

[1] https://blog.apollographql.com/announcing-apollo-cache-persi... [2] https://github.com/banterfm/graphql-crunch [3] https://www.apollographql.com/server [4] https://github.com/facebook/dataloader

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#142
post #135

Earlier quoted context omitted.

He (the hypothetical he) doesn't want to talk to them to get them to add the endpoint he needs, so he'd prefer to just get the schema directly and query for whatever.

Note that this means the back end team has to keep the schema backwards compatible forever. Having everyone have direct access to the schema (speaking from SQL experience) is bad. This is the start of a slow moving disaster train that you can't stop.

I'm not quite sure what does the database schemas have to do with the front-end queries...

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#143
These are taken from some of the other comments in this thread:

    * As a consumer of APIs I vastly prefer REST APIs.
    * Highly recommend GraphQL to anyone.
    * I *love* how flexible it is for client developers.
    * As a manager/business owner, I like GraphQL.
    * How I _wish_ I could nope the hell out of our GraphQL dependency
    * Unnecessary complexity over simple rest calls with no benefits.
    * REST semantics are a distraction
    * my experience is just ok.
What I see here is many different reactions from different people, presumably with different experiences and use cases on their hands. And that's perfectly OK -- REST, GraphQL, XML-RPC, heck even SOAP are just tools, and use whichever works best for your particular situation.

Just because it worked for me doesn't mean it will work great for everyone else; and if it sucks for me doesn't mean it won't be a life-saver for someone else. Those are just tools; use them as you find fit.

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#144
post #99

Earlier quoted context omitted.

> the client dictates its own requirements, and the server is able to respond with no more and no less than what the client requires. The burden of dictating specs is just shifted around, and it seems the workload on the server side is bigger with h GraphQL (wider range of cases to handle). Am I missing something ? > REST necessitates multiple queries This is a self imposed limitation at best

Your graphql library will handle that for you. The server defines data as it knows, the client asks for data that it wants, and the library does the transform work.

What will the library handle ? From my understanding it’s only the query parsing and the data filtering part. Correct me if I’m wrong.

You still have to do the data mapping, fetching everything from DB in a reasonable way, and make sure it all makes sense performance wise.

All of that will be needed for any API, but it seems to me GraphQL adds the uncertainty on how much data will be exchanged (lots of small queries ? a few big queries ?), what can be optimized, how will cache behave etc.

I remember a study on te github API on how some types of queries would make it crawl excessively. It fear it becomes a nightmare to try to cover for all the cases that can go wrong.

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#145

Earlier quoted context omitted.

This is my experience as well. We briefly worked with GraphQL for one of our smaller projects. One of the biggest shortcomings of the API we created was that there was an incredibly large learning curve for teams/people not interested in using GraphQL. Instead of using the sensible features of GraphQL queries, the other team consuming our API were writing the most verbose queries they possibly could, not utilizing an…

That's interesting, because as a frontend engineer, I find most RESTful APIs to introduce more "needless hassle", mostly in the form of making me send multiple requests to get data multiple levels deep and then consolidating it on the frontend, rather than just sending me what I want to begin with. I agree with query building being tedious, though I'm working on a query builder helper library to make this less painfu…

[deleted]

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#146
post #82

You're going to get responses from people who have invested a considerable amount of time in something they already had plans for (a "sunk cost") so I'm not sure you will get the sort of information you are after here.

I'll be the voice of someone who is actively implementing GraphQL for the first time. We were mid-way through our project before realizing GraphQL might be a better fit for our use case so we paused for a week to play around with it and see if we could stand up something inside of our project that made sense. GraphQL felt very "plug and play" to us. Aside from having to re-work some validation to fit into GraphQL's i…

What about caching though?

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#147
post #95
post #63

I'd like to add that correctly designed resolvers allow you: - to control very easily who can fetch what where it's fetched (permissions) - to fetch nested data when you need it without writing serializers - to help your frontend team find what they are looking for without asking the backend team everytime - mutations are a huge plus when it comes to standardization of your API too FYI we're using it in production ov…

>to control very easily who can fetch what where it's fetched (permissions) This is a piece of GraphQL I haven't been able to get my head around. Could you elaborate or point me to a good explanation of how this is implemented? Everything I found when I looked into GraphQL previously was something like "you control access to individual resources in your business layer" but never explained how .

In order for GraphQL to return a result when you ask for field “foo”, you need to define a resolver function for that field. Whatever that function returns will be returned to the client.

Inside that function you can write any code you’d like, including permissions code.

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#148
post #82

Earlier quoted context omitted.

I'll be the voice of someone who is actively implementing GraphQL for the first time. We were mid-way through our project before realizing GraphQL might be a better fit for our use case so we paused for a week to play around with it and see if we could stand up something inside of our project that made sense. GraphQL felt very "plug and play" to us. Aside from having to re-work some validation to fit into GraphQL's i…

What about caching though?

GraphQL has magic record-level caching that I do not understand but somehow appears to sometimes work and sometimes doesn't (but using the default apollo-client, you can't easily turn it off)

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#149

Almost all developers/team who actually tried out and used GraphQL, will say the same thing: You never want to go back. Most of the arguments against GraphQL that I'm reading here, seem to actually be misconceptions (or the GraphQL ecosystem not being far enough yet).

I disagree. Some points are really valid. It adds complexity to your system, especially at the server. It is a costs/benefits thing, not only benefits. The benefits are minimal for simple CRUD-like applications.

Re: Ask HN: Were you happy moving your API from REST to GraphQL?

#150
post #135

Earlier quoted context omitted.

Note that this means the back end team has to keep the schema backwards compatible forever. Having everyone have direct access to the schema (speaking from SQL experience) is bad. This is the start of a slow moving disaster train that you can't stop.

I'm not quite sure what does the database schemas have to do with the front-end queries...

In this case OP is talking about the GraphQL schemas, not the DB.
Post reply on HN