Replies: 1 comment 2 replies
|
Hey, great question. The main thing that came to mind here was Lookahead (https://graphql-ruby.org/queries/lookahead.html). You could determine whether users' cities were going to be loaded with something like def self.city(context, id:)
root_lookahead = context.query.lookahead
if root_lookahead.selects?(:cities)
# ... ?
elsif root_lookahead.selection(:users).selection(:nodes).selects?(:city) ||
root_lookahead.selection(:users).selection(:edges).selection(:node).selects?(:city)
# ... ?
end But I'm honestly not sure how to get Dataloader (are you using that?) to order these queries in the right way. Could you share some details about how you accomplished this before? Maybe that will help me brainstorm 😅 |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to optimize the communication with the database and make as few queries as possible. The context can be e bit complicated, so I think I'll manage to explain it best if I lead with an example.
I end up having the following GraphQL query (really, several batched/multiplexed ones, but for simplicity's sake):
Resolved in the order written, it would result in three SQLs:
Now, if I could choose in what order the to run the resolvers, there are two things that can be done here.
Query.usersget resolved beforeQuery.city, then theUser.cityand theQuery.citycould leverage batching to run only oneWHERE id IN (?), reducing the count of queries to 2.Query.citiesgets resolved first, there is a chance that the cities we need for the other two resolvers are in the first page and we can just use them, collapsing everything to a single query tocities.I've gotten that to work with the "old" execution engine, but it's kinda messy and needs to rely on private stuff. As far as I can tell, if there is a hook in the new breath-first execution engine that would allow me to pick/override the order in which the query is resolved (on a per-query basis), that could end up being an elegant solution.
I guess the questions are (1) is there a way I could do it and (2) is there some other approach I am missing to accomplish the same result?
Thanks :)
All reactions