$30 off During Our Annual Pro Sale. View Details »

Go Backends for frontends with GraphQL and gRPC

Go Backends for frontends with GraphQL and gRPC

Seiji Takahashi

July 13, 2019
Tweet

More Decks by Seiji Takahashi

Other Decks in Programming

Transcript

  1. Go Backends for Frontends
    with and
    @__timakin__

    View Slide

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

    View Slide

  3. BFF

    View Slide

  4. Backends
    For
    Frontends

    View Slide

  5. BFF
    (Web)
    BFF
    (Native)
    User
    Micro Service
    Article
    Micro Service
    Payment
    Micro Service
    Access Controled
    Web Browser
    iOS/Android

    View Slide

  6. Pros of BFF
    ● Each microservices can focus on only their own
    responsibility.
    ● Customize networking protocol for each clients.
    ● Flexibly change the response fields.

    View Slide

  7. BFF Use-cases
    1. API Aggregation
    2. Session Management
    3. SSR
    4. File Upload
    5. WebSocket

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  11. Chunked file upload
    BFF Use-cases
    4. File Upload
    BFF
    External
    File
    Storage
    Internal API
    Client
    Send a request with filePath
    Store file

    View Slide

  12. Connect with WS
    BFF Use-cases
    5. WebSocket/Polling
    BFF
    Message
    Queue
    Internal API
    Client
    Pub/Sub

    View Slide

  13. Micro Service
    Today’s CaseStudy
    BFF
    Micro Service
    Micro Service
    Web Client

    View Slide

  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

    View Slide

  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

    View Slide

  16. GraphQL

    View Slide

  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

    View Slide

  18. Client

    View Slide

  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.

    View Slide

  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`.

    View Slide

  21. ● In-browser playground
    to explore GraphQL
    calls.
    GraphiQL

    View Slide

  22. ● Packages
    ○ graphql-go/graphql
    ○ graphql-go/relay
    ○ samsarahq/thunder
    ○ 99designs/gqlgen
    GraphQL in Go

    View Slide

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

    View Slide

  24. ● github.com/99designs/gqlgen
    ○ Active development
    ○ Type-Safe
    ○ Schema Driven
    ○ Custom middleware appendable
    GraphQL in Go

    View Slide

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

    View Slide

  26. ● github.com/99designs/gqlgen-contrib
    ○ package of middlewares for performance tracing
    ■ apollo-tracing
    ■ opencensus
    ● datadog
    ■ opentracing
    ■ prometheus
    GraphQL in Go

    View Slide

  27. GraphQL in Go
    ● Other packages
    ○ Cannot share schema with frontend
    ○ Reflection based schema
    ■ A negative effect to latency

    View Slide

  28. schema.graphql
    Prepare schemas
    Instead of
    auto-generated code,
    you can use defined
    structs.
    gqlgen.yml

    View Slide

  29. $ gqlgen
    Code generate

    View Slide

  30. The implementation inside functions
    are not automatically generated.
    Write your own code.
    grpc client

    View Slide

  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

    View Slide

  32. gRPC

    View Slide

  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

    View Slide

  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

    View Slide

  35. blog.proto
    Schema

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  39. Client

    View Slide

  40. Client
    send a request to internal gRPC API

    View Slide

  41. Server

    View Slide

  42. Server

    View Slide

  43. Server
    Call the logic implemented by yourself
    e.g. DB access, external API-call

    View Slide

  44. After implementation
    ktr0731/evans helps you to explore your gRPC server locally

    View Slide

  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.

    View Slide

  46. Thank you for listening

    View Slide