Skip to content

Commit ab5318f

Browse files
authored
Merge pull request #106 from abekoh/update-readme
Update docs for v7
2 parents a33c0cd + a761971 commit ab5318f

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

MIGRATE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ The preferred import method includes the major version tag.
9797
```go
9898
import "github.com/graph-gophers/dataloader/v6"
9999
```
100+
101+
## Upgrade from v6 to v7
102+
103+
[Generics](https://go.dev/doc/tutorial/generics) support has been added.
104+
With this update, you can now write more type-safe code.
105+
106+
Use the major version tag in the import path.
107+
108+
```go
109+
import "github.com/graph-gophers/dataloader/v7"
110+
```

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ This is an implementation of [Facebook's DataLoader](https://github.com/facebook
1111
```go
1212
// setup batch function - the first Context passed to the Loader's Load
1313
// function will be provided when the batch function is called.
14-
batchFn := func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
15-
var results []*dataloader.Result
14+
// this function is registered with the Loader, and the key and value are fixed using generics.
15+
batchFn := func(ctx context.Context, keys []int) []*dataloader.Result[*User] {
16+
var results []*dataloader.Result[*User]
1617
// do some async work to get data for specified keys
1718
// append to this list resolved values
1819
return results
@@ -32,7 +33,7 @@ loader := dataloader.NewBatchedLoader(batchFn)
3233
* The first context passed to Load is the object that will be passed
3334
* to the batch function.
3435
*/
35-
thunk := loader.Load(context.TODO(), dataloader.StringKey("key1")) // StringKey is a convenience method that make wraps string to implement `Key` interface
36+
thunk := loader.Load(context.TODO(), 5)
3637
result, err := thunk()
3738
if err != nil {
3839
// handle data error
@@ -51,3 +52,7 @@ This implementation contains a very basic cache that is intended only to be used
5152
5253
## Examples
5354
There are a few basic examples in the example folder.
55+
56+
## See also
57+
- [TRACE](TRACE.md)
58+
- [MIGRATE](MIGRATE.md)

TRACE.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,72 @@ import (
1212
"context"
1313
"strings"
1414

15-
exp "go.opencensus.io/examples/exporter"
16-
"github.com/nicksrandall/dataloader"
15+
"github.com/graph-gophers/dataloader/v7"
16+
exp "go.opencensus.io/examples/exporter"
1717
"go.opencensus.io/trace"
1818
)
1919

20+
type User struct {
21+
ID string
22+
}
23+
2024
// OpenCensusTracer Tracer implements a tracer that can be used with the Open Tracing standard.
2125
type OpenCensusTracer struct{}
2226

2327
// TraceLoad will trace a call to dataloader.LoadMany with Open Tracing
24-
func (OpenCensusTracer) TraceLoad(ctx context.Context, key dataloader.Key) (context.Context, dataloader.TraceLoadFinishFunc) {
28+
func (OpenCensusTracer) TraceLoad(ctx context.Context, key string) (context.Context, dataloader.TraceLoadFinishFunc[*User]) {
2529
cCtx, cSpan := trace.StartSpan(ctx, "Dataloader: load")
2630
cSpan.AddAttributes(
27-
trace.StringAttribute("dataloader.key", key.String()),
31+
trace.StringAttribute("dataloader.key", key),
2832
)
29-
return cCtx, func(thunk dataloader.Thunk) {
33+
return cCtx, func(thunk dataloader.Thunk[*User]) {
3034
// TODO: is there anything we should do with the results?
3135
cSpan.End()
3236
}
3337
}
3438

3539
// TraceLoadMany will trace a call to dataloader.LoadMany with Open Tracing
36-
func (OpenCensusTracer) TraceLoadMany(ctx context.Context, keys dataloader.Keys) (context.Context, dataloader.TraceLoadManyFinishFunc) {
40+
func (OpenCensusTracer) TraceLoadMany(ctx context.Context, keys []string) (context.Context, dataloader.TraceLoadManyFinishFunc[*User]) {
3741
cCtx, cSpan := trace.StartSpan(ctx, "Dataloader: loadmany")
3842
cSpan.AddAttributes(
39-
trace.StringAttribute("dataloader.keys", strings.Join(keys.Keys(), ",")),
43+
trace.StringAttribute("dataloader.keys", strings.Join(keys, ",")),
4044
)
41-
return cCtx, func(thunk dataloader.ThunkMany) {
45+
return cCtx, func(thunk dataloader.ThunkMany[*User]) {
4246
// TODO: is there anything we should do with the results?
4347
cSpan.End()
4448
}
4549
}
4650

4751
// TraceBatch will trace a call to dataloader.LoadMany with Open Tracing
48-
func (OpenCensusTracer) TraceBatch(ctx context.Context, keys dataloader.Keys) (context.Context, dataloader.TraceBatchFinishFunc) {
52+
func (OpenCensusTracer) TraceBatch(ctx context.Context, keys []string) (context.Context, dataloader.TraceBatchFinishFunc[*User]) {
4953
cCtx, cSpan := trace.StartSpan(ctx, "Dataloader: batch")
5054
cSpan.AddAttributes(
51-
trace.StringAttribute("dataloader.keys", strings.Join(keys.Keys(), ",")),
55+
trace.StringAttribute("dataloader.keys", strings.Join(keys, ",")),
5256
)
53-
return cCtx, func(results []*dataloader.Result) {
57+
return cCtx, func(results []*dataloader.Result[*User]) {
5458
// TODO: is there anything we should do with the results?
5559
cSpan.End()
5660
}
5761
}
5862

59-
func batchFunc(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
60-
// ...loader logic goes here
63+
func batchFunc(ctx context.Context, keys []string) []*dataloader.Result[*User] {
64+
// ...loader logic goes here
6165
}
6266

63-
func main(){
64-
//initialize an example exporter that just logs to the console
65-
trace.ApplyConfig(trace.Config{
67+
func main() {
68+
//initialize an example exporter that just logs to the console
69+
trace.ApplyConfig(trace.Config{
6670
DefaultSampler: trace.AlwaysSample(),
6771
})
68-
trace.RegisterExporter(&exp.PrintExporter{})
69-
// initialize the dataloader with your new tracer backend
70-
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithTracer(OpenCensusTracer{}))
71-
// initialize a context since it's not receiving one from anywhere else.
72-
ctx, span := trace.StartSpan(context.TODO(), "Span Name")
73-
defer span.End()
74-
// request from the dataloader as usual
75-
value, err := loader.Load(ctx, dataloader.StringKey(SomeID))()
76-
// ...
72+
trace.RegisterExporter(&exp.PrintExporter{})
73+
// initialize the dataloader with your new tracer backend
74+
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithTracer[string, *User](OpenCensusTracer{}))
75+
// initialize a context since it's not receiving one from anywhere else.
76+
ctx, span := trace.StartSpan(context.TODO(), "Span Name")
77+
defer span.End()
78+
// request from the dataloader as usual
79+
value, err := loader.Load(ctx, SomeID)()
80+
// ...
7781
}
7882
```
7983

0 commit comments

Comments
 (0)