Skip to content

Commit

Permalink
Resolve camelCase enum values (#13)
Browse files Browse the repository at this point in the history
* EOD commit
- enums seem to not be marshalling correctly

* camelcase generates enums as int32 instead of type

* remove console logs from response.go

* add CHANGELOG entry
  • Loading branch information
alehechka authored Aug 1, 2022
1 parent b218024 commit 702c8f1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## UNRELEASED

- Resolve camelCase enum values ([#13](https://github.com/alehechka/grpc-graphql-gateway/pull/13))

## [v0.2.4](https://github.com/alehechka/grpc-graphql-gateway/releases/tag/v0.2.4)

- Add compiler and plugin version info to header comment ([#12](https://github.com/alehechka/grpc-graphql-gateway/pull/12))
Expand Down
1 change: 1 addition & 0 deletions example/starwars/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build: init
--plugin=../../dist/protoc-gen-graphql \
--graphql_out=${SPECDIR} \
--graphql_opt=paths=source_relative \
--graphql_opt=field_camel \
--go_out=:${SPECDIR} \
--go_opt=paths=source_relative \
--go-grpc_out=:${SPECDIR} \
Expand Down
8 changes: 8 additions & 0 deletions example/starwars/app/graphql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/alehechka/grpc-graphql-gateway/example/starwars/spec/starwars"
"github.com/alehechka/grpc-graphql-gateway/runtime"
"github.com/friendsofgo/graphiql"
"google.golang.org/grpc"
)

Expand All @@ -16,6 +17,13 @@ func main() {
if err := starwars.RegisterStartwarsServiceGraphqlHandler(mux, nil, "localhost:50051", opts...); err != nil {
log.Fatalln(err)
}

graphiqlHandler, err := graphiql.NewGraphiqlHandler("/graphql")
if err != nil {
log.Fatalln(err)
}

http.Handle("/graphql", mux)
http.Handle("/graphiql", graphiqlHandler)
log.Fatalln(http.ListenAndServe(":8888", nil))
}
50 changes: 25 additions & 25 deletions example/starwars/spec/starwars/starwars.graphql.go

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

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/alehechka/grpc-graphql-gateway
go 1.18

require (
github.com/friendsofgo/graphiql v0.2.2
github.com/golang/protobuf v1.5.2
github.com/graphql-go/graphql v0.8.0
github.com/iancoleman/strcase v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/friendsofgo/graphiql v0.2.2 h1:ccnuxpjgIkB+Lr9YB2ZouiZm7wvciSfqwpa9ugWzmn0=
github.com/friendsofgo/graphiql v0.2.2/go.mod h1:8Y2kZ36AoTGWs78+VRpvATyt3LJBx0SZXmay80ZTRWo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down
3 changes: 2 additions & 1 deletion protoc-gen-graphql/spec/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewEnum(
d *descriptor.EnumDescriptorProto,
f *File,
prefix []string,
isCamel bool,
paths ...int,
) *Enum {

Expand All @@ -37,7 +38,7 @@ func NewEnum(
for i, v := range d.GetValue() {
ps := make([]int, len(paths))
copy(ps, paths)
e.values = append(e.values, NewEnumValue(v, f, append(ps, 2, i)...))
e.values = append(e.values, NewEnumValue(v, f, isCamel, append(ps, 2, i)...))
}
return e
}
Expand Down
12 changes: 11 additions & 1 deletion protoc-gen-graphql/spec/enum_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ type EnumValue struct {
descriptor *descriptor.EnumValueDescriptorProto
*File

paths []int
paths []int
isCamel bool
}

func NewEnumValue(
d *descriptor.EnumValueDescriptorProto,
f *File,
isCamel bool,
paths ...int,
) *EnumValue {

return &EnumValue{
descriptor: d,
File: f,
isCamel: isCamel,
paths: paths,
}
}
Expand All @@ -36,3 +39,10 @@ func (e *EnumValue) Number() int32 {
func (e *EnumValue) Name() string {
return e.descriptor.GetName()
}

func (e *EnumValue) IsCamel() bool {
if e != nil {
return e.isCamel
}
return false
}
4 changes: 2 additions & 2 deletions protoc-gen-graphql/spec/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewFile(
f.messages = append(f.messages, f.messagesRecursive(m, []string{}, 4, i)...)
}
for i, e := range d.GetEnumType() {
f.enums = append(f.enums, NewEnum(e, f, []string{}, 5, i))
f.enums = append(f.enums, NewEnum(e, f, []string{}, f.isCamel, 5, i))
}
return f
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (f *File) messagesRecursive(d *descriptor.DescriptorProto, prefix []string,
for i, e := range d.GetEnumType() {
p := make([]int, len(paths))
copy(p, paths)
f.enums = append(f.enums, NewEnum(e, f, prefix, append(p, 5, i)...))
f.enums = append(f.enums, NewEnum(e, f, prefix, f.isCamel, append(p, 5, i)...))
}

for i, m := range d.GetNestedType() {
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-graphql/template/template.go

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

0 comments on commit 702c8f1

Please sign in to comment.