News for hackers. Stuff that matters.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @12:00PM (#14677629)
You've hit a (pain) point. While graphql reduces the amount of trips to the backend for the browser, those round trips get pushed down to a lower level, between backend and db. The reference graphql-js implementation, while at first looks so easy, you just write a resolver for a field, makes it oh so easy to have terrible n+1 problems. dataloader helps a bit but it's not as optimal as it can be.

You need to be very careful. To be optimal, you'll basically need to write very complex resolvers that inspect the AST themselves and fetch the data optimally which at that point is almost as writing your custom execution module.

That is basically what i did, custom execution module to translate a graphql request to a single sql query (https://subzero.cloud/)

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by hliyan on Saturday July 01, 2017 @03:02PM (#14678428)
Sometimes I worry whether GraphQL is analogous to ORMs: a theoretically elegant attempt to solve an impedance mismatch between a data source and its consumer, but one that ultimately just shifts or redistributes the impedance to different layers of the codebase...
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by westoncb on Saturday July 01, 2017 @05:17PM (#14679083)
I agree it seems like potentially the same problem, and it manifests in forcing devs to write 'resolvers' which looks like horrible drudge work. So I wonder if there are decent offerings without the impedance mismatch between GraphQL queries and the database (so we don't have to translate them into SQL). I did a very quick look and at least found this: https://dgraph.io/

I'm getting the impression that if nothing else though, I'm probably gonna need to wait a few years for this tech to stabilize more. Which I find very unfortunate, because the standard way of doings things these days feels very unpleasant to me (I hate writing boilerplate more than anything: have an overuse injury, so typing is the worst part of coding)β€”and the GraphQL queries do seem to make a lot of sense (although forcing clients to have explicit knowledge of schema structure seems a little dangerous...).

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by dustinmr on Saturday July 01, 2017 @05:34PM (#14679157)
Here's a project that might be interesting.

https://github.com/postgraphql/postgraphql

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Monday July 03, 2017 @02:29PM (#14690384)
Technique inspired by PostgREST and graphql spin "stolen" (in a good way) from subZero :), although a completely different implementation method then subZero
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by jarym on Saturday July 01, 2017 @12:48PM (#14677811)
That's really cool - we're doing the same thing, translating entire queries into a single SQL statement!
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @01:01PM (#14677857)
I hope you are using PostgreSQL! :) Check out PostgREST and the types of queries it generates
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by jarym on Saturday July 01, 2017 @01:23PM (#14677962)
Yes, Postgres all the way. We don't use PostgREST though we construct the same type of queries.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by i_s on Saturday July 01, 2017 @04:18PM (#14678782)
> custom execution module to translate a graphql request to a single sql query

If I have a schema like this:

    users
      projects   (many per user)
      blog_posts (many per user)
What SQL do you generate to handle a request for everything in a single query?
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @04:28PM (#14678824)
take a look at PostgREST https://github.com/begriffs/postgrest

simplified example query https://pastebin.com/7J2ZswRC

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by i_s on Saturday July 01, 2017 @04:36PM (#14678867)
Ah, nice trick - thanks for the tip.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by scottmf on Saturday July 01, 2017 @12:06PM (#14677648)
Which n+1 problems doesn't dataloader solve?
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @12:14PM (#14677688)
It eliminates n+1 but not in a perfect way.

If you have a 3 level query (3 tables), the best you can hope for is to get 3 sequential queries, like get first level, collect the ids, request the second level, collect the ids, get 3rd level. It gets even more complicated teh more levels you and the bigger the dataset returned.

all of this can be done using a single join which is one roundtrip and it's faster.

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by mambodog on Saturday July 01, 2017 @12:45PM (#14677802)
If you have low latency queries (eg. because most things hit cache rather than db) sequential queries aren't as much of a problem. Being able to join everything into one query, on the other hand, is a luxury which can be hard to maintain at scale.

In the 'join in SQL' case you could identify particular cases which are doing sequential queries and implement a different loader which just does one. It's not automatic but perhaps in most cases it's not necessary to do this step anyway. In the worst case you're back to doing as much work as you would for a bespoke API endpoint, but that's not the typical case. How much of a problem this ends up being in practice very much depends on the type of app you're building and how you intend to scale it.

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @12:59PM (#14677852)
Saying join is a luxury is a dangerous thing :) (for impressional devs :)) 99% of projects are not "at FB scale" :) so join is exactly the right thing to use, it's been tuned over decades so until your scale/dataset does not outgrow one box (and there are big boxes now), you are not going to do a better job then the query optimiser (cause after all that is what you are trying to do).
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by mambodog on Saturday July 01, 2017 @01:16PM (#14677928)
Fair enough. What I'm trying to get at is that you need to pay attention to the cost of the real world queries which are actually being executed against your API, and then optimize. I'm not sure that SQL is a magic bullet there either (you can still request too much data at once, for example).

In practice this probably means adding some logging of how long requests take and graphing it (say, 95th percentile request time) from time to time to spot pathological queries. Even better if you can automate it. I think this is stuff that everyone should be doing (after a certain stage), regardless of whether you are using GraphQL or a bespoke JSON API.

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by andrewingram on Saturday July 01, 2017 @03:02PM (#14678429)
I'd say build out your GraphQL server under the assumption that joins aren't always an option, but allow them as an optimization where possible.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @03:26PM (#14678534)
why would i "optimise" for the thing that might never happen (join not an option) while taking the hit right now? i've tested this (3 level query) and the throughput is 10 times slower with dataloader, as in i need 10 servers instead of one to do the same job.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by andrewingram on Saturday July 01, 2017 @03:34PM (#14678558)
I mean to structure your server in such a way where you're not trying to automatically convert your entire query AST into a single massively complicated SQL query. It's true that in simple cases, one complex query can be the fastest way to get the data you need, but you leave this territory pretty quickly.

It's perfectly valid to identify subtrees of your query that would benefit from being executed as a database query, but to do that to your entire query just sounds like you're asking for trouble, I'd even go so far as to call it a premature optimization.

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @04:05PM (#14678708)
I kind of understand why you'd think it's a massively complicated query. You are probably thinking the types of joins on two tables on random columns with weird conditions which go into full table scan.

I am talking about queries/joins between tables that have foreign keys between them, like client/project/task/comment. I bet 90% of graphql schemas expose those kinds of relations between types.

For those type of relations (with FK) i can generate a single query that is as fast as it can be (certainly faster then dataloader) and as far as i've tested (a few millions of rows in tables, 3-7 levels in a query) i didn't leave the fast territory :) Of course there might be edge cases ...

About premature optimisations. Everyone likes to quote that, but never the full one which is "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." https://shreevatsa.wordpress.com/2008/05/16/premature-optimi...

The paper where it was published was about using goto to optimize loops and such small things, it was never "targeted" at algorithms and architecture.

As i think i mentioned here above, i was getting 10X throughput with joins compared to dataloader and i would not call that premature.

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by andrewingram on Saturday July 01, 2017 @08:18PM (#14679938)
I use a programming language that is 1-2 orders of magnitude slower than C, should I switch today?

I get my data for a full UI within the budget i've allowed for uncached scenarios (100ms, but I want to go to 50ms). The approach you're suggesting will (in some circumstances) give me some short-terms win in terms of throughput and response times, but you've not said anything to suggest that I won't lose these benefits as I gradually transition into a domain-driven or micro-services (yuck) architecture.

I like to build my GraphQL servers under the assumption of a domain-driven architecture (because that's where all the projects I've worked on seem to end up, your mileage may vary), and then shoe-horn in some short-term performance tricks when I can.

I'm possibly a special snowflake here, but it's been a long time since i've had the opportunity to work on a project where I can go straight to the DB. Be it Elastic Search, a 3rd party, ill-advised micro-services, or complex logic in-between storage and presentation; nothing has quite been a pure DB project in the last 6-7 years.

Of course, you could argue this is premature architecture ;) but many of these complexities are from day one, or at least pretty early in a project's life.

Re:React, Relay and GraphQL: Under the Hood of the Times Web... by raarts on Saturday July 01, 2017 @12:31PM (#14677759)
Thank you, thank you. A critical piece of information.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by foota on Saturday July 01, 2017 @12:10PM (#14677668)
You don't have to worry so much about n+1 (once you're using dataloader) as much as you do deeply nested queries, or queries that run against a large number of datatypes.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by ruslan_talpa on Saturday July 01, 2017 @12:16PM (#14677696)
i am not sure i follow, the deeper the query or bigger the returned dataset, the worse the performance of a dataloader type solution (see my other comment).
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by foota on Saturday July 01, 2017 @12:54PM (#14677833)
We're agreeing here, I think. I'm saying that after taking care of the n+1 problem by using dataloader, you still have to worry about deeply nested queries.
Re:React, Relay and GraphQL: Under the Hood of the Times Web... by andrewingram on Saturday July 01, 2017 @03:01PM (#14678425)
This is true, but most complex UIs tends to result in queries that spread wide rather than deep. It takes some deliberate contrivance or fairly unusual real-world cases to get to more than 3-4 levels of nested relationships, and this is often the point at which you'd be thinking to deferred/lazy loading in the client anyway.

I've spent a significant amount of time over the last year or so optimizing GraphQL servers built on domain-driven services (i.e. joins aren't an option) and managed to get to equal (or very marginally worse) performance to existing handcrafted endpoints that returned equivalent data (it was possible to build the same UI, even though the payloads weren't identical).

There are areas where GraphQL is inherently inefficient (trying to work on ways to mitigate these issues), but the reality is that deeply-nested UI appears to be less of a problem than I originally thought it would be.