SlideShare a Scribd company logo
GraphQL API
Gateway
Go code generation isn’t frightful.
Yaroslav Mytso
Software engineer at EGT Ukraine
Service 1
(GRPC API)
Service 2
(GRPC API)
Service 3
(GRPC API)
Service N
(GRPC API)
Front-end
X
X
X
X
API
Gateway
Service 1
(GRPC API)
Service 2
(GRPC API)
Service 3
(GRPC API)
Service N
(GRPC API)
Front-end
{
user(id: «1») {
name
}
dog(name: «A»){
nick
}
}
{
«user»: {
«name»: «UserName»
},
«dog»: {
«nick»: «Bobby»
}
}
Type: User
Type: Pet
Type: String
Type: String
Type: Query
GraphQL → GRPC Proxy writing process
GRPC Message
GraphQL
Output Object
GraphQL
Input Object
GRPC Service
GraphQL
Fields Array
GRPC Enum
GraphQL
Enum
Input Object
unmarshaler+
GRPC
Method
Method
Resolver
Message in .proto file VS Message in GraphQL Schema
message A {
int32 someField = 1;
int64 someAnotherField = 2;
string andAnotherOne = 3;
}
var GQLA = graphql.NewObject(graphql.ObjectConfig{
Name: "A",
Fields: graphql.Fields{
"someField": &graphql.Field{
Name: "someField",
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
switch src := p.Source.(type) {
case A:
return src.SomeField, nil
case *A:
return src.SomeField, nil
}
return nil, nil
},
},
"someAnotherField": &graphql.Field{
Name: "someAnotherField",
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
switch src := p.Source.(type) {
case A:
return src.SomeAnotherField, nil
case *A:
return src.SomeAnotherField, nil
}
return nil, nil
},
},
"andAnotherOne": &graphql.Field{
Name: "andAnotherOne",
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
switch src := p.Source.(type) {
case A:
return src.AndAnotherOne, nil
case *A:
return src.AndAnotherOne, nil
}
return nil, nil
},
},
},
})
😞
protoc-gen-gogoopsee
(gogo plugin)
http://github.com/opsee/protobuf/
*.proto gogo
stdin
stdout
GRPC Client
GRPC Client Tests
GoGo code generation process
plugin 1plugin 1 plugin 2 plugin N
protoc-gen-gogoopsee
GRPC Message
GraphQL
Output Object
GraphQL
Input Object
GRPC Service
GraphQL
Fields Array
GRPC Enum
GraphQL
Enum
Input Object
unmarshaler+
GRPC
Method
Field
Resolver
X
X
X
p.P(`»`, field.GetName(), `": &`, graphQLPkg.Use(), `.Field{`)
p.In()
p.P(`Type: `, p.graphQLType(message, field, graphQLPkg, schemaPkg), `,`)
p.P(`Description: `, fieldGQL, `,`)
p.P(`Resolve: func(p `, graphQLPkg.Use(), `.ResolveParams) (interface{}, error) {`)
p.In()
p.P(`return nil, nil`)
p.Out()
p.P(`}}`)
p.Out()
fmt.Println() generated code
«{{$field.Name}}»: {{gqlPkg}}.Field{
Type: {{call $field.Type}},
Description: {{$field.Description}},
Resolve: func(p {{gqlPkg}}.ResolveParams) (interface{}, error) {
return nil, nil
}
}
Generate code using
text/template
GoGo plugin problems
• Only two files in output
• Generator knows about only one .proto file and can’t
make composition
• Hard to debug
• Option-based configuration
.proto files parser
generate.yaml
vendor_path: «./vendor»
paths:
- «$GOPATH/src»
- «./vendor»
proto_files:
- path : «./a.proto»
- …
- path: «./b.proto»
schema:
Query:
- field: «a»
type: «SERVICE»
- field: «b»
type: «SERVICE»
Parser
Query {
a : AService,
b: BService,
}
AService {
meth1(req:Req) : MethResp,
meth2(req:Req) : SomeRes,
}
BService {
meth1(req:Req) : MethResp,
}
a.proto b.proto
Template
schema.go
./services/a.go
./services/b.go
proto2gql
Too heavy template
Generator responsibilities
.proto files
GraphQL schema
Generator responsibilities
.proto files
GraphQL schema
swagger files
Custom .proto files parser
generate.yaml
vendor_path: «./vendor»
paths:
- «$GOPATH/src»
- «./vendor»
proto_files:
- path : «./a.proto»
- …
- path: «./b.proto»
schema:
Query:
- field: «a»
type: «SERVICE»
- field: «b»
type: «SERVICE»
Query {
a : AService,
b: BService,
}
AService {
meth1(req:Req) : MethResp,
meth2(req:Req) : SomeRes,
}
BService {
meth1(req:Req) : MethResp,
}
Parser
a.proto b.proto
Template
schema.go
./services/a.go
./services/b.go
Normalizing data
generate.yaml
vendor_path: «./vendor»
paths:
- «$GOPATH/src»
- «./vendor»
proto_files:
- path : «./a.proto»
- …
- path: «./b.proto»
schema:
Query:
- field: «a»
type: «SERVICE»
- field: «b»
type: «SERVICE»
Query {
a : AService,
b: BService,
}
AService {
meth1(req:Req) : MethResp,
meth2(req:Req) : SomeRes,
}
BService {
meth1(req:Req) : MethResp,
}
.proto
parser
a.proto
b.proto
Template
schema.go
./services/a.go
./services/b.go
swagger
Parser
data
normalizer
data
normalizer
swagger1.yml
swagger2.yml
generate.yaml
vendor_path: «./»
graphql:
schemas:
…
proto2gql:
proto_files:
- …
swagger2gql:
swagger_files:
- …
graphql plugin
(knows how to generate graphql
schema based on own DTO’s)
proto2gql plugin
(knows how to convert info about
*.proto file into graphql DTO’s)
proto2gql
swagger2gql plugin
(knows how to convert info
swagger file into graphql DTO’s)
Plugins
github.com/saturn4er/proto2gql
Conclusions
• Use templates in code generation
• Prepare data for templates in case of a possibility
of some other data-source
• Choose toolchain that will not frame you
Questions?
Yaroslav Mytso
yaroslav.mitso@egt-ua.com
github.com/saturn4er

