Skip to content

Commit

Permalink
Merge pull request #19 from edsrzf/document-slice
Browse files Browse the repository at this point in the history
Document -slice in the README
  • Loading branch information
vektah authored Jan 26, 2019
2 parents 484aef2 + fb45fe4 commit 6f1fe26
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dataloaden github.com/dataloaden/example.User
```

In another file in the same package, create the constructor method:
```go
```go
func NewLoader() *UserLoader {
return &UserLoader{
wait: 2 * time.Millisecond,
Expand All @@ -36,23 +36,33 @@ func NewLoader() *UserLoader {
},
}
}
```
```

Then wherever you want to call the dataloader
```go
loader := NewLoader()

user, err := loader.Load("123")
```
```

This method will block for a short amount of time, waiting for any other similar requests to come in, call your fetch
function once. It also caches values and wont request duplicates in a batch.
function once. It also caches values and wont request duplicates in a batch.

#### Returning Slices

You may want to generate a dataloader that returns slices instead of single values. This can be done using the `-slice` flag:

```bash
dataloaden -slice github.com/dataloaden/example.User
```

Now each key is expected to return a slice of values and the `fetch` function has the return type `[][]User`.

#### Wait, how do I use context with this?

I don't think context makes sense to be passed through a data loader. Consider a few scenarios:
1. a dataloader shared between requests: request A and B both get batched together, which context should be passed to the DB? context.Background is probably more suitable.
2. a dataloader per request for graphql: two different nodes in the graph get batched together, they have different context for tracing purposes, which should be passed to the db? neither, you should just use the root request context.
2. a dataloader per request for graphql: two different nodes in the graph get batched together, they have different context for tracing purposes, which should be passed to the db? neither, you should just use the root request context.


So be explicit about your context:
Expand All @@ -62,10 +72,10 @@ func NewLoader(ctx context.Context) *UserLoader {
wait: 2 * time.Millisecond,
maxBatch: 100,
fetch: func(keys []string) ([]*User, []error) {
// you now have a ctx to work with
// you now have a ctx to work with
},
}
}
```

If you feel like I'm wrong please raise an issue.
If you feel like I'm wrong please raise an issue.

0 comments on commit 6f1fe26

Please sign in to comment.