vektah / dataloaden

go generate based DataLoader
MIT License
530 stars 79 forks source link

Problem with generating dataloader using struct as key type #33

Open acelot opened 5 years ago

acelot commented 5 years ago

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 commented 5 years ago

@vektah Any thoughts?

ddouglas commented 5 years ago

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

ddouglas commented 5 years ago

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

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

acelot commented 5 years ago

Go is so "simple" and "straightforward" :laughing:

ddouglas commented 5 years ago

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 commented 4 years ago

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