Skip to content

Commit

Permalink
Merge 14521ac into 644ade1
Browse files Browse the repository at this point in the history
  • Loading branch information
damour committed Jan 16, 2019
2 parents 644ade1 + 14521ac commit d47dd7e
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 64 deletions.
49 changes: 33 additions & 16 deletions Gopkg.lock

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

15 changes: 12 additions & 3 deletions generator/plugins/dataloader/field_renderers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ func (r *fieldsRenderer) RenderFields(o graphql.OutputObject, ctx graphql.BodyCo
"gqlPkg": func() string {
return ctx.Importer.New(graphql.GraphqlPkgPath)
},
"multierrorPkg": func() string {
return ctx.Importer.New("github.com/hashicorp/go-multierror")
},
"loadersPkg": func() string {
return ctx.Importer.New(r.dataLoader.Pkg)
},
"graphqlOutputLoaderTypeName": func(ctx graphql.BodyContext, dataLoaderName string) string {
dataLoaderConfig := r.dataLoader.Loaders[dataLoaderName]
"graphqlOutputLoaderTypeName": func(ctx graphql.BodyContext, dataLoaderFieldConfig graphql.DataLoaderField) string {
dataLoaderConfig := r.dataLoader.Loaders[dataLoaderFieldConfig.DataLoaderName]

resolver := dataLoaderConfig.OutputGraphqlType

if dataLoaderFieldConfig.KeyFieldSlice {
resolver = graphql.GqlListTypeResolver(resolver)
}

return dataLoaderConfig.OutputGraphqlType(ctx)
return resolver(ctx)
},
}

Expand Down
12 changes: 6 additions & 6 deletions generator/plugins/dataloader/templates.go

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

58 changes: 38 additions & 20 deletions generator/plugins/dataloader/templates/output_object_fields.gohtml
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
{{- /*gotype: github.com/EGT-Ukraine/go2gql/generator/plugins/graphql.RenderFieldsContext*/ -}}
{{ range $field := $.OutputObject.DataLoaderFields -}}
{{$.OutputObject.VariableName}}.AddFieldConfig("{{$field.Name}}", &{{gqlPkg}}.Field{
Name: "{{$field.Name}}",
Description: "",
Type: {{graphqlOutputLoaderTypeName $.ObjectContext $field.DataLoaderName}},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
parent := p.Source.(*{{goType $.OutputObject.GoType}})

loaders := {{loadersPkg}}.GetDataLoadersFromContext(p.Context)

if loaders == nil {
return nil, errors.New("Data loaders not found in context. Call loaders.GetContextWithLoaders")
}

thunk := loaders.{{$field.DataLoaderName}}Loader.LoadThunk(parent.{{$field.NormalizedParentKeyFieldName}})

return func() (interface{}, error) {
return thunk()
}, nil
},
})
{{$.OutputObject.VariableName}}.AddFieldConfig("{{$field.Name}}", &{{gqlPkg}}.Field{
Name: "{{$field.Name}}",
Description: "",
Type: {{graphqlOutputLoaderTypeName $.ObjectContext $field}},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
parent := p.Source.(*{{goType $.OutputObject.GoType}})

loaders := {{loadersPkg}}.GetDataLoadersFromContext(p.Context)

if loaders == nil {
return nil, errors.New("Data loaders not found in context. Call loaders.GetContextWithLoaders")
}

{{if $field.KeyFieldSlice}}
thunk := loaders.{{$field.DataLoaderName}}Loader.LoadAllThunk(parent.{{$field.NormalizedParentKeyFieldName}})

return func() (interface{}, error) {
var loaderErrors error

result, errs := thunk()

for _, err := range errs {
if err != nil {
loaderErrors = {{multierrorPkg}}.Append(loaderErrors, err)
}
}

return result, loaderErrors
}, nil
{{else}}
thunk := loaders.{{$field.DataLoaderName}}Loader.LoadThunk(parent.{{$field.NormalizedParentKeyFieldName}})

return func() (interface{}, error) {
return thunk()
}, nil
{{end}}
},
})
{{ end -}}
1 change: 1 addition & 0 deletions generator/plugins/graphql/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type DataLoaderField struct {
Name string
Type string
ParentKeyFieldName string
KeyFieldSlice bool
NormalizedParentKeyFieldName string
DataLoaderName string
}
Expand Down
Loading

0 comments on commit d47dd7e

Please sign in to comment.