Skip to content
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

Dataloaders documentation example does not work without extra steps #1443

Open
greg-1-anderson opened this issue Jan 21, 2021 · 1 comment
Open

Comments

@greg-1-anderson
Copy link

What happened?

Followed the instructions in the documentation on Dataloaders and received an inscrutable error message:

go run github.com/vektah/dataloaden UserLoader string *github.com/org/project/pkg/graph/model.SiteInfo
unable to gofmt: /path/project/pkg/dataloader/userloader_gen.go:6:1: expected 'IDENT', found 'import'
exit status 2

What's worse, the code generator cleaned up after itself, giving little info about what might have gone wrong (userloader_gen.go does not exist).

What did you expect?

Documentation should contain correct steps to generate a data loader.

Recommended solution

Update the documentation to contain the missing steps described in vektah/dataloaden#35

I created a new file loaders.go containing only a package statement, and then the instructions worked as presented. There are a number of ways this could be addressed; a one-line fix is presented below:

go get github.com/vektah/dataloaden
mkdir dataloader
cd dataloader
echo 'package dataloader' > loaders.go
go run github.com/vektah/dataloaden UserLoader int *gqlgen-tutorials/dataloader/graph/model.User

Then, instruct users to populate the rest of loaders.go with contents similar to the example below after Dataloaden has generated the data loader.

@greg-1-anderson
Copy link
Author

Also, tangentially related, to avoid a lint error in the example I replaced:

const loadersKey = "dataloaders"

With:

type key int

const (
       loadersKey key = iota
)

Without this change, the linter reports:

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
		ctx := context.WithValue(r.Context(), loadersKey, &Loaders{
		                                      ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant