Skip to content

Commit

Permalink
Merge pull request #14 from EGT-Ukraine/feature/remove_mutations_serv…
Browse files Browse the repository at this point in the history
…ice_name

Remove mutations service name.
  • Loading branch information
damour authored Nov 1, 2018
2 parents 2971f68 + b5e566d commit 7fef65b
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 206 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ proto2gql:
- google/protobuf/timestamp.proto: "github.com/gogo/protobuf/protobuf/google/protobuf/timestamp.proto"
services: # services settings
"serviceName": # service name
queries_service_name: "someAlias" # queries service name alias
mutations_service_name: "someAliasMutations" # mutations service name alias
service_name: "someAlias" # service name alias
methods:
"methodName": # method name
alias: "methodAlias"
Expand Down Expand Up @@ -181,8 +180,7 @@ swagger2gql:
tags: # tags settings
"some-swagger-tag": # tag name
client_go_package: "github.com/myproject/service1/client/some_swagger_tag/client" # go client package
queries_service_name: "SomeSwaggerTag" # queries service name alias
mutations_service_name: "SomeSwaggerTagMutations" # mutations service name alias
service_name: "SomeSwaggerTag" # service name alias
methods: # tag method settings
"/somes": # request path
get: # request method (get/post/put/options...)
Expand Down Expand Up @@ -239,11 +237,11 @@ func (p plugin) Generate() error {
defer file.Close()
for _, typesFile := range p.gqlPlugin.Types() {
for _, service := range typesFile.Services {
if len(service.Methods) == 0 {
if len(service.QueryMethods) == 0 {
continue
}
file.WriteString(service.Name + ":\n")
for _, method := range service.Methods {
for _, method := range service.QueryMethods {
file.WriteString(" " + method.Name + ":\n")
}
}
Expand Down Expand Up @@ -273,3 +271,15 @@ Use `queries_service_name` & `mutations_service_name` instead.
`alias` config field for services is removed.

Use `queries_service_name` & `mutations_service_name` instead.

## UPGRADE FROM 2.x to 3.0

### Swagger plugin

`queries_service_name` & `mutations_service_name` config fields is removed.
Use `service_name` instead.

### Proto plugin

`queries_service_name` & `mutations_service_name` config fields is removed.
Use `service_name` instead.
4 changes: 2 additions & 2 deletions example/simple_plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func (p plugin) Generate() error {
defer file.Close()
for _, typesFile := range p.gqlPlugin.Types() {
for _, service := range typesFile.Services {
if len(service.Methods) == 0 {
if len(service.QueryMethods) == 0 {
continue
}
file.WriteString(service.Name + ":\n")
for _, method := range service.Methods {
for _, method := range service.QueryMethods {
file.WriteString(" " + method.Name + ":\n")
}
}
Expand Down
24 changes: 21 additions & 3 deletions generator/plugins/graphql/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ type MapOutputObject struct {
ValueObjectType TypeResolver
}
type Service struct {
Name string
CallInterface GoType
Methods []Method
Name string
CallInterface GoType
QueryMethods []Method
MutationMethods []Method
}
type Method struct {
Name string
Expand Down Expand Up @@ -161,6 +162,15 @@ type BodyContext struct {
Importer *importer.Importer
TracerEnabled bool
}

type ServiceContext struct {
Service Service
TracerEnabled bool
ServiceMethods []Method
FieldType string
BodyContext BodyContext
}

type SchemaBodyContext struct {
File SchemaConfig
Importer *importer.Importer
Expand Down Expand Up @@ -192,3 +202,11 @@ type gqlObject struct {
QuotedComment string
Fields []fieldConfig
}

func (gqlObject *gqlObject) TypeName() string {
if gqlObject.QueryObject == true {
return "Query"
}

return "Mutation"
}
17 changes: 13 additions & 4 deletions generator/plugins/graphql/schemas_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,17 @@ func (g *schemaGenerator) resolveObjectFields(nodeCfg SchemaNodeConfig, object *
continue
}

meths := make([]string, len(service.Methods))
for i, meth := range service.Methods {
var serviceMethods []Method

if object.QueryObject == true {
serviceMethods = service.QueryMethods
} else {
serviceMethods = service.MutationMethods
}

meths := make([]string, len(serviceMethods))

for i, meth := range serviceMethods {
meths[i] = meth.Name
}
fields := g.filterMethods(meths, nodeCfg.FilterMethods, nodeCfg.ExcludeMethods)
Expand Down Expand Up @@ -210,8 +219,8 @@ func (g schemaGenerator) bodyTemplateFuncs() map[string]interface{} {
"goType": g.goTypeStr,
"goTypeForNew": g.goTypeForNew,

"serviceConstructor": func(service SchemaService, ctx SchemaBodyContext) string {
return ctx.Importer.Prefix(service.Pkg) + "Get" + service.Name + "ServiceMethods"
"serviceConstructor": func(filedType string, service SchemaService, ctx SchemaBodyContext) string {
return ctx.Importer.Prefix(service.Pkg) + "Get" + service.Name + "Service" + filedType + "Methods"
},
}
}
Expand Down
53 changes: 38 additions & 15 deletions generator/plugins/graphql/templates.go

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions generator/plugins/graphql/templates/schemas_body.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ func Get{{$.SchemaName}}Schema(cls {{$.SchemaName}}SchemaClients, ih *{{intercep
}
{{ end -}}
{{ range $service := $.Services -}}
var {{$service.Name}}Fields = {{serviceConstructor $service $}}(cls.{{$service.Name}}Client, ih{{- if $.TracerEnabled -}}, tr{{- end -}})
var _ = {{$service.Name}}Fields
var {{$service.Name}}QueryFields = {{serviceConstructor "Query" $service $}}(cls.{{$service.Name}}Client, ih{{- if $.TracerEnabled -}}, tr{{- end -}})
var _ = {{$service.Name}}QueryFields
var {{$service.Name}}MutationFields = {{serviceConstructor "Mutation" $service $}}(cls.{{$service.Name}}Client, ih{{- if $.TracerEnabled -}}, tr{{- end -}})
var _ = {{$service.Name}}MutationFields
{{ end -}}
{{ range $object := $.Objects -}}
var {{$object.Name}} = {{gqlPkg}}.NewObject({{gqlPkg}}.ObjectConfig{
Expand All @@ -23,7 +25,7 @@ func Get{{$.SchemaName}}Schema(cls {{$.SchemaName}}SchemaClients, ih *{{intercep
{{ if len $object.Fields -}}
{{ range $fld := $object.Fields -}}
{{ if $fld.Service -}}
"{{$fld.Name}}": {{$fld.Service.Name}}Fields["{{$fld.Name}}"],
"{{$fld.Name}}": {{$fld.Service.Name}}{{$object.TypeName}}Fields["{{$fld.Name}}"],
{{ else -}}
"{{$fld.Name}}": &{{gqlPkg}}.Field{
Name: "{{$fld.Name}}",
Expand Down
88 changes: 0 additions & 88 deletions generator/plugins/graphql/templates/types_body.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -285,91 +285,3 @@ func {{$resolver.FunctionName}}({{ if $.TracerEnabled }} tr {{tracerPkg}}.Tracer
})
}
{{end -}}

// Services
{{ range $service := .File.Services -}}
func Get{{$service.Name}}ServiceMethods(c {{goType $service.CallInterface}}, ih *{{interceptorsPkg}}.InterceptorHandler {{ if $.TracerEnabled }} ,tr {{tracerPkg}}.Tracer {{end}}) {{gqlPkg}}.Fields {
{{ if $service.Methods -}}
return {{gqlPkg}}.Fields{
{{range $method := $service.Methods -}}
"{{$method.Name}}": &{{gqlPkg}}.Field{
Name: "{{$method.Name}}",
Type: {{call $method.GraphQLOutputType $}},
{{ if $method.Arguments -}}
Args: {{gqlPkg}}.FieldConfigArgument{
{{ range $arg := $method.Arguments -}}
"{{$arg.Name}}": &{{gqlPkg}}.ArgumentConfig{Type: {{call $arg.Type $}}},
{{ end -}}
},
{{ end -}}
Resolve: func(p {{gqlPkg}}.ResolveParams) (_ interface{}, rerr error) {
ctx := p.Context
_ = ctx
{{ if $.TracerEnabled -}}
span := tr.CreateChildSpanFromContext(p.Context, "{{$service.Name}}.{{$method.Name}} Resolver")
defer span.Finish()
p.Context = {{opentracingPkg}}.ContextWithSpan(ctx, span)
defer func(){
if rerr != nil {
span.SetTag("error", true).LogFields({{logPkg}}.Error(rerr))
}
}()
{{end -}}
if ih == nil {
{{ if $method.RequestResolver -}}
req, err := {{call $method.RequestResolver "p.Args" $}}{{- if not $method.RequestResolverWithErr -}}, error(nil){{end}}
if err != nil {
return nil, err
}
return {{call $method.ClientMethodCaller "c" "req" $}}
{{ else -}}
req := new({{goTypeForNew $method.RequestType}})
return {{call $method.ClientMethodCaller "c" "req" $}}
{{ end -}}
}
ictx := &{{interceptorsPkg}}.Context{
Service: "{{$service.Name}}",
Method: "{{$method.Name}}",
Params: p,
}
req, err := ih.ResolveArgs(ictx, func(ictx *{{interceptorsPkg}}.Context, next {{interceptorsPkg}}.ResolveArgsInvoker) (result interface{}, err error) {
{{ if $method.RequestResolver -}}
return {{call $method.RequestResolver "p.Args" $}}{{- if not $method.RequestResolverWithErr -}}, error(nil){{end}}
{{ else -}}
return new({{goTypeForNew $method.RequestType}}), nil
{{ end -}}
})
if err != nil {
return nil, {{errorsPkg}}.Wrap(err, "failed to resolve args")
}
return ih.Call(ictx, req, func(ictx *{{interceptorsPkg}}.Context, req interface{}, next {{interceptorsPkg}}.CallMethodInvoker) (result interface{}, err error) {
r, ok := req.({{goType $method.RequestType}})
if !ok {
return nil, {{errorsPkg}}.New({{fmtPkg}}.Sprintf("Resolve args interceptor returns bad request type(%T). Should be: {{goType $method.RequestType}}", req))
}
{{if $method.PayloadErrorChecker -}}
res, err := {{call $method.ClientMethodCaller "c" "r" $}}
if err != nil {
return nil, err
}
if {{call $method.PayloadErrorChecker "res"}} {
{{ if $method.PayloadErrorAccessor -}}
ictx.PayloadError = {{call $method.PayloadErrorAccessor "res"}}
{{ else -}}
ictx.PayloadError = res
{{ end -}}
}
return res, err
{{else -}}
return {{call $method.ClientMethodCaller "c" "r" $}}
{{end -}}
})
},
},
{{ end -}}
}
{{else -}}
return nil
{{end -}}
}
{{ end -}}
84 changes: 84 additions & 0 deletions generator/plugins/graphql/templates/types_service.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
func Get{{.Service.Name}}Service{{.FieldType}}Methods(c {{goType .Service.CallInterface}}, ih *{{interceptorsPkg}}.InterceptorHandler {{ if $.TracerEnabled }} ,tr {{tracerPkg}}.Tracer {{end}}) {{gqlPkg}}.Fields {
{{ if .ServiceMethods -}}
return {{gqlPkg}}.Fields{
{{range $method := .ServiceMethods -}}
"{{$method.Name}}": &{{gqlPkg}}.Field{
Name: "{{$method.Name}}",
Type: {{call $method.GraphQLOutputType $.BodyContext}},
{{ if $method.Arguments -}}
Args: {{gqlPkg}}.FieldConfigArgument{
{{ range $arg := $method.Arguments -}}
"{{$arg.Name}}": &{{gqlPkg}}.ArgumentConfig{Type: {{call $arg.Type $.BodyContext}}},
{{ end -}}
},
{{ end -}}
Resolve: func(p {{gqlPkg}}.ResolveParams) (_ interface{}, rerr error) {
ctx := p.Context
_ = ctx
{{ if $.TracerEnabled -}}
span := tr.CreateChildSpanFromContext(p.Context, "{{$.Service.Name}}.{{$method.Name}} Resolver")
defer span.Finish()
p.Context = {{opentracingPkg}}.ContextWithSpan(ctx, span)
defer func(){
if rerr != nil {
span.SetTag("error", true).LogFields({{logPkg}}.Error(rerr))
}
}()
{{end -}}
if ih == nil {
{{ if $method.RequestResolver -}}
req, err := {{call $method.RequestResolver "p.Args" $.BodyContext}}{{- if not $method.RequestResolverWithErr -}}, error(nil){{end}}
if err != nil {
return nil, err
}
return {{call $method.ClientMethodCaller "c" "req" $.BodyContext}}
{{ else -}}
req := new({{goTypeForNew $method.RequestType}})
return {{call $method.ClientMethodCaller "c" "req" $.BodyContext}}
{{ end -}}
}
ictx := &{{interceptorsPkg}}.Context{
Service: "{{$.Service.Name}}",
Method: "{{$method.Name}}",
Params: p,
}
req, err := ih.ResolveArgs(ictx, func(ictx *{{interceptorsPkg}}.Context, next {{interceptorsPkg}}.ResolveArgsInvoker) (result interface{}, err error) {
{{ if $method.RequestResolver -}}
return {{call $method.RequestResolver "p.Args" $.BodyContext}}{{- if not $method.RequestResolverWithErr -}}, error(nil){{end}}
{{ else -}}
return new({{goTypeForNew $method.RequestType}}), nil
{{ end -}}
})
if err != nil {
return nil, {{errorsPkg}}.Wrap(err, "failed to resolve args")
}
return ih.Call(ictx, req, func(ictx *{{interceptorsPkg}}.Context, req interface{}, next {{interceptorsPkg}}.CallMethodInvoker) (result interface{}, err error) {
r, ok := req.({{goType $method.RequestType}})
if !ok {
return nil, {{errorsPkg}}.New({{fmtPkg}}.Sprintf("Resolve args interceptor returns bad request type(%T). Should be: {{goType $method.RequestType}}", req))
}
{{if $method.PayloadErrorChecker -}}
res, err := {{call $method.ClientMethodCaller "c" "r" $.BodyContext}}
if err != nil {
return nil, err
}
if {{call $method.PayloadErrorChecker "res"}} {
{{ if $method.PayloadErrorAccessor -}}
ictx.PayloadError = {{call $method.PayloadErrorAccessor "res"}}
{{ else -}}
ictx.PayloadError = res
{{ end -}}
}
return res, err
{{else -}}
return {{call $method.ClientMethodCaller "c" "r" $.BodyContext}}
{{end -}}
})
},
},
{{ end -}}
}
{{else -}}
return nil
{{end -}}
}
Loading

0 comments on commit 7fef65b

Please sign in to comment.