vektah / dataloaden

go generate based DataLoader
MIT License
528 stars 79 forks source link

don't panic when fetch returns a single error #5

Closed jon-walton closed 6 years ago

jon-walton commented 6 years ago

occasionally we were hitting a point in our codepath where we were returning a single error from our fetch func back to dataloaden. Doing so ended up causing a panic index out of range.

the conditions seem to be if multiple keys get passed into fetch() but we only return a slice of 1 error. LoadThunk would get the key index and later use that index to set the error, however if 40 items were passed into fetch(), we get an our of range panic

simplified example:

func newMemberLoader(ctx context.Context, db *sqlx.DB) *MemberResolverLoader {
    return &MemberResolverLoader{
        maxBatch: 100,
        wait:     10 * time.Millisecond,
        fetch: func(keys []string) ([]*MemberResolver, []error) {
            return nil, []error{errors.New("an error!")}
        },
    }
}
vektah commented 6 years ago

Thanks, a test for this would be good.

jon-walton commented 6 years ago

will do