vektah / dataloaden

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

How could one implement pagination using this library #16

Closed Daavidaviid closed 5 years ago

Daavidaviid commented 5 years ago

Hi, Thanks for the good work.

I was wondering, how would one proceed to have pagination with dataloaden (or dataloader more globally) ?

For instance with ordersByCustomer, we currently have :

// 1:M loader
ldrs.ordersByCustomer = &OrderSliceLoader{
    wait:     wait,
    maxBatch: 100,
    fetch: func(keys []int) ([][]Order, []error) {
        var keySql []string
        for _, key := range keys {
            keySql = append(keySql, strconv.Itoa(key))
        }

        fmt.Printf("SELECT * FROM orders WHERE customer_id IN (%s)\n", strings.Join(keySql, ","))
        time.Sleep(5 * time.Millisecond)

        // ...

        return orders, errors
    },
}

Could ordersByCustomer become something like this :

// 1:M loader
ldrs.ordersByCustomer = &OrderSliceLoader{
    wait:     wait,
    maxBatch: 100,
    fetch: func(keys []int, last int, from Date) ([][]Order, []error) { // <-- HERE
        var keySql []string
        for _, key := range keys {
            keySql = append(keySql, strconv.Itoa(key))
        }

        fmt.Printf("SELECT * FROM orders WHERE customer_id IN (%s) AND WHERE created_at > (%s) LIMIT (%d)", strings.Join(keySql, ","), from, last) // <-- HERE
        time.Sleep(5 * time.Millisecond)

        // ...

        return orders, errors
    },
}

Or do I need to hack something using the example with context ?

Regards, David.

Daavidaviid commented 5 years ago

I just realized that it was a pretty dumb question actually.... 😱