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

[Question] How to specify first, last, after, before etc with dataloader #44

Open
touyu opened this issue Feb 14, 2020 · 2 comments
Open

Comments

@touyu
Copy link

touyu commented Feb 14, 2020

For example, if you request with GitHub GraphQL API with the following query, the result will be returned considering first and after.

{
  viewer {
    repositories(first: 30) {
      nodes {
        issues(first: 30, after: "Y3Vyc29yOnYyOpHOAp96sw==") {
          nodes {
            title
          }
        }
      }
    }
  }
}

Probably, if I implement something similar api with gqlgen, I should use dataloader for issues.
However I can only pass keys to dataloader.
How should I pass first, after, etc. information?

@calentang
Copy link

i have the same problem,are you resolved it?

@touyu
Copy link
Author

touyu commented Oct 29, 2020

I came up with one solution.

Generate code

$ go run github.com/vektah/dataloaden IssueConnectionLoader Param *model_path/model.IssueConnection
type Param struct {
  Key uint
  First *int
  After *stirng
}

Impl Dataloader

Loader := IssueConnectionLoader{
	maxBatch: 100,
	wait:     1 * time.Millisecond,
	fetch: func(ids []Param) ([]*model.IssueConnection, []error) {
              // You can use `first` or `last` param etc...
        },
 }

Impl Resolver

func (r *repositoryResolver) IssueConnection(ctx context.Context, obj *model.Repository, first *int) (*model.IssueConnection, error) {
	param := dataloader.Param{
		Key:   obj.id,
		First: first,
	}
	return dataloader.For(ctx).Loader.Load(param)
}

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

2 participants