dataloaden icon indicating copy to clipboard operation
dataloaden copied to clipboard

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

Open touyu opened this issue 5 years ago • 2 comments

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?

touyu avatar Feb 14 '20 08:02 touyu

i have the same problem,are you resolved it?

calentang avatar Sep 14 '20 02:09 calentang

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)
}

touyu avatar Oct 29 '20 10:10 touyu