I am using pg-promise in a GraphQL application and because of the nested/iterative nature of the resolvers each HTTP request makes a lot of database queries.
So I am wondering is there is a more effective way to share a connection from the connection pool as the resolvers are collecting data?
I understand that a pg-promise
task is only alive for the callback of the function and I don't see any other way to chain the queries (as documented here).
Example
GraphQL Query:
{
users {
files {
name
date
}
}
}
Resolvers example when using Apollo Server
Query: {
users: (obj, args, context, info) => {
return context.db.manyOrNone('select id from users')
}
}
and
Users: {
files: (obj, args, context, info) => {
const userId = obj.id;
return context.db.manyOrNone('select * from files where user_id = $1', userId);
}
}
This will generate lots of SQL queries if there are lots of users for instance.
NOTE
I'm aware of techniques like dataloader
to address problems like N+1 Select but I cannot afford to rearchitect this application at the moment and simply being more efficient with database connections would be a huge performance win.
Thank you.