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

Allow forcing resolvers on generated types #190

Merged
merged 1 commit into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func Generate(cfg Config) error {
return err
}
for _, model := range modelsBuild.Models {
cfg.Models[model.GQLType] = TypeMapEntry{
Model: cfg.Model.ImportPath() + "." + model.GoType,
}
modelCfg := cfg.Models[model.GQLType]
modelCfg.Model = cfg.Model.ImportPath() + "." + model.GoType
cfg.Models[model.GQLType] = modelCfg
}

for _, enum := range modelsBuild.Enums {
cfg.Models[enum.GQLType] = TypeMapEntry{
Model: cfg.Model.ImportPath() + "." + enum.GoType,
}
modelCfg := cfg.Models[enum.GQLType]
modelCfg.Model = cfg.Model.ImportPath() + "." + enum.GoType
cfg.Models[enum.GQLType] = modelCfg
}
}

Expand Down
3 changes: 0 additions & 3 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ func (tm TypeMap) Exists(typeName string) bool {

func (tm TypeMap) Check() error {
for typeName, entry := range tm {
if entry.Model == "" {
return fmt.Errorf("model %s: entityPath is not defined", typeName)
}
if strings.LastIndex(entry.Model, ".") < strings.LastIndex(entry.Model, "/") {
return fmt.Errorf("model %s: invalid type specifier \"%s\" - you need to specify a struct to map to", typeName, entry.Model)
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

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

27 changes: 13 additions & 14 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,7 @@
defer rctx.Pop()
{{- end }}

{{- if $field.GoVarName }}
res := obj.{{$field.GoVarName}}
{{- else if $field.GoMethodName }}
{{- if $field.NoErr }}
res := {{$field.GoMethodName}}({{ $field.CallArgs }})
{{- else }}
res, err := {{$field.GoMethodName}}({{ $field.CallArgs }})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
{{- end }}
{{- else }}

{{- if $field.IsResolver }}
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
return ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})
})
Expand All @@ -72,6 +59,18 @@
return graphql.Null
}
res := resTmp.({{$field.Signature}})
{{- else if $field.GoVarName }}
res := obj.{{$field.GoVarName}}
{{- else if $field.GoMethodName }}
{{- if $field.NoErr }}
res := {{$field.GoMethodName}}({{ $field.CallArgs }})
{{- else }}
res, err := {{$field.GoMethodName}}({{ $field.CallArgs }})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
{{- end }}
{{- end }}
{{ $field.WriteJson }}
{{- if $field.IsConcurrent }}
Expand Down
17 changes: 6 additions & 11 deletions codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ func (cfg *Config) buildNamedTypes() NamedTypes {
for _, schemaType := range cfg.schema.Types {
t := namedTypeFromSchema(schemaType)

userType := ""
if userEntry, ok := cfg.Models[t.GQLType]; ok {
userType = userEntry.Model
}
t.IsUserDefined = userType != ""
if userType == "" && t.IsScalar {
userType = "github.com/vektah/gqlgen/graphql.String"
}

if userType != "" {
t.Package, t.GoType = pkgAndType(userType)
if userEntry, ok := cfg.Models[t.GQLType]; ok && userEntry.Model != "" {
t.IsUserDefined = true
t.Package, t.GoType = pkgAndType(userEntry.Model)
} else if t.IsScalar {
t.Package = "github.com/vektah/gqlgen/graphql"
t.GoType = "String"
}

types[t.GQLType] = t
Expand Down
1 change: 0 additions & 1 deletion example/chat/generated.go

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

6 changes: 3 additions & 3 deletions example/starwars/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ models:
model: github.com/vektah/gqlgen/example/starwars.Droid
FriendsConnection:
model: github.com/vektah/gqlgen/example/starwars.FriendsConnection
FriendsEdge:
model: github.com/vektah/gqlgen/example/starwars.FriendsEdge
Human:
model: github.com/vektah/gqlgen/example/starwars.Human
Review:
model: github.com/vektah/gqlgen/example/starwars.Review
ReviewInput:
model: github.com/vektah/gqlgen/example/starwars.Review
Starship:
model: github.com/vektah/gqlgen/example/starwars.Starship
fields:
length:
resolver: true
46 changes: 37 additions & 9 deletions example/starwars/generated.go

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

Loading