Fork me on GitHub
#off-topic
<
2018-11-04
>
rgb-one01:11:53

@slipset Yea, that's the purpose. Why do you think REST is the wrong approach for this use case?

slipset09:11:31

@rgb.ide Because a SPA is basically doing two things: It’s querying a database (through a proxy which is your backend). GraphQl and it’s likes are bette at this than vanilla REST. GraphQl lets the client decide the shape of the data it needs and also lets the client request all the data it needs in one request, avoiding the n + 1 problem. https://secure.phabricator.com/book/phabcontrib/article/n_plus_one/

slipset10:11:03

The other thing it’s doing is issuing commands. Yes you can do this with REST, but I think you’ll see over the course of time that by thinking in terms of commands, the vocabulary of your system will appear. IMO REST mutability in REST is about mutating resources. When you need to mutate several resources in a transaction, REST is not a good fit.

slipset10:11:30

The stupidest example, which can be refuted easily is “transfer funds between two accounts”

slipset10:11:59

How do you do this in REST? I’d probably do

slipset10:11:48

POST /transfer/:from-acount/to/:to-account/:amount

slipset10:11:03

Which is just a very convoluted way of doing:

slipset10:11:15

POST /do-it
{:command :transfer-funds
  :from-account ...
  :to-account ...
  :amount ...}

slipset10:11:43

If you adopt this pattern implementing new commands on the backend is just a matter of adding another defmethod to your (defmulti execute :command)

slipset10:11:41

And it becomes quite clear what your application is about, since you’ve now enumerated all the commands available as implementations of the execute function.

3Jane11:11:37

standard GraphQL has a huge problem with N+1, unless you add dataloader

3Jane11:11:22

What it is good at is giving frontend flexibility.

3Jane11:11:21

With a well-designed schema you have a huge potential for data exploration.

3Jane11:11:00

It’s especially beneficial when your schema grows and your entity graph is densely interconnected.

3Jane11:11:03

Here’s a list of publicly available APIs, have a play and see for yourself. https://github.com/APIs-guru/graphql-apis

3Jane11:11:48

(Happy to talk more about specifics when you have questions, I’ve been working on a graphql backend serving a huge graph from multiple datasources for the last 2 years)

slipset11:11:49

@lady3janepl just so I understand. I was referring to n+1 between client and API, and that’s the n+1 problem I see GraphQL solving.

slipset11:11:28

What happens on the server wrt n+1 is a different problem 🙂

3Jane11:11:30

well, the location of the problem basically moves from client to API.

slipset11:11:43

Then we agree.

3Jane11:11:21

I’ve seen people are unaware about n+1 within the server, adopt the technology and get confused when they hit performance problems; so I wanted to make that specific.

slipset11:11:50

We’ve ended up with a bunch of ad-hoc aggregated REST-endpoints to “fix” the n+1 between client and server. Doesn’t really scale all that well.

3Jane11:11:00

All the companies that used it heavily that I know of had their own server implementation (that was not dataloader)

slipset11:11:34

And yes, we have a bunch of n+1 problems on the server because we’ve composed our “fetch n things” out of a bunch of “fetch 1 thing”.

3Jane11:11:43

Facebook has TAO, shopify (I think) has something of their own as well although it’s more publicised.

3Jane11:11:40

One thing you can do in that case (if you want a quick solution to improving performance, and if you don’t want to faff with batching through dataloader) is log queries and check which ones come from your downstream(s) the most, and build in optimised solutions for those ones in particular. It’s a “discovery” approach similar to how public planning discovers where to put down asphalt pedestrian paths: check where people walk often enough to create visible tracks and then put a path there.

jaihindhreddy13:11:59

Is that how most people do it, or are solutions like join-monster better suited to solve the n+1 problem b/w the server and the DB? (Assuming its a SQL DB)

jaihindhreddy13:11:36

Or pathom + walkable in the clojure world.

jaihindhreddy13:11:22

But the last I've seen walkable still does table joins on the server.

3Jane13:11:18

As with everything in programming, I doubt there’s a good answer to how most people do it because it’s hard to know what goes on in closed projects 🙂 What I outlined above is a quick and universal solution.

👍 4
3Jane13:11:01

I’d strongly caution against using anything that ties your graphql schema too tightly to your sql schema, or that generates graphql from sql.

4
💯 4
3Jane13:11:13

(for example postgraphql, postgraphile, and whatever neo4j wrote for it)

3Jane13:11:39

(And I don’t have much experience with join monster because I’m stitching disparate data sources in different kinds of stores)

jaihindhreddy13:11:32

README sounds promising. Haven't checked it out still.

3Jane13:11:28

hm… I have to prod around it some more

3Jane13:11:15

(if you’re interested in graph data specifically, you can also take a look at gremlin, but now we’ve drifted far off from the question of whether to REST or not)

3Jane13:11:25

(and also: for some uses REST is still best.)