Skip to content
Implementation of Facebook's DataLoader in Golang
Go
Branch: master
Clone or download
tonyghita Merge pull request #62 from alex/patch-1
Fixed module name in README
Latest commit 81a076d Nov 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Godeps Adding support for godep Apr 11, 2018
example Fixing package location Sep 25, 2019
.gitignore start of v3 Nov 29, 2017
.travis.yml Fixes #40 - Make sure inMemoryCache work on Go versions before 1.9 May 18, 2018
Gopkg.lock start of v3 Nov 29, 2017
Gopkg.toml start of v3 Nov 29, 2017
LICENSE add name and year to licence Dec 15, 2016
MIGRATE.md Adding migration guide Apr 11, 2018
README.md
TRACE.md opencensus trace example (#58) Jul 26, 2019
cache.go Implement `Key` interface for V5 Jan 4, 2018
codecov.yml Create codecov.yml Dec 20, 2016
dataloader.go Fix typos (#48) Feb 27, 2019
dataloader_test.go Implement `Key` interface for V5 Jan 4, 2018
inMemoryCache.go Fix typos (#48) Feb 27, 2019
inMemoryCache_go19.go Fix typos (#48) Feb 27, 2019
key.go Fix typos (#48) Feb 27, 2019
trace.go Implement `Key` interface for V5 Jan 4, 2018

README.md

DataLoader

GoDoc Build Status

This is an implementation of Facebook's DataLoader in Golang.

Install

go get -u github.com/graph-gophers/dataloader

Usage

// setup batch function
batchFn := func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
  var results []*dataloader.Result
  // do some async work to get data for specified keys
  // append to this list resolved values
  return results
}

// create Loader with an in-memory cache
loader := dataloader.NewBatchedLoader(batchFn)

/**
 * Use loader
 *
 * A thunk is a function returned from a function that is a
 * closure over a value (in this case an interface value and error).
 * When called, it will block until the value is resolved.
 */
thunk := loader.Load(context.TODO(), dataloader.StringKey("key1")) // StringKey is a convenience method that make wraps string to implement `Key` interface
result, err := thunk()
if err != nil {
  // handle data error
}

log.Printf("value: %#v", result)

Don't need/want to use context?

You're welcome to install the v1 version of this library.

Cache

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

it also has a NoCache type that implements the cache interface but all methods are noop. If you do not wish to cache anything.

Examples

There are a few basic examples in the example folder.

You can’t perform that action at this time.