More Related Content

PDF
10 reasons to be excited about go
PDF
GOCON Autumn (Story of our own Monitoring Agent in golang)
PDF
Golang
PPTX
G++ & GCC
PDF
Software maintenance PyConPL 2016
PPTX
Go. Why it goes
ODP
CRUD Operation With Dgraph
PDF
Cap'n Proto (C++ Developer Meetup Iasi)
10 reasons to be excited about go
GOCON Autumn (Story of our own Monitoring Agent in golang)
Golang
G++ & GCC
Software maintenance PyConPL 2016
Go. Why it goes
CRUD Operation With Dgraph
Cap'n Proto (C++ Developer Meetup Iasi)

What's hot (20)

PDF
ActiveDoc
PPT
Debugging Applications with GNU Debugger
PDF
Go 1.10 Release Party - PDX Go
PPTX
Andriy Shalaenko - GO security tips
PDF
Invoke Dynamic
PPTX
Compiling Under Linux
PPT
Gccgdb
PPTX
Gnu debugger
PPTX
Iron Languages - NYC CodeCamp 2/19/2011
PPTX
Golang for OO Programmers
PDF
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
PPTX
Documenting an API written in Django Rest Framework
PDF
PyCon Ukraine 2016: Maintaining a high load Python project for newcomers
PDF
GNU Compiler Collection - August 2005
PPT
GCC compiler
PDF
How to easily find the optimal solution without exhaustive search using Genet...
PDF
Java 11 - Keeping the Java Release Train on the Right Track
PDF
Run Go applications on Pico using TinyGo
PDF
Usage of GDB
ActiveDoc
Debugging Applications with GNU Debugger
Go 1.10 Release Party - PDX Go
Andriy Shalaenko - GO security tips
Invoke Dynamic
Compiling Under Linux
Gccgdb
Gnu debugger
Iron Languages - NYC CodeCamp 2/19/2011
Golang for OO Programmers
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Documenting an API written in Django Rest Framework
PyCon Ukraine 2016: Maintaining a high load Python project for newcomers
GNU Compiler Collection - August 2005
GCC compiler
How to easily find the optimal solution without exhaustive search using Genet...
Java 11 - Keeping the Java Release Train on the Right Track
Run Go applications on Pico using TinyGo
Usage of GDB
Ad

