From 500bd858293a29d21b311e500c30343ab22b4e06 Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Wed, 27 Jul 2022 16:15:08 -0500 Subject: [PATCH] Generate GraphQL types for empty proto messages (#2) --- protoc-gen-graphql/generator/generator.go | 4 ---- protoc-gen-graphql/template.go | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/protoc-gen-graphql/generator/generator.go b/protoc-gen-graphql/generator/generator.go index a244a43..2902002 100644 --- a/protoc-gen-graphql/generator/generator.go +++ b/protoc-gen-graphql/generator/generator.go @@ -111,10 +111,6 @@ func (g *Generator) generateFile(file *spec.File, tmpl string, services []*spec. var packages []*spec.Package for _, m := range g.messages { - // skip empty field message, otherwise graphql-go raise error - if len(m.Fields()) == 0 { - continue - } if m.IsDepended(spec.DependTypeMessage, file.Package()) { switch { case file.Package() == m.Package(): diff --git a/protoc-gen-graphql/template.go b/protoc-gen-graphql/template.go index 8461363..cebe057 100644 --- a/protoc-gen-graphql/template.go +++ b/protoc-gen-graphql/template.go @@ -95,7 +95,8 @@ func Gql__type_{{ .TypeName }}() *graphql.Object { Description: ` + "`" + `{{ .Comment }}` + "`" + `, {{- end }} Fields: graphql.Fields { -{{- range .Fields }} +{{- if .Fields }} + {{- range .Fields }} {{- if .IsResolve }} {{ $query := .ResolveSubField $.Services }} "{{ .FieldName }}": &graphql.Field{ @@ -158,6 +159,11 @@ func Gql__type_{{ .TypeName }}() *graphql.Object { {{- end }} }, {{- end }} + {{- end }} +{{- else }} + "_": &graphql.Field{ + Type: graphql.Boolean, + }, {{- end }} }, {{- if .Interfaces }} @@ -180,13 +186,19 @@ func Gql__input_{{ .TypeName }}() *graphql.InputObject { gql__input_{{ .TypeName }} = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "{{ $.RootPackage.CamelName }}_Input_{{ .TypeName }}", Fields: graphql.InputObjectConfigFieldMap{ -{{- range .Fields }} +{{- if .Fields}} + {{- range .Fields }} "{{ .FieldName }}": &graphql.InputObjectFieldConfig{ {{- if .Comment }} Description: ` + "`" + `{{ .Comment }}` + "`" + `, {{- end }} Type: {{ .FieldTypeInput $.RootPackage.Name }}, }, + {{- end }} +{{- else }} + "_": &graphql.InputObjectFieldConfig{ + Type: graphql.Boolean, + }, {{- end }} }, })