Skip to content

Commit

Permalink
Move DataLoaderConfig to swagger2gql plugin. Dataloader model now rec…
Browse files Browse the repository at this point in the history
…eive name and WaitDuration instead of Config
  • Loading branch information
saturn4er committed Feb 1, 2019
1 parent b313ae6 commit 3f5c505
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 29 deletions.
8 changes: 0 additions & 8 deletions generator/plugins/dataloader/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package dataloader

import "time"

type DataLoadersConfig struct {
OutputPath string `mapstructure:"output_path"`
}

type ProviderConfig struct {
Name string `mapstructure:"name"`
WaitDuration time.Duration `mapstructure:"wait_duration"`
Slice bool
}

type FieldConfig struct {
FieldName string `mapstructure:"field_name"`
KeyFieldName string `mapstructure:"key_field_name"`
Expand Down
8 changes: 5 additions & 3 deletions generator/plugins/dataloader/data_loader_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type Loader struct {
RequestGoType graphql.GoType
ResponseGoType graphql.GoType
OutputGraphqlType graphql.TypeResolver
Config ProviderConfig
Name string
WaitDuration time.Duration
}

type LoaderGenerator struct {
Expand All @@ -57,7 +58,7 @@ func (p *LoaderGenerator) GenerateDataLoaders() error {

for _, dataLoader := range p.dataLoader.Loaders {
if err := p.generateLoaders(dataLoader.InputGoType, dataLoader.OutputGoType, dataLoader.Slice); err != nil {
return errors.Wrapf(err, "failed to generate %s data loader", dataLoader.Config.Name)
return errors.Wrapf(err, "failed to generate %s data loader", dataLoader.Name)
}
}

Expand Down Expand Up @@ -167,7 +168,8 @@ func (p *LoaderGenerator) generateBody() ([]byte, error) {
FetchCode: dataLoaderModel.FetchCode(p.importer),
RequestGoType: requestGoType,
ResponseGoType: responseGoType,
Config: dataLoaderModel.Config,
Name: dataLoaderModel.Name,
WaitDuration: dataLoaderModel.WaitDuration,
})
}

Expand Down
5 changes: 4 additions & 1 deletion generator/plugins/dataloader/dataloader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dataloader

import (
"time"

"github.com/pkg/errors"

"github.com/EGT-Ukraine/go2gql/generator/plugins/graphql"
Expand All @@ -19,13 +21,14 @@ type Service struct {
}

type LoaderModel struct {
Name string
WaitDuration time.Duration
Service *Service
Method *graphql.Method
InputGoType graphql.GoType
OutputGoType graphql.GoType
OutputGraphqlType graphql.TypeResolver
FetchCode func(importer *importer.Importer) string
Config ProviderConfig
Slice bool
}

Expand Down
2 changes: 1 addition & 1 deletion generator/plugins/dataloader/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Plugin struct {
}

func (p *Plugin) AddLoader(loader LoaderModel) {
p.loaders[loader.Config.Name] = loader
p.loaders[loader.Name] = loader
}

func (p *Plugin) Prepare() error {
Expand Down
4 changes: 2 additions & 2 deletions generator/plugins/dataloader/templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions generator/plugins/dataloader/templates/loaders_body.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type LoaderClients interface {

type DataLoaders struct {
{{range $loader := $.Loaders -}}
{{$loader.Config.Name}}Loader {{$loader.LoaderTypeName}}Loader
{{$loader.Name}}Loader {{$loader.LoaderTypeName}}Loader
{{end -}}
}

Expand All @@ -19,20 +19,20 @@ var dataLoadersContextKey = dataLoadersContextKeyType{}
func GetContextWithLoaders(ctx context.Context, apiClients LoaderClients) context.Context {
dataLoaders := &DataLoaders{
{{range $loader := $.Loaders -}}
{{$loader.Config.Name}}Loader: create{{$loader.Config.Name}}(ctx, apiClients.Get{{$loader.Service.Name}}Client()),
{{$loader.Name}}Loader: create{{$loader.Name}}(ctx, apiClients.Get{{$loader.Service.Name}}Client()),
{{end -}}
}

return context.WithValue(ctx, dataLoadersContextKey, dataLoaders)
}

{{range $loader := $.Loaders -}}
func create{{$loader.Config.Name}}(ctx context.Context, client {{goType $loader.Service.CallInterface}}) {{$loader.LoaderTypeName}}Loader {
func create{{$loader.Name}}(ctx context.Context, client {{goType $loader.Service.CallInterface}}) {{$loader.LoaderTypeName}}Loader {
return {{$loader.LoaderTypeName}}Loader{
fetch: func(keys {{goType $loader.RequestGoType}}) ([]{{goType $loader.ResponseGoType}}, []error) {
{{$loader.FetchCode}}
},
wait: {{duration $loader.Config.WaitDuration}},
wait: {{duration $loader.WaitDuration}},
}
}
{{end -}}
Expand Down
8 changes: 3 additions & 5 deletions generator/plugins/proto2gql/dataloaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ func (g Proto2GraphQL) registerMethodDataLoader(name string, cfg DataLoaderConfi
},
OutputGoType: responseGoType,
OutputGraphqlType: dataLoaderOutType,
Config: dataloader.ProviderConfig{
Name: name,
WaitDuration: cfg.WaitDuration,
},
Slice: cfg.Type == DataLoaderType1ToN,
Name: name,
WaitDuration: cfg.WaitDuration,
Slice: cfg.Type == DataLoaderType1ToN,
}

g.DataLoaderPlugin.AddLoader(dataLoaderProvider)
Expand Down
14 changes: 11 additions & 3 deletions generator/plugins/swagger2gql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package swagger2gql

import (
"regexp"
"time"

"github.com/pkg/errors"

Expand All @@ -18,10 +19,17 @@ type ObjectConfig struct {
}

type MethodConfig struct {
Alias string `mapstructure:"alias"`
RequestType string `mapstructure:"request_type"` // QUERY | MUTATION
DataLoaderProvider dataloader.ProviderConfig `mapstructure:"data_loader_provider"`
Alias string `mapstructure:"alias"`
RequestType string `mapstructure:"request_type"` // QUERY | MUTATION
DataLoaderProvider ProviderConfig `mapstructure:"data_loader_provider"`
}

type ProviderConfig struct {
Name string `mapstructure:"name"`
WaitDuration time.Duration `mapstructure:"wait_duration"`
Slice bool
}

type TagConfig struct {
ClientGoPackage string `mapstructure:"client_go_package"`
ServiceName string `mapstructure:"service_name"`
Expand Down
3 changes: 2 additions & 1 deletion generator/plugins/swagger2gql/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/EGT-Ukraine/go2gql/generator"
"github.com/EGT-Ukraine/go2gql/generator/plugins/dataloader"
"github.com/EGT-Ukraine/go2gql/generator/plugins/graphql"
"github.com/EGT-Ukraine/go2gql/generator/plugins/graphql/lib/pluginconfig"
"github.com/EGT-Ukraine/go2gql/generator/plugins/swagger2gql/parser"
)

Expand Down Expand Up @@ -60,7 +61,7 @@ func (p *Plugin) Init(config *generator.GenerateConfig, plugins []generator.Plug
func (p *Plugin) parseImports() error {
for _, pluginsConfigsImports := range p.generateConfig.PluginsConfigsImports {
configs := new([]*SwaggerFileConfig)
if err := mapstructure.Decode(pluginsConfigsImports.PluginsConfigs[PluginImportConfigKey], configs); err != nil {
if err := pluginconfig.Decode(pluginsConfigsImports.PluginsConfigs[PluginImportConfigKey], configs); err != nil {
return errors.Wrap(err, "failed to decode config")
}

Expand Down
3 changes: 2 additions & 1 deletion generator/plugins/swagger2gql/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func (p *Plugin) addDataLoaderProvider(
}

dataLoaderProvider := dataloader.LoaderModel{
Name: dataLoaderProviderConfig.Name,
WaitDuration: dataLoaderProviderConfig.WaitDuration,
Service: &dataloader.Service{
Name: p.tagName(tag, &tagCfg),
CallInterface: p.serviceCallInterface(&tagCfg),
Expand Down Expand Up @@ -193,7 +195,6 @@ func (p *Plugin) addDataLoaderProvider(
InputGoType: inputArgumentGoType,
OutputGoType: responseGoType,
OutputGraphqlType: dataLoaderOutType,
Config: dataLoaderProviderConfig,
Slice: dataLoaderProviderConfig.Slice,
}

Expand Down

0 comments on commit 3f5c505

Please sign in to comment.