3 ms·
2) is easy, but leads to two queries no matter what, because you have to do a whereIn 3) here's the code: const EagerQL = (info, allowed) => { let fields =
by zackify 9y ago
2) is easy, but leads to two queries no matter what, because you have to do a whereIn
3) here's the code:
const EagerQL = (info, allowed) => {
let fields =
info.operation.selectionSet.selections[0].selectionSet.selections;
return fields
.map(field => field.name.value)
.filter(field => allowed.includes(field));
};
// in a resolver
user(_, args, context, info) {
let eagers = EagerQL(info);
console.log(eagers);
// ['posts']
}
This will give you fields that were queried for so you can do a big join. Hope this helps someone :P
- joesb 9y agotrying to access fields like `info.operation.selectionSet.selections[0].selectionSet.selections` always feel wrong to me. I wish graphql library part comes with some utility function for these.
- zackify 9y agoCompletely agree.... I don't understand why there isn't a more standard way of accessing the fields queried for so that you can do joins.