zenyui / gqlgen-dataloader

Demonstrates GqlGen using the graph-gophers/dataloader
27 stars 5 forks source link

DataLoader ctx field is never assigned #5

Closed frederikhors closed 3 years ago

frederikhors commented 3 years ago

I think I found a strange thing here:

type DataLoader struct {
    ctx        context.Context
    userLoader *dataloader.Loader
}

func (i *DataLoader) GetUser(userID string) (*model.User, error) {
    thunk := i.userLoader.Load(i.ctx, gopher_dataloader.StringKey(userID))
    result, err := thunk()
    if err != nil {
        return nil, err
    }
    return result.(*model.User), nil
}

You are calling i.ctx but that ctx on DataLoader type is never assigned in all your code in dataloader.go.

Am I wrong?

zenyui commented 3 years ago

Nice catch. Since the data loader is no longer request scoped, we should pass in the request context to the GetUser method