There are often examples of injecting the batch function into the context when implementing dataloader in Golang, but I'm not sure why. While I understand the concept of caching data per request in the dataloader mechanism, I'm not sure if it's necessary to inject the batch function into the context.
I'm considering implementing it like the following, where constructor injection is used. However, I'm not sure if such an implementation is not recommended:
func (r *issueResolver) User(ctx context.Context, obj *model.Issue) (*model.User, error) {
thunk := r.Loaders.UserLoader.Load(ctx, obj.User.ID)
user, err := thunk()
if err != nil {
return nil, err
}
return user, nil
}
With this approach, the context is still limited to the request scope, and the data is cached per request, so there shouldn't be any issues. Am I correct in my understanding?