Go Backends for frontends with GraphQL and gRPC

Go Backends for frontends with GraphQL and gRPC

5bfed9aa3a9ebccb0c0f0cb65ee9e012?s=128

Seiji Takahashi

July 13, 2019
Tweet

Transcript

  1. Go Backends for Frontends with and @__timakin__

  2. Seiji Takahashi Software Engineer Gunosy Inc. Tokyo, Japan.

  3. BFF

  4. Backends For Frontends

  5. BFF (Web) BFF (Native) User Micro Service Article Micro Service

    Payment Micro Service Access Controled Web Browser iOS/Android
  6. Pros of BFF • Each microservices can focus on only

    their own responsibility. • Customize networking protocol for each clients. • Flexibly change the response fields.
  7. BFF Use-cases 1. API Aggregation 2. Session Management 3. SSR

    4. File Upload 5. WebSocket
  8. BFF Use-cases 1. API Aggregation BFF User Micro Service Article

    Micro Service Comment Micro Service Client Concat every JSON Object from internal APIs Send requests to several services concurrently Request an article entity
  9. BFF Use-cases 2. Session Management BFF User Micro Service Article

    Micro Service Comment Micro Service Client Send requests with token Get data from session store Send session_id Session Store
  10. Page request BFF Use-cases 3. SSR BFF User Micro Service

    Article Micro Service Comment Micro Service Client Concat every JSON and pass them to HTML renderer Data fetching
  11. Chunked file upload BFF Use-cases 4. File Upload BFF External

    File Storage Internal API Client Send a request with filePath Store file
  12. Connect with WS BFF Use-cases 5. WebSocket/Polling BFF Message Queue

    Internal API Client Pub/Sub
  13. Micro Service Today’s CaseStudy BFF Micro Service Micro Service Web

    Client
  14. • When they talk about BFF… ◦ JavaScript(Node) is popular

    in most of the cases. ◦ Because it eases the communication between frontend & backend engineers. BFF in Go
  15. • Why Go? ◦ If you are a backend engineer,

    Go BFF will be a good choice from the aspect of performance. ◦ With goroutine, Go would handle the concurrent internal accesses rapidly. ▪ If you threw away REST and changed into RPC-call, you must send request multiple servers everytime. ▪ This means internal latency is critical for UX. ◦ Awesome packages for both of GraphQL and gRPC exist in Go community. BFF in Go
  16. GraphQL

  17. • GraphQL is a simple query language for API which

    has the type system. • Instead of JSON request in like REST API or JSON-RPC, you will send `query` to GraphQL server. • Implementation in JavaScript is the most active. GraphQL introduction
  18. Client

  19. GraphQL vs gRPC • Generally, GraphQL is hotter than gRPC.

    • In a lot of countries GraphQL wins. • However, in East Asia(especially in China), gRPC has more fans.
  20. • Client-side-related words like `apollo`, `react`. • `github` is ranked

    in the top 10. Related words of each protocols • Server-side languages like `java` , `goland`, `python` and the format `protobuf`.
  21. • In-browser playground to explore GraphQL calls. GraphiQL

  22. • Packages ◦ graphql-go/graphql ◦ graphql-go/relay ◦ samsarahq/thunder ◦ 99designs/gqlgen

    GraphQL in Go
  23. • Packages ◦ graphql-go/graphql ◦ graphql-go/relay ◦ samsarahq/thunder ◦ 99designs/gqlgen

    GraphQL in Go
  24. • github.com/99designs/gqlgen ◦ Active development ◦ Type-Safe ◦ Schema Driven

    ◦ Custom middleware appendable GraphQL in Go
  25. • github.com/99designs/gqlgen ◦ Active development ◦ Type-Safe ◦ Schema Driven

    ◦ Custom middleware appendable GraphQL in Go
  26. • github.com/99designs/gqlgen-contrib ◦ package of middlewares for performance tracing ▪

    apollo-tracing ▪ opencensus • datadog ▪ opentracing ▪ prometheus GraphQL in Go
  27. GraphQL in Go • Other packages ◦ Cannot share schema

    with frontend ◦ Reflection based schema ▪ A negative effect to latency
  28. schema.graphql Prepare schemas Instead of auto-generated code, you can use

    defined structs. gqlgen.yml
  29. $ gqlgen Code generate

  30. The implementation inside functions are not automatically generated. Write your

    own code. grpc client
  31. When you test GraphQL • Integration test with input &

    golden files. ◦ Record a real request and response ◦ Mock or temporarily boot internal API ◦ Iterate recorded files and check the equality with the actual responses
  32. gRPC

  33. • HTTP/2 networking • Protobuf serialization ◦ template code generation

    • Go is one of the most active languages for gRPC implementation. • Number of Github stars ◦ Go > Java > Node > Swift > PHP > Haskell
  34. • github.com/golang/protobuf/proto • github.com/golang/protobuf/protoc-gen-go • google.golang.org/grpc Required packages • github.com/ktr0731/evans

    • github.com/uber/prototool Optional tools
  35. blog.proto Schema

  36. uber/prototool strictly checks and formats *.proto Linter

  37. Linter Define the rules on prototool.yaml Run commands

  38. Code Generation github.com/golang/protobuf/protoc-gen-go

  39. Client

  40. Client send a request to internal gRPC API

  41. Server

  42. Server

  43. Server Call the logic implemented by yourself e.g. DB access,

    external API-call
  44. After implementation ktr0731/evans helps you to explore your gRPC server

    locally
  45. Conclusion • Go is one of the best candidate language

    for BFF. • GraphQL + gRPC will empower you with schema-driven-development and low-latency protocol.
  46. Thank you for listening