Skip to content

Commit a19b9a6

Browse files
committed
Rework the type system
1 parent 0f86e10 commit a19b9a6

File tree

15 files changed

+431
-138
lines changed

15 files changed

+431
-138
lines changed

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ get used.
1010

1111
#### Getting started
1212

13-
First grab it:
13+
From inside the package you want to have the dataloader in:
1414
```bash
15-
go get -u github.com/vektah/dataloaden
15+
go run github.com/vektah/dataloaden UserLoader string *github.com/dataloaden/example.User
1616
```
1717

18-
then from inside the package you want to have the dataloader in:
19-
```bash
20-
dataloaden github.com/dataloaden/example.User
21-
```
18+
This will generate a dataloader called `UserLoader` that looks up `*github.com/dataloaden/example.User`'s objects
19+
based on a `string` key.
2220

2321
In another file in the same package, create the constructor method:
2422
```go
25-
func NewLoader() *UserLoader {
23+
func NewUserLoader() *UserLoader {
2624
return &UserLoader{
2725
wait: 2 * time.Millisecond,
2826
maxBatch: 100,
@@ -41,7 +39,7 @@ func NewLoader() *UserLoader {
4139

4240
Then wherever you want to call the dataloader
4341
```go
44-
loader := NewLoader()
42+
loader := NewUserLoader()
4543

4644
user, err := loader.Load("123")
4745
```
@@ -51,13 +49,14 @@ function once. It also caches values and wont request duplicates in a batch.
5149

5250
#### Returning Slices
5351

54-
You may want to generate a dataloader that returns slices instead of single values. This can be done using the `-slice` flag:
52+
You may want to generate a dataloader that returns slices instead of single values. Both key and value types can be a
53+
simple go type expression:
5554

5655
```bash
57-
dataloaden -slice github.com/dataloaden/example.User
56+
go run github.com/vektah/dataloaden UserSliceLoader string []*github.com/dataloaden/example.User
5857
```
5958

60-
Now each key is expected to return a slice of values and the `fetch` function has the return type `[][]User`.
59+
Now each key is expected to return a slice of values and the `fetch` function has the return type `[][]*User`.
6160

6261
#### Using with go modules
6362

dataloaden.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
package main
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76

87
"github.com/vektah/dataloaden/pkg/generator"
98
)
109

1110
func main() {
12-
keyType := flag.String("keys", "int", "what type should the keys be")
13-
slice := flag.Bool("slice", false, "this dataloader will return slices")
14-
15-
flag.Parse()
16-
17-
if flag.NArg() != 1 {
18-
flag.Usage()
11+
if len(os.Args) != 4 {
12+
fmt.Println("usage: name keyType valueType")
13+
fmt.Println(" example:")
14+
fmt.Println(" dataloaden 'UserLoader int []*github.com/my/package.User'")
1915
os.Exit(1)
2016
}
2117

2218
wd, err := os.Getwd()
23-
2419
if err != nil {
2520
fmt.Fprintln(os.Stderr, err.Error())
2621
os.Exit(2)
2722
}
2823

29-
if err := generator.Generate(flag.Arg(0), *keyType, *slice, wd); err != nil {
24+
if err := generator.Generate(os.Args[1], os.Args[2], os.Args[3], wd); err != nil {
3025
fmt.Fprintln(os.Stderr, err.Error())
3126
os.Exit(2)
3227
}

example/pkgname/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package differentpkg
22

3-
//go:generate go run github.com/vektah/dataloaden -keys string github.com/vektah/dataloaden/example.User
3+
//go:generate go run github.com/vektah/dataloaden UserLoader string *github.com/vektah/dataloaden/example.User

example/pkgname/userloader_gen.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/slice/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate go run github.com/vektah/dataloaden -keys int -slice github.com/vektah/dataloaden/example.User
1+
//go:generate go run github.com/vektah/dataloaden UserSliceLoader int []github.com/vektah/dataloaden/example.User
22

33
package slice
44

example/slice/usersliceloader_gen.go

Lines changed: 14 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)