Skip to content

Commit

Permalink
Merge pull request #44 from EGT-Ukraine/feature/custom-dataloader-name
Browse files Browse the repository at this point in the history
Use messageGraphQL names for names of dataloaders
  • Loading branch information
Garanzha Dmitriy authored May 22, 2019
2 parents 69037a9 + 1a9d8c6 commit 5540281
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
12 changes: 6 additions & 6 deletions generator/plugins/dataloader/data_loader_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func (p *LoaderGenerator) GenerateDataLoaders() error {
}

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

return nil
}

func (p *LoaderGenerator) generateLoaders(requestGoType graphql.GoType, responseGoType graphql.GoType, slice bool) (rerr error) {
func (p *LoaderGenerator) generateLoaders(outputGraphqlTypeName string, requestGoType graphql.GoType, responseGoType graphql.GoType, slice bool) (rerr error) {
keyType := requestGoType.ElemType.Kind.String()

var typeName string
Expand All @@ -81,7 +81,7 @@ func (p *LoaderGenerator) generateLoaders(requestGoType graphql.GoType, response
}
}()

if err := generator.Generate(typeName, keyType, slice, true, p.dataLoader.OutputPath); err != nil {
if err := generator.Generate(outputGraphqlTypeName, typeName, keyType, slice, true, p.dataLoader.OutputPath); err != nil {
return errors.Wrapf(err, "Failed to generate loader for '%s'", typeName)
}

Expand Down Expand Up @@ -156,14 +156,14 @@ func (p *LoaderGenerator) generateBody() ([]byte, error) {

responseGoType := dataLoaderModel.OutputGoType

loaderTypeName := responseGoType.ElemType.Name
loaderTypeName := dataLoaderModel.OutputGraphqlTypeName

if dataLoaderModel.Slice {
loaderTypeName = responseGoType.ElemType.ElemType.Name + "Slice"
loaderTypeName += "Slice"
}

loaders = append(loaders, Loader{
LoaderTypeName: loaderTypeName,
LoaderTypeName: loaderTypeName + "Loader",
Service: *service,
FetchCode: dataLoaderModel.FetchCode(p.importer),
RequestGoType: requestGoType,
Expand Down
19 changes: 10 additions & 9 deletions generator/plugins/dataloader/dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ 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
Slice bool
Name string
WaitDuration time.Duration
Service *Service
Method *graphql.Method
InputGoType graphql.GoType
OutputGoType graphql.GoType
OutputGraphqlType graphql.TypeResolver
OutputGraphqlTypeName string
FetchCode func(importer *importer.Importer) string
Slice bool
}

func (p *Plugin) createDataLoader(config *DataLoadersConfig, vendorPath string) (*DataLoader, error) {
Expand Down
8 changes: 4 additions & 4 deletions generator/plugins/dataloader/templates.go

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

6 changes: 3 additions & 3 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.Name}}Loader {{$loader.LoaderTypeName}}Loader
{{$loader.Name}}Loader {{$loader.LoaderTypeName}}
{{end -}}
}

Expand All @@ -27,8 +27,8 @@ func GetContextWithLoaders(ctx context.Context, apiClients LoaderClients) contex
}

{{range $loader := $.Loaders -}}
func create{{$loader.Name}}(ctx context.Context, client {{goType $loader.Service.CallInterface}}) {{$loader.LoaderTypeName}}Loader {
return {{$loader.LoaderTypeName}}Loader{
func create{{$loader.Name}}(ctx context.Context, client {{goType $loader.Service.CallInterface}}) {{$loader.LoaderTypeName}} {
return {{$loader.LoaderTypeName}}{
fetch: func(keys {{goType $loader.RequestGoType}}) ([]{{goType $loader.ResponseGoType}}, []error) {
{{$loader.FetchCode}}
},
Expand Down
2 changes: 1 addition & 1 deletion generator/plugins/graphql/templates.go

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

8 changes: 8 additions & 0 deletions generator/plugins/proto2gql/dataloaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func (g Proto2GraphQL) registerMethodDataLoader(name string, cfg DataLoaderConfi
normalResultField := resultField.(*parser.NormalField)
resultMessage := normalResultField.Type.(*parser.Message)

resultMessageFile, err := g.parsedFile(resultMessage.File())
if err != nil {
return errors.Wrap(err, "failed to get result message file")
}


matchField, err := messageFieldByPath(resultField.GetType().(*parser.Message), cfg.MatchField)
if err != nil {
return errors.Wrap(err, "failed to get match field")
Expand All @@ -44,6 +50,7 @@ func (g Proto2GraphQL) registerMethodDataLoader(name string, cfg DataLoaderConfi
if err != nil {
return errors.Wrap(err, "failed to get result field go type")
}

outputMsgTypeFile, err := g.parsedFile(method.OutputMessage.File())
if err != nil {
return errors.Wrap(err, "failed to resolve file type file")
Expand Down Expand Up @@ -79,6 +86,7 @@ func (g Proto2GraphQL) registerMethodDataLoader(name string, cfg DataLoaderConfi
}

dataLoaderProvider := dataloader.LoaderModel{
OutputGraphqlTypeName: g.outputMessageGraphQLName(resultMessageFile, resultMessage),
Service: &dataloader.Service{
Name: g.serviceName(serviceConfig, method.Service),
CallInterface: g.serviceCallInterface(file, method.Service.Name),
Expand Down
10 changes: 5 additions & 5 deletions generator/plugins/swagger2gql/templates.go

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

0 comments on commit 5540281

Please sign in to comment.