News for hackers. Stuff that matters.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by EduardoBautista on Saturday January 12, 2019 @07:57PM (#18894296)
As primarily an Ember.js user (with an interest in Elm), just looking at how many hoops people in the React ecosystem go through just to send a request just boggles the mind.

Maybe I am in the minority, but writing boilerplate code all the time just isn't something that interests me.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @08:13PM (#18894368)
I write React every day and don't know what you're referring to – you can just call fetch() in a lifecycle hook and be done with it, just like in any other framework.

But mostly I'm curious how your point relates to this blog post. Maybe you can clarify? The stuff they're doing with Apollo here that potentially comes across as boilerplate also gets query batching, caching, refetching, etc. for free (in addition to the fancy stuff they also talk about like automatic mocks). It's not like they're "just sending a request." Maybe you can show me how you do all that in another framework without some setup/wrappers/etc.?

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by EduardoBautista on Saturday January 12, 2019 @08:30PM (#18894432)
Things Ember Data's `findRecord` does:

* Checks if you already have the resource to prevent sending an unnecessary request (can be forced)

* Retrieves the resource based on a convention for the URL

* Parses the JSON based on a convention

* Stores the resource in the "store" for use in the app

Batching is also very simple thanks to a plugin from Netflix:

https://github.com/Netflix/ember-batch-request

The reason the Ember community doesn't talk about any of this stuff is because, from the developers perspective, nothing interesting happens, which is why developer productivity is high in Ember.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @08:35PM (#18894454)
Why would a UI framework know anything about records? Keep in mind that it's explicitly not React's goal to know anything about requests, routing, records, data, or anything like that. That's why you hear React people talking about how they achieve this stuff. Ember is a framework with a much larger scope, while React is a view layer.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by EduardoBautista on Saturday January 12, 2019 @08:38PM (#18894462)
Ember Data can be used separately from Ember itself. In fact, the official documentation treats them as if they are. It's even a separate project on GitHub:

https://github.com/emberjs/data

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @08:39PM (#18894470)
If even in Ember-land it's an entirely separate package, then why make the comparison to React at all..? (Ember + Ember Data) is more like (React + Apollo) then, no? But you're just comparing it to React without anything else.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by EduardoBautista on Saturday January 12, 2019 @08:43PM (#18894491)
My original comment said "React ecosystem", not just "React". Apollo is part of the ecosystem.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @09:01PM (#18894564)
Here's what just sending a request looks like with React + Apollo: https://codesandbox.io/s/q71px7njrw

Which boilerplate do you take issue with? There are a couple imports you might find ugly, I suppose. Most likely though the blog post we're talking about isn't a good impression of what "just making a request" looks like. :)

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by lhorie on Saturday January 12, 2019 @09:51PM (#18894732)
This assumes a GraphQL backend though. A lot of React shops still use a combination of redux and REST APIs, which are admittedly a tad on the verbose side
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @10:11PM (#18894797)
Seeing as the article under discussion is all about GraphQL, maybe the top-level comment was off-topic?

Another thing to consider though, and something I've done in the past, is that you can actually embed the GraphQL resolver runtime completely on the client and easily adapt REST endpoints to it. It's pretty neat! So you can use all the benefits of Apollo without actually having a server that speaks GraphQL.

This Apollo project is one such way to do that: https://github.com/apollographql/apollo-link-rest (although not the strategy I used... I don't think Apollo even existed yet at the time)

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by np_tedious on Saturday January 12, 2019 @11:19PM (#18895047)
I hadn't seen a client-side approach for that translation before, but I do know if a server-side one here: https://github.com/remind101/rest-graphql

The amount of stars would suggest the client approach is more popular. It's that consistent with your experience or am I not comparing apples to apples?

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @11:32PM (#18895086)
It's less about the approach being popular and more about dealing with the hand you've been dealt. The use case for something like apollo-link-rest is "I'm in control of the frontend only, and am being provided with a REST API." The popularity of it just speaks to how common it is for folks to find themselves in that scenario.

Approaches like rest-graphql on the other hand involve being able to control the API piece, thus being able to offer the frontend a real GraphQL endpoint to talk to. Adapting REST APIs to GraphQL like this is definitely much more common than the client-side use case (which is more of a last resort), so don't let the stars fool you. It's just that (in my experience) people tend to roll REST-to-GraphQL resolvers by hand rather than use a helper like rest-graphql, because it tends to not be too difficult. Writing GraphQL resolvers is one of the aspects of it I find most enjoyable, actually.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by np_tedious on Sunday January 13, 2019 @02:11AM (#18895551)
Now that I re-read the library I linked above, I think I got it backwards there.

rest-graphql is for the other direction: graphql to rest. I had previously investigated it in order to switch rest backend to graphql and provide a thin conversion layer so that several different clients could convert at their leisure.

These two things are not at all comparable

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by froglegs on Sunday January 13, 2019 @01:35AM (#18895457)
what’s the point of resolving rest to graphql on the client if you’re not working with actual gql resources? what do you gain?
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Sunday January 13, 2019 @02:57AM (#18895652)
In this case, you gain the Apollo helpers around entity management, caching, etc. and the components they've already created for sending queries & mutations – so the ecosystem basically.

Also if you're planning in the future to migrate the API to GraphQL, and this is just a first step, you'll be able to leave most of the frontend code you wrote the same and just switch out the "link" part. It's kinda like how most languages have generic database adapters where you don't need to think about whether you're talking to MySQL/Postgres/SQLite etc. most of the time.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by froglegs on Saturday January 12, 2019 @11:16PM (#18895030)
It was slightly off topic but I can understand it seeing as the example is in React/jsx and ember gets no love :) Personally jsx makes the hair on my body stand upright but that’s just me!
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Sunday January 13, 2019 @03:16AM (#18895713)
Likewise, I feel the same way about template DSLs (why add yet another language's control flow/scoping/etc. into the mix?). I think if JSX had been around first, many more developers would find templates off-putting, seeing as they amount to writing code that builds up other code as a string. It's just that they're the way things were done for so long...

I wonder what a newer dev's take would be? Would they find UI elements as values/expressions weird, or UI elements as strings weird?

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by froglegs on Sunday January 13, 2019 @05:28AM (#18896066)
I can see that argument... the same could be said about learning jsx as a layer on top of native js.. Before spa’s it was just interpolated strings rendered on the server.. people probably thought the same thing when rendering a string of html inside their python/ruby/java program. My take is if it gets the done and the team agrees, use whatever tool works! In ember-land, single file components are in the pipeline I believe, will be interesting to see if they end up supporting jsx too.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by mnutt on Sunday January 13, 2019 @11:35AM (#18897294)
Ember’s HTMLBars is no more “building up code as a string” than jsx is.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by Bahamut on Sunday January 13, 2019 @09:11AM (#18896729)
Just a counterpoint, but we've had a lot of pain with Ember, especially Ember Data, with only senior developers working on an app - figuring out the right areas to modify to implement a feature/bug fix was unnecessarily difficult, and some of the recommendations we got from those closely aligned with Ember (including from a former core dev) from a maintainability perspective were highly suspect.

We ended up switching to React by rewriting the Ember app in a few weeks and moving significantly faster as a team, gaining more flexibility & better abstractions (which we had to write, but it was not a problem with our engineers' quality). Since then, my whole organization has only written new UIs using React to my knowledge.

I personally would rather use Angular (latest) or React than use Ember again. I do appreciate some of the things Ember has brought to the ecosystem though like a robust CLI ala Rails, but I think the routing convention needs more flexibility and Ember Data needs some love to fix the ease of getting into some buggy situations.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by dbbk on Sunday January 13, 2019 @09:25AM (#18896775)
I’m a big fan of Ember but Ember Data is just completely unusable for me once you start getting into relationships. Say you have a blog post with many comments. Creating a new Comment ends up sending an UPDATE request to the Post model with ALL the comments in an array. It’s crazy.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by aboutruby on Saturday January 12, 2019 @08:15PM (#18894375)
I also much prefer simple REST APIs, just much easier to understand among other things. (e.g. fetch("/api/users").then(...) and it's good to go)
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @08:25PM (#18894417)
It's certainly easier to start out that way. But there are also a lot of pitfalls, that systems like what's described in the article are designed to solve.

For example: let's say when component A or component B get rendered, they need to fetch users in order to show something about them. You only want to make the request if either of these components is actually rendered on the screen. What if they both get rendered? Is something going to know to reuse the inflight request so you don't have two identical requests? Or is it just naively going to make unnecessary requests? What if some other thing already fetched the user list earlier, are either of them still going to make the request anyway? Or use the data that's already there?

Another example: you've got your result from the users endpoint. Now you fetch some user information from the "blog author" endpoint. Information about the same user is in both responses. What's the authoritative client-side source of information about that user now? Are you now going to potentially be displaying stale/conflicting info about that user in 2+ different places?

etc.

You can always just keep things simple if you want, and people in React and GraphQL land are happy to do that, too. But a lot of people are also focused on building complex applications. This is just to give you some perspective on the "why" here.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by shaunpersad on Saturday January 12, 2019 @09:51PM (#18894734)
> Is something going to know to reuse the inflight request so you don't have two identical requests?

I really don't see what GraphQL has to do with this problem. What you're describing are issues that pop up with any external datastore. GraphQL is just a protocol. If there are clients that take care of that type of caching, that's an implementation detail. You can also have REST clients that do the same.

> Are you now going to potentially be displaying stale/conflicting info about that user in 2+ different places?

If you knew you needed info about the user as a blog author as well, you could've included that in the users request. Isn't that what you would've done in GraphQL anyway?

I'm not trying to detract from GraphQL's usefulness, but you can accomplish the same things in REST pretty easily too, especially if you control both server and client code.

IMO, GraphQL really shines when you're implementing clients for APIs that you don't already control. In that case, the flexibility is great. But if you're building both the APIs and the clients, REST works (and has worked) pretty easily.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @10:07PM (#18894782)
Indeed! I wasn't talking about REST vs. GraphQL at all, but rather the benefit that fancy clients get you (notice I said "systems like what's described in the article", not GraphQL in general). I think that's what the grandparents were talking about, since the boilerplate they were discussing presumably comes from Apollo, not GraphQL. Apollo is a fancy client like you describe.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by aboutruby on Saturday January 12, 2019 @08:57PM (#18894544)
In my cases, for 1) and 2) the parent component would be fetching the user and passing it down to the child components.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Saturday January 12, 2019 @09:06PM (#18894590)
For (1), how does the parent know whether those components will actually be rendered? Keep in mind one of the requirements was "only fetch if they're actually rendered." Presumably some other components along the way could be deciding to conditionally render them, right?

For (2), the complexity is still there, just in the parent: it needs an entity store and to merge/invalidate when the same entity appears in multiple responses. So still not just a simple case of making some REST API calls.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by ludwigvan on Sunday January 13, 2019 @09:21AM (#18896763)
> For (1), how does the parent know whether those components will actually be rendered?

Well, usually parent decides whether the children will be rendered or not.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by exogen on Sunday January 13, 2019 @01:38PM (#18897978)
All the conditionals in your rendering are at the very top level..?
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by mattbillenstein on Saturday January 12, 2019 @08:38PM (#18894461)
GraphQL removes a lot of the REST boilerplate on the backend -- you define the graphql schema, and the clients can fetch it in any shape they want -- especially helpful for supporting different clients that want the data in different shapes depending on what the view is. ie, a web vs mobile view, or a customer web view vs one that might be for internal tools, etc
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by aboutruby on Saturday January 12, 2019 @08:59PM (#18894556)
I prefer having either too much information in the response for each case (I'm not operating at Google/Facebook-scale), and then other endpoints for sub-resources (both individual and lists).
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by yodon on Saturday January 12, 2019 @08:29PM (#18894428)
> ... hoops people in the React ecosystem go through just to send a request boggles the mind

React Hooks (currently in alpha for the next release of React) finally clean up that component lifecycle complexity I think you're referring to. Nader Dabit has a great post [0] on how to use React Hooks with GraphQL.

[0]https://medium.com/open-graphql/react-hooks-for-graphql-3fa8...

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by saranshk on Saturday January 12, 2019 @08:55PM (#18894537)
Thanks for the blog post. I was going to look for a similar thing soon enough.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by bgentry on Saturday January 12, 2019 @11:02PM (#18894981)
Shameless self-promotional plug: ember-apollo-client is pretty damn easy to get started with and fits in really nicely with the Ember way of doing things :)

https://github.com/bgentry/ember-apollo-client

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by Waterluvian on Saturday January 12, 2019 @08:22PM (#18894401)
Are you talking about thunks and action creators and whatnot? All totally optional and ideally with a future plan in mind. Just call fetch.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by xrd on Saturday January 12, 2019 @08:34PM (#18894451)
I don't disagree with what you are suggesting. I personally prefer react over all other frameworks. And, there are many blog posts with outdated suggestions referring to the items you've referenced. Knowing which of those posts to skip over now that the proper solution has been named is what's really challenging about React (and, of course, JS in general).

Probably the OP has seen two many of those posts and doesn't have the right frame of reference to know what to do, and Apollo looks like the same.

Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by dorian-graph on Sunday January 13, 2019 @09:55AM (#18896879)
You can end up writing a lot of boiler plate code to send requests in Elm, FYI.
Re:Airbnb Is Moving Faster at Scale with GraphQL and Apollo by dpacmittal on Saturday January 12, 2019 @11:30PM (#18895075)
Except Apollo is for sending graphql requests, not just any request.