4 ms·
I looked at GraphQL..... can someone please explain how extensive join queries are done? Literally almost every app I ever write, even the simple ones, offload
by Exuma 10y ago
I looked at GraphQL..... can someone please explain how extensive join queries are done?
Literally almost every app I ever write, even the simple ones, offload tons of work to the database for joins between a half dozen tables.
I have yet to see a sane answer on how this is possible with GraphQL? How am I supposed to think about performance ... and not just having something overly simple/basic like HERES MAH BLOG... HERES DA COMMENTS!!!!!!
- jlouis 10y agoSquint your eyes a lot! A GraphQL query is just a query on some objects having some fields. You can tie any function to a field. In particular, you can have each field be a function which executes SQL code. And that code is free to JOIN as it wants. Squint your eyes some more: how do you do it in a RESTful interface? It is the same idea. GraphQL really excels, however, in that it is not tied to SQL. You can have part of the query be from one source, and other parts be from other sources.
- ruslan_talpa 10y agoLook at PostgREST, then look at what &select parameter does, you'll see a mini GraphQL query there. Every request generates a single query. Then know that it's possible to have PostgREST execute GraphQL queries :)
- jahewson 10y agoGraphQL is not a query language for your database, it's more like a query language for your REST API. So asking how to do a join in GraphQL is like asking how to do a join in REST. It's not a well-formed question. The good news is that the sever can expose absolutely anything as a GraphQL query. They're just arbitrary entry-points into the schema. Think of it as like calling a stored procedure. The procedure is an opaque thing as far as the caller is concerned.
- ruslan_talpa 10y agoYou are dismissing a valid question and incidentally this is the question that exposes a problem of the graphql-js implementation (specifically the execution module) when the data source is a database (which 90% of the time it is), though it's not a problem of the GraphQL . Of course he can not "well-forme" it since he is just looking into GraphQL. What he is looking for actually is this https://github.com/stems/join-monster https://github.com/stems/join-monster but it's not without it's problems. PS: My comment does not mean the reference implementation is bad, it's just that it's a reference and good in a lot of cases but not in all. It does not mean you have to use every part of it, you can write your own execution module.
- tooFastFuckYou 10y agoIt's a totally legitimate and well-formed question. What is the point of using GraphQL if you're still implementing every query on both the server and the client? I thought GraphQL was some way of abstracting the database so that you could isolate your query logic in the client. That logic is going to require joins in a great many applications. The big pain in the ass of writing a client/server app is keeping the queries in sync between the server and the client, from the parameters for the query itself to deserializing the result. How does GraphQL alleviate this, and if it doesn't, what are we gaining? Thanks for any insight.
- Horusiath 10y agoIt depends on the actual implementation of the server side, that you're using. From what I remember (this may have changed): - Ruby implementation can dig into Active Record to perform batching of the queries, that normally would be executed in parallel. - JavaScript implementation may use DataLoader library to support the same batching behavior for basically any async (promise-based) operation. See this presentation, it shows how the issue look like and how Data Loader helps to solve it: https://www.youtube.com/watch?v=c35bj1AT3X8&t=15m42s https://www.youtube.com/watch?v=c35bj1AT3X8&t=15m42s - Python and Elixir implementations can make use of information about query subsegments, so you can prefetch necessary data. - F# implementation works in similar way to Elixir but it's not limited to analyse GraphQL query fragments, but can also perform live analysis of the user-defined resolve functions code to determine a tree of used F# objects and properties instead. This allow to diverge GraphQL domain model from the underlying database model. - A lot of other libraries are dedicated to particular database or query language and map GraphQL schema directly onto database model, so GQL query is translated directly into underlying database query model (including joins).