3 ms·
I'm interested in learning solutions for this as well, but it's hard to find because most GraphQL tutorials stop at the resolver level. There are a couple appr
by xtagon 9y ago
I'm interested in learning solutions for this as well, but it's hard to find because most GraphQL tutorials stop at the resolver level.
There are a couple approaches I've seen for mapping to an SQL backend.
One is batching - You get the top level resources, then save their accosted record IDs until later so you can make fewer calls to get their associations. This still makes quite a few database calls, especially for deeper queries or many different type of objects.
Another approach is "whole tree up-front" where you take the AST of the graph you're trying to resolve, and convert that to one big SQL query that joins all the associated objects. This can be more performant in some situations as it only makes a single database query, but a drawback is that larger queries eat up a more bandwidth from your query since having many joins causes data duplication. Another drawback is that having a single query makes it so you can't cache each resolver individually like you might be able to do with a batching solution.
I don't have all my links handy but here's one you can check out: https://github.com/facebook/dataloader https://github.com/facebook/dataloader
If anyone has something to add please chime in! I'd love to hear how people are really using GraphQL with SQL backends!
- benwilson-512 9y agoHey! I'm one of the co-authors of the Elixir implementation Absinthe. Absinthe has an extensible middleware mechanism from which you can build a variety of different batching patterns. We run our API at http://www.cargosense.com/ http://www.cargosense.com/ entirely via GraphQL over quite a lot of postgres tables and this has remained very performant for us. Elixir makes it easy to do concurrent batched requests, as well as in memory caches that live for either the duration of the request or long as desired. For some particularly complicated cases we have a materialized view, but this is largely used to power a reporting service that builds snapshots from this. Any questions you have in particular?