Skip to content

Commit

Permalink
Merge pull request #28 from vektah/rework-types
Browse files Browse the repository at this point in the history
Rework the type system
  • Loading branch information
vektah authored May 15, 2019
2 parents 0f86e10 + a19b9a6 commit 9d6b85c
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 138 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ get used.

#### Getting started

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

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

In another file in the same package, create the constructor method:
```go
func NewLoader() *UserLoader {
func NewUserLoader() *UserLoader {
return &UserLoader{
wait: 2 * time.Millisecond,
maxBatch: 100,
Expand All @@ -41,7 +39,7 @@ func NewLoader() *UserLoader {

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

user, err := loader.Load("123")
```
Expand All @@ -51,13 +49,14 @@ 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:
You may want to generate a dataloader that returns slices instead of single values. Both key and value types can be a
simple go type expression:

```bash
dataloaden -slice github.com/dataloaden/example.User
go run github.com/vektah/dataloaden UserSliceLoader string []*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`.
Now each key is expected to return a slice of values and the `fetch` function has the return type `[][]*User`.

#### Using with go modules

Expand Down
15 changes: 5 additions & 10 deletions dataloaden.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/vektah/dataloaden/pkg/generator"
)

func main() {
keyType := flag.String("keys", "int", "what type should the keys be")
slice := flag.Bool("slice", false, "this dataloader will return slices")

flag.Parse()

if flag.NArg() != 1 {
flag.Usage()
if len(os.Args) != 4 {
fmt.Println("usage: name keyType valueType")
fmt.Println(" example:")
fmt.Println(" dataloaden 'UserLoader int []*github.com/my/package.User'")
os.Exit(1)
}

wd, err := os.Getwd()

if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
}

if err := generator.Generate(flag.Arg(0), *keyType, *slice, wd); err != nil {
if err := generator.Generate(os.Args[1], os.Args[2], os.Args[3], wd); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
}
Expand Down
2 changes: 1 addition & 1 deletion example/pkgname/user.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package differentpkg

//go:generate go run github.com/vektah/dataloaden -keys string github.com/vektah/dataloaden/example.User
//go:generate go run github.com/vektah/dataloaden UserLoader string *github.com/vektah/dataloaden/example.User
18 changes: 9 additions & 9 deletions example/pkgname/userloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/slice/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run github.com/vektah/dataloaden -keys int -slice github.com/vektah/dataloaden/example.User
//go:generate go run github.com/vektah/dataloaden UserSliceLoader int []github.com/vektah/dataloaden/example.User

package slice

Expand Down
24 changes: 14 additions & 10 deletions example/slice/usersliceloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d6b85c

Please sign in to comment.