Reputation: 43
I recently implemented in nodeJS a dataloader for my graphQL API.
It works fine when I set the batchScheduleFn to a standard setTimeout for 100 ms for example, so I know everything works and fetch the data correctly.
What I want to do is to make the actual calls to the database, so to call dispatch() on the dataloader when all resolvers from a depth level of the graphQL query have been called. This should allow for the fastest calls to the database, since all objects of the same type can be called in the same batch.
However I can't figure out how to know when the graphQL engine has reached the end of a query depth level.
For reference, if we consider the following graphQL query :
films {
actors {
name
}
}
what I call a depth level is each nested object : so films is one level, actors is the following one etc ...
So when all resolvers for films have been called, I want to have the dataloader actually make the query to the database, that way I am sure it will be only one round trip to the database to fetch all films. And of course do the same for each level of the query.
But I couldn't figure out a way to check when all resolvers of a level have been called, I found out how to check when the graphQL engine passed a level, when it calls the first resolver on the next level, but not when it calls the last resolver on a given level.
Essentially I want to achieve what the java implementation of graphQL is capable of doing, as described here : https://github.com/graphql-java/graphql-java/issues/1198#issuecomment-630927882 (control+f "depth" to find the exact part in the comment)
Upvotes: 0
Views: 735
Reputation: 11
Add a numFilmssQueried
field to the context which you set at the end of the fields query. Then as you call each field resolver for actors, tally another field on context called numFilmsLoadedIntoActorsDataloader
. When "numFieldsQueried" equals numFieldsLoadedIntoActorsDataloader
, dispatch your DataLoader.
Upvotes: 1