5 ms·
Since the announcement of GraphQL I've been waiting for some 'real world' apis. (Sure the Star Wars GraphQL apis are fun) Does anyone know any best practices i
by Kwastie 10y ago
Since the announcement of GraphQL I've been waiting for some 'real world' apis. (Sure the Star Wars GraphQL apis are fun)
Does anyone know any best practices if you want to adopt this is in an existing application using a relation database (i.e. PostgreSQL). I don't know how to implement this without causing N+1 queries. (or Worse).
For example:
{
Post {
title,
content,
Author {
name,
avatar,
},
Comments(first:10) {
..
}
}
}
A naive implementation would cause a lot of query, for each "edge" a query.
- biscarch 10y agoYou can use something like dataloader[0] or haxl[1] to help with this issue. [0]: https://github.com/facebook/dataloader https://github.com/facebook/dataloader [1]: https://github.com/facebook/Haxl https://github.com/facebook/Haxl
- brblck 10y agoYup. That's something we can handle internally, under the hood to batch database requests into a single query for all edges. This problem is actually easier to solve in GraphQL then it is with a traditional REST API.
- arianvanp 10y agoExactly, now you put the onus to solve N+1 on the serverside, instead of in the old case the client would do N+1 requests...
- pcsanwald 10y agoyou can use a helper library like the wonderful graphql-sequelize [https://github.com/mickhansen/graphql-sequelize https://github.com/mickhansen/graphql-sequelize] which helps you get from an ORM like sequelize to graphql queries/results fairly easily.
- xentronium 10y agoIn rails/graphql combo it's resolved the same way you usually resolve n+1 queries: you make 1 request per type of edge (i.e. one request for posts, one request for authors, on request for comments)