News for hackers. Stuff that matters.
Re:The GitHub GraphQL API by Kwastie on Wednesday September 14, 2016 @03:38PM (#12500514)
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.

Re:The GitHub GraphQL API by brblck on Wednesday September 14, 2016 @03:59PM (#12500720)
Yup. 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.
Re:The GitHub GraphQL API by arianvanp on Wednesday September 14, 2016 @04:06PM (#12500778)
Exactly, 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...
Re:The GitHub GraphQL API by pcsanwald on Wednesday September 14, 2016 @06:37PM (#12501801)
you can use a helper library like the wonderful graphql-sequelize [https://github.com/mickhansen/graphql-sequelize] which helps you get from an ORM like sequelize to graphql queries/results fairly easily.
Re:The GitHub GraphQL API by biscarch on Wednesday September 14, 2016 @03:46PM (#12500599)
You can use something like dataloader[0] or haxl[1] to help with this issue.

[0]: https://github.com/facebook/dataloader

[1]: https://github.com/facebook/Haxl

Re:The GitHub GraphQL API by xentronium on Thursday September 15, 2016 @04:06AM (#12504257)
In 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)