Similar to Graph ql api gateway (20)

PDF
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
PDF
GraphQL IN Golang
PDF
Fast and Reliable Swift APIs with gRPC
PDF
如何透過 Go-kit 快速搭建微服務架構應用程式實戰
PPTX
Building your First gRPC Service
PDF
Graphql usage
PPTX
Modern webservices using gRPC and Protocol Buffers in Golang
PPTX
Building gRPC services
PPTX
The API Journey: GraphQL Specification and Implementation
PDF
Implementing OpenAPI and GraphQL services with gRPC
PDF
What we learnt at carousell tw for golang gathering #31
PDF
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
PPTX
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
PDF
Build microservice with gRPC in golang
PDF
Protobuf & Code Generation + Go-Kit
PDF
Introduction to GraphQL for beginners
PDF
Writing a REST Interconnection Library in Swift
PDF
apidays LIVE Paris - GraphQL meshes by Jens Neuse
PPTX
The API Journey: from REST to GraphQL
PDF
The GrapQL ecosystem
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GraphQL IN Golang
Fast and Reliable Swift APIs with gRPC
如何透過 Go-kit 快速搭建微服務架構應用程式實戰
Building your First gRPC Service
Graphql usage
Modern webservices using gRPC and Protocol Buffers in Golang
Building gRPC services
The API Journey: GraphQL Specification and Implementation
Implementing OpenAPI and GraphQL services with gRPC
What we learnt at carousell tw for golang gathering #31
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
Build microservice with gRPC in golang
Protobuf & Code Generation + Go-Kit
Introduction to GraphQL for beginners
Writing a REST Interconnection Library in Swift
apidays LIVE Paris - GraphQL meshes by Jens Neuse
The API Journey: from REST to GraphQL
The GrapQL ecosystem
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
master seminar digital applications in india
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Institutional Correction lecture only . . .
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
RMMM.pdf make it easy to upload and study
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
GDM (1) (1).pptx small presentation for students
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Renaissance Architecture: A Journey from Faith to Humanism
master seminar digital applications in india
Supply Chain Operations Speaking Notes -ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Institutional Correction lecture only . . .
Sports Quiz easy sports quiz sports quiz
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Graph ql api gateway

  • 1. GraphQL API Gateway Go code generation isn’t frightful. Yaroslav Mytso Software engineer at EGT Ukraine
  • 2. Service 1 (GRPC API) Service 2 (GRPC API) Service 3 (GRPC API) Service N (GRPC API) Front-end X X X X
  • 3. API Gateway Service 1 (GRPC API) Service 2 (GRPC API) Service 3 (GRPC API) Service N (GRPC API) Front-end
  • 4. { user(id: «1») { name } dog(name: «A»){ nick } } { «user»: { «name»: «UserName» }, «dog»: { «nick»: «Bobby» } } Type: User Type: Pet Type: String Type: String Type: Query
  • 5. GraphQL → GRPC Proxy writing process GRPC Message GraphQL Output Object GraphQL Input Object GRPC Service GraphQL Fields Array GRPC Enum GraphQL Enum Input Object unmarshaler+ GRPC Method Method Resolver
  • 6. Message in .proto file VS Message in GraphQL Schema message A { int32 someField = 1; int64 someAnotherField = 2; string andAnotherOne = 3; } var GQLA = graphql.NewObject(graphql.ObjectConfig{ Name: "A", Fields: graphql.Fields{ "someField": &graphql.Field{ Name: "someField", Type: graphql.Int, Resolve: func(p graphql.ResolveParams) (interface{}, error) { switch src := p.Source.(type) { case A: return src.SomeField, nil case *A: return src.SomeField, nil } return nil, nil }, }, "someAnotherField": &graphql.Field{ Name: "someAnotherField", Type: graphql.Int, Resolve: func(p graphql.ResolveParams) (interface{}, error) { switch src := p.Source.(type) { case A: return src.SomeAnotherField, nil case *A: return src.SomeAnotherField, nil } return nil, nil }, }, "andAnotherOne": &graphql.Field{ Name: "andAnotherOne", Type: graphql.String, Resolve: func(p graphql.ResolveParams) (interface{}, error) { switch src := p.Source.(type) { case A: return src.AndAnotherOne, nil case *A: return src.AndAnotherOne, nil } return nil, nil }, }, }, }) 😞
  • 8. *.proto gogo stdin stdout GRPC Client GRPC Client Tests GoGo code generation process plugin 1plugin 1 plugin 2 plugin N
  • 9. protoc-gen-gogoopsee GRPC Message GraphQL Output Object GraphQL Input Object GRPC Service GraphQL Fields Array GRPC Enum GraphQL Enum Input Object unmarshaler+ GRPC Method Field Resolver X X X
  • 10. p.P(`»`, field.GetName(), `": &`, graphQLPkg.Use(), `.Field{`) p.In() p.P(`Type: `, p.graphQLType(message, field, graphQLPkg, schemaPkg), `,`) p.P(`Description: `, fieldGQL, `,`) p.P(`Resolve: func(p `, graphQLPkg.Use(), `.ResolveParams) (interface{}, error) {`) p.In() p.P(`return nil, nil`) p.Out() p.P(`}}`) p.Out() fmt.Println() generated code
  • 11. «{{$field.Name}}»: {{gqlPkg}}.Field{ Type: {{call $field.Type}}, Description: {{$field.Description}}, Resolve: func(p {{gqlPkg}}.ResolveParams) (interface{}, error) { return nil, nil } } Generate code using text/template
  • 12. GoGo plugin problems • Only two files in output • Generator knows about only one .proto file and can’t make composition • Hard to debug • Option-based configuration
  • 13. .proto files parser generate.yaml vendor_path: «./vendor» paths: - «$GOPATH/src» - «./vendor» proto_files: - path : «./a.proto» - … - path: «./b.proto» schema: Query: - field: «a» type: «SERVICE» - field: «b» type: «SERVICE» Parser Query { a : AService, b: BService, } AService { meth1(req:Req) : MethResp, meth2(req:Req) : SomeRes, } BService { meth1(req:Req) : MethResp, } a.proto b.proto Template schema.go ./services/a.go ./services/b.go proto2gql
  • 17. Custom .proto files parser generate.yaml vendor_path: «./vendor» paths: - «$GOPATH/src» - «./vendor» proto_files: - path : «./a.proto» - … - path: «./b.proto» schema: Query: - field: «a» type: «SERVICE» - field: «b» type: «SERVICE» Query { a : AService, b: BService, } AService { meth1(req:Req) : MethResp, meth2(req:Req) : SomeRes, } BService { meth1(req:Req) : MethResp, } Parser a.proto b.proto Template schema.go ./services/a.go ./services/b.go
  • 18. Normalizing data generate.yaml vendor_path: «./vendor» paths: - «$GOPATH/src» - «./vendor» proto_files: - path : «./a.proto» - … - path: «./b.proto» schema: Query: - field: «a» type: «SERVICE» - field: «b» type: «SERVICE» Query { a : AService, b: BService, } AService { meth1(req:Req) : MethResp, meth2(req:Req) : SomeRes, } BService { meth1(req:Req) : MethResp, } .proto parser a.proto b.proto Template schema.go ./services/a.go ./services/b.go swagger Parser data normalizer data normalizer swagger1.yml swagger2.yml
  • 19. generate.yaml vendor_path: «./» graphql: schemas: … proto2gql: proto_files: - … swagger2gql: swagger_files: - … graphql plugin (knows how to generate graphql schema based on own DTO’s) proto2gql plugin (knows how to convert info about *.proto file into graphql DTO’s) proto2gql swagger2gql plugin (knows how to convert info swagger file into graphql DTO’s) Plugins
  • 21. Conclusions • Use templates in code generation • Prepare data for templates in case of a possibility of some other data-source • Choose toolchain that will not frame you