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

Problem with generating dataloader using struct as key type #33

Open
acelot opened this issue Jul 14, 2019 · 6 comments
Open

Problem with generating dataloader using struct as key type #33

acelot opened this issue Jul 14, 2019 · 6 comments

Comments

@acelot
Copy link

acelot commented Jul 14, 2019

Im trying to generate dataloader that uses struct type as a key instead of primitive type.
Im using gqlgen and all my models are generated automatically. My project structure:

internal/
  graphql/
    generated/
      executor_gen.go
      models_gen.go
      ...
    models/
      loaderkey.go

Contents of loaderkey.go:

package models

type LoaderKey struct {
	ID     int
	Fields []string
}

Command to generate dataloader:

cd internal/graphql/generated
go run github.com/vektah/dataloaden \
  AuthorLoader \
  *my.site/project/internal/graphql/models.LoaderKey \
  *my.site/project/internal/graphql/generated.Author

Dataloaded succesfully generates authorloader_gen.go dataloader. But there is a problem:

// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.

package generated

import (
	"sync"
	"time"
)

// AuthorLoaderConfig captures the config to create a new AuthorLoader
type AuthorLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []*LoaderKey) ([]*Author, []error)

	// Wait is how long wait before sending a batch
	Wait time.Duration

	// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
	MaxBatch int
}

<...>

As you can see fetch func using my LoaderKey type, but package is not imported. How to fix this?

@acelot
Copy link
Author

acelot commented Jul 16, 2019

@vektah Any thoughts?

@ddouglas
Copy link

ddouglas commented Oct 13, 2019

@acelot @vektah Any updates on this? Would be interested to use this method to pass arguments for pagination to the query for example

@ddouglas
Copy link

@acelot exported packages cannot contain the word "internal" in the path

https://stackoverflow.com/a/41572137/3689435

@acelot
Copy link
Author

acelot commented Oct 13, 2019

Go is so "simple" and "straightforward" 😆

@ddouglas
Copy link

Yup, just got done testing this. Can confirm that this generated correctly. Thank you for turning me onto this.

package generated

import (
	"sync"
	"time"

	"github.com/ddouglas/monocle"
)

// CorporationAllianceHistory2LoaderConfig captures the config to create a new CorporationAllianceHistory2Loader
type CorporationAllianceHistory2LoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []*monocle.DataloaderSort) ([][]*monocle.CorporationAllianceHistory, []error)

	// Wait is how long wait before sending a batch
	Wait time.Duration

	// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
	MaxBatch int
}

Still not sure how realistic it is, but we shall give it a shot. At least the generated code is correct

@mtatarau90
Copy link

Hi guys, i have the same issue, but in my case every thing is imported and MyStructLoaderConfig is ok, but seems that dataloaden use my struct as key for cache.
// lazily created cache cache map[models.QueryParams][]*models.MyModel
Is there any way to create a different cache key when I use the "generate" command?

Thanks

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

3 participants