Skip to content

Request to reintroduce Key/Keys interfaces #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update README.md
  • Loading branch information
sGy1980de committed Aug 1, 2022
commit bb37d01595429c08522c5e81568b1d40d65c9b96
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
coverage.txt
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# DataLoader
[![GoDoc](https://godoc.org/gopkg.in/graph-gophers/dataloader.v3?status.svg)](https://godoc.org/github.com/graph-gophers/dataloader)
[![GoDoc](https://godoc.org/gopkg.in/graph-gophers/dataloader.v7?status.svg)](https://godoc.org/github.com/graph-gophers/dataloader)
[![Build Status](https://travis-ci.org/graph-gophers/dataloader.svg?branch=master)](https://travis-ci.org/graph-gophers/dataloader)

This is an implementation of [Facebook's DataLoader](https://github.com/facebook/dataloader) in Golang.

## Install
`go get -u github.com/graph-gophers/dataloader`
`go get -u github.com/graph-gophers/dataloader/v8`

## Usage
```go
// setup batch function - the first Context passed to the Loader's Load
// function will be provided when the batch function is called.
batchFn := func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
batchFn := func(ctx context.Context, keys dataloader.Keys[string]) []*dataloader.Result[any] {
var results []*dataloader.Result[any]
// do some async work to get data for specified keys
// append to this list resolved values
return results
Expand All @@ -32,7 +32,7 @@ loader := dataloader.NewBatchedLoader(batchFn)
* The first context passed to Load is the object that will be passed
* to the batch function.
*/
thunk := loader.Load(context.TODO(), dataloader.StringKey("key1")) // StringKey is a convenience method that make wraps string to implement `Key` interface
thunk := loader.Load(context.TODO(), dataloader.KeyOf("key1")) // KeyOf is a convenience method that make wraps any comparable type to implement `Key` interface
result, err := thunk()
if err != nil {
// handle data error
Expand All @@ -42,7 +42,15 @@ log.Printf("value: %#v", result)
```

### Don't need/want to use context?
You're welcome to install the v1 version of this library.
You're welcome to install the `v1` version of this library.

### Don't need/want to use type parameters?
Please feel free to use `v6` version of this library.

### Don't need/want to use Key/Keys interface?
Just use the `v7` version of this library. This completely removes the need for the `Key` interface, but it limits
the key type parameter to `comparable` types only, whereas `v8` allows `any` type, as long as it is wrapped `Key`, and
exports itself as `string`.

## Cache
This implementation contains a very basic cache that is intended only to be used for short lived DataLoaders (i.e. DataLoaders that only exist for the life of an http request). You may use your own implementation if you want.
Expand Down