I'm just starting out with graphql so this may be a stupid question, but I have the following situation: I have an object, let's call it car, it has various properties, among them a log of drivers or whatever. When I query a specific car and I want the logs as well, the resolver runs and returns the log entries from the database using the parent id. But when I'm querying multiple cars the resolver for the log entries would run for each car that is returned.
Outside of graphql I would fetch all required log entries at once and assign them to each of the cars so I only need to query the database once.
Is there any way I can accomplish this in graphql? I think it's more efficient to run one query and filter the results instead of running one query for each queried object. Or am I overthinking it and I should just query the database for each record? I guess, what I need to know is: Is there a way to know from the parent resolver if I need to resolve data in a child resolver and whether I can fetch this data in the parent and pass it down to the child resolver.
I have since found what I should've been looking for: lookahead data fetching and prefetching. You can get the fields from the fourth resolve argument. From there you can determine if any fields are queried that can be prefetched.
Not sure about the performance but I assume it's better to prefetch all the required rows once instead of running lot's of queries for each object in the result.
Anyway, yes prefetching is possible, not sure about performance but I can now at least run some benchmarks to see how performant it is.
Edit: Ended up using a dataloader. Didn't realize that it automatically batches your requests, so you don't actually have to have any logic to prefetch oder lookahead.