Upgrade to Pro — share decks privately, control downloads, hide ads and more …

So you Want to Distribute your GraphQL Schema?

So you Want to Distribute your GraphQL Schema?

Marc-Andre Giroux

June 20, 2019
Tweet

More Decks by Marc-Andre Giroux

Other Decks in Technology

Transcript

  1. @__xuorig__
    GraphQL Conf 2019
    So You Want To Distribute Your
    GraphQL Schema?

    View Slide

  2. GraphQL Conf 2019
    @__xuorig__
    Marc-André Giroux
    Montréal, Canada

    @__xuorig__

    View Slide

  3. View Slide

  4. GraphQL Conf 2019
    @__xuorig__
    Distributed GraphQL?

    View Slide

  5. GraphQL Conf 2019
    @__xuorig__
    Distributed GraphQL
    The Basic Case
    GraphQL API
    query GetRepositories {
    viewer {
    repositories(first: 10) {
    name
    owner {
    name
    }
    }
    }
    }

    View Slide

  6. GraphQL Conf 2019
    @__xuorig__
    Distributed GraphQL
    Distributed Case
    query GetRepositories {
    viewer {
    repositories(first: 10) {
    name
    owner {
    name
    }
    }
    }
    }
    Search Service
    Monolith
    User Service

    View Slide

  7. GraphQL Conf 2019
    @__xuorig__
    Distributed GraphQL
    Distributed Case
    query GetRepositories {
    viewer {
    repositories(first: 10) {
    name
    owner {
    name
    }
    }
    }
    }
    Search Service
    Monolith
    User Service
    ?

    View Slide

  8. GraphQL Conf 2019
    @__xuorig__
    Distributed GraphQL
    Distributed Case
    query GetRepositories {
    viewer {
    repositories(first: 10) {
    name
    owner {
    name
    }
    }
    }
    }
    Search Service
    Monolith
    User Service

    View Slide

  9. GraphQL Conf 2019
    @__xuorig__
    Schema Stitching

    View Slide

  10. GraphQL Conf 2019
    @__xuorig__
    Schema Federation

    View Slide

  11. GraphQL Conf 2019
    @__xuorig__
    GraphQL Gateway

    View Slide

  12. GraphQL Conf 2019
    @__xuorig__
    GraphQL Modules

    View Slide

  13. GraphQL Conf 2019
    @__xuorig__
    Namespaces

    View Slide

  14. GraphQL Conf 2019
    @__xuorig__
    Schema Delegation

    View Slide

  15. GraphQL Conf 2019
    @__xuorig__
    Schema Composition

    View Slide

  16. GraphQL Conf 2019
    @__xuorig__

    View Slide

  17. "Though there is only one graph, the
    implementation of that graph should
    be federated"
    - https://principledgraphql.com/
    GraphQL Conf 2019
    @__xuorig__

    View Slide

  18. GraphQL Conf 2019
    @__xuorig__

    View Slide

  19. GraphQL Conf 2019
    @__xuorig__

    View Slide

  20. GraphQL Conf 2019
    @__xuorig__
    The API Gateway pattern is far from
    new...

    View Slide

  21. GraphQL Conf 2019
    @__xuorig__
    So what's different this time?

    View Slide

  22. GraphQL Conf 2019
    @__xuorig__
    API Gateways
    Endpoint Based
    GET /users/1
    Search Service
    Monolith
    User Service

    View Slide

  23. GraphQL Conf 2019
    @__xuorig__
    API Gateways
    Endpoint Based
    GET /search?q=stuff
    Search Service
    Monolith
    User Service

    View Slide

  24. GraphQL Conf 2019
    @__xuorig__
    In some ways, not so different...

    View Slide

  25. GraphQL Conf 2019
    @__xuorig__
    Schema "Stitching"

    View Slide

  26. GraphQL Conf 2019
    @__xuorig__
    query {
    search(query: "stuff") {}
    issue(id: "abc") {}

    user(id: "def") {}
    }
    Search Service
    Monolith
    User Service

    View Slide

  27. GraphQL Conf 2019
    @__xuorig__
    Search Service Monolith
    User Service
    type Query {
    search(query: String!): [SearchResult!]
    }
    type Query {
    user(id: ID!): User
    }
    type Query {
    issue(id: ID!): Issue
    }

    View Slide

  28. GraphQL Conf 2019
    @__xuorig__
    finalSchema = merge(user_schema, search_schema, monolith_schema)

    View Slide

  29. GraphQL Conf 2019
    @__xuorig__
    Gateway
    type Query {
    search(query: String!): [SearchResult!]
    user(id: ID!): User
    issue(id: ID!): Issue
    }

    View Slide

  30. GraphQL Conf 2019
    @__xuorig__
    In other ways, much more complex.

    View Slide

  31. GraphQL Conf 2019
    @__xuorig__
    API Gateways
    •Types, and even singular fields, can possibly be resolved by `n`
    services

    •Nested fields may need to be resolved outside the current service

    •Resolution logic is extremely complex (Avoiding N+1's, handling
    failure scenarios within resolution of a query, etc)
    GraphQL

    View Slide

  32. GraphQL Conf 2019
    @__xuorig__
    Search Service Monolith
    User Service
    type Query {
    search(query: String!): [SearchResult!]
    }
    union SearchResult = User | Issue
    type Query {
    user(id: ID!): User
    }
    type Query {
    issue(id: ID!): Issue
    }
    type Issue {
    owner: User!
    }
    Schema Stitching
    The Harder Parts

    View Slide

  33. GraphQL Conf 2019
    @__xuorig__
    Schema Stitching
    The Harder Parts
    Search Service Monolith
    User Service
    type Query {
    search(query: String!): [SearchResult!]
    }
    union SearchResult = User | Issue
    type Query {
    user(id: ID!): User
    }
    type Query {
    issue(id: ID!): Issue
    }
    type Issue {
    owner: User!
    }

    View Slide

  34. GraphQL Conf 2019
    @__xuorig__
    •Individual Schemas are invalid on their own, only the final merged
    schema is valid.

    •The Gateway is now much more complex than just simple
    "proxying"
    Schema Stitching
    The Harder Parts

    View Slide

  35. GraphQL Conf 2019
    @__xuorig__
    finalSchema = merge(user_schema, search_schema, monolith_schema)

    View Slide

  36. GraphQL Conf 2019
    @__xuorig__
    finalSchema = merge(user_schema, search_schema, monolith_schema)

    View Slide

  37. GraphQL Conf 2019
    @__xuorig__
    finalSchema = merge(...schemas, links)

    View Slide

  38. GraphQL Conf 2019
    @__xuorig__
    Schema Stitching
    Developer Experience
    Search Service Monolith
    User Service
    Gateway
    Add the "User.pullRequests" field
    Add the "User.pullRequests" link

    View Slide

  39. GraphQL Conf 2019
    @__xuorig__
    •Brittle Gateway Code

    •Business logic often creeping into the Gateway

    •What we wanted to decentralize often ends up being centralized
    Schema Stitching
    Developer Experience

    View Slide

  40. GraphQL Conf 2019
    @__xuorig__

    View Slide

  41. https://www.apollographql.com/docs/graphql-tools/schema-stitching/

    View Slide

  42. GraphQL Conf 2019
    @__xuorig__
    If not schema stitching, then what?

    View Slide

  43. View Slide

  44. View Slide

  45. GraphQL Conf 2019
    @__xuorig__
    Hard Mode: The Magic Gateway

    View Slide

  46. GraphQL Conf 2019
    @__xuorig__
    Schema Federation: Don't miss
    tomorrow's talk!

    View Slide

  47. View Slide

  48. GraphQL Conf 2019
    @__xuorig__
    Let's take a step back

    View Slide

  49. GraphQL Conf 2019
    @__xuorig__
    Is there a more pragmatic approach
    to a GraphQL Gateway?

    View Slide

  50. GraphQL Conf 2019
    @__xuorig__
    Why are we here today? Why
    GraphQL?

    View Slide

  51. GraphQL Conf 2019
    @__xuorig__
    One Size Fits All APIs

    View Slide

  52. GraphQL Conf 2019
    @__xuorig__
    Netflix's Server Side Adapters
    https://medium.com/netflix-techblog/embracing-the-differences-inside-the-netflix-api-redesign

    View Slide

  53. GraphQL Conf 2019
    @__xuorig__
    Netflix's Server Side Adapters
    - Daniel Jacobson
    The better approach for handling API
    design [...] is to create true separation
    of concerns, rather than have all
    server-side decisions made by the
    API provider.

    View Slide

  54. GraphQL Conf 2019
    @__xuorig__
    SoundCloud's BFF Pattern

    View Slide

  55. GraphQL Conf 2019
    @__xuorig__
    GraphQL can be our BFF

    View Slide

  56. GraphQL Conf 2019
    @__xuorig__
    GraphQL can be our server side
    client adapter engine

    View Slide

  57. GraphQL Conf 2019
    @__xuorig__
    https://medium.com/netflix-techblog/embracing-the-differences-inside-the-netflix-api-redesign-15fd8b3dc49d

    View Slide

  58. GraphQL Conf 2019
    @__xuorig__
    GraphQL queries are kind of on-
    demand resource representations

    View Slide

  59. GraphQL Conf 2019
    @__xuorig__
    GraphQL API
    query {
    viewer {
    repositories(first: 10) {
    name
    owner {
    name
    }
    }
    }
    }
    "I want this resource in this shape pls"

    View Slide

  60. GraphQL Conf 2019
    @__xuorig__
    GraphQL API
    query {
    viewer {
    repositories(first: 10) {
    name
    owner {
    name
    }
    }
    }
    }
    "K, it's accessible at /api/abc"

    View Slide

  61. GraphQL Conf 2019
    @__xuorig__
    GraphQL API
    GET /api/abc

    View Slide

  62. GraphQL Conf 2019
    @__xuorig__
    We're basically all using REST at this
    point

    View Slide

  63. GraphQL Conf 2019
    @__xuorig__
    This actually sounds like a great
    feature for an API gateway!

    View Slide

  64. GraphQL Conf 2019
    @__xuorig__
    What are we looking for in an API
    Gateway?

    View Slide

  65. GraphQL Conf 2019
    @__xuorig__
    Decouple the clients from the
    services involved in the consumed
    use cases

    View Slide

  66. GraphQL Conf 2019
    @__xuorig__
    Avoid domain logic #

    View Slide

  67. We remain concerned about business logic and process
    orchestration implemented in middleware [...] Vendors in the
    highly competitive API gateway market are continuing this
    trend by adding features through which they attempt to
    differentiate their products. This results in OVERAMBITIOUS
    API GATEWAY products whose functionality — on top of
    what is essentially a reverse proxy — encourages designs
    that continue to be difficult to test and deploy.
    https://www.thoughtworks.com/radar/platforms/overambitious-api-gateways
    GraphQL Conf 2019
    @__xuorig__

    View Slide

  68. GraphQL Conf 2019
    @__xuorig__
    Do we really want to commit to
    GraphQL for inter-service
    communication?

    View Slide

  69. GraphQL Conf 2019
    @__xuorig__
    Not all of us are starting from
    scratch!

    View Slide

  70. GraphQL Conf 2019
    @__xuorig__
    Use Case Based, Curated API

    View Slide

  71. GraphQL Conf 2019
    @__xuorig__
    Really need a distributed schema?

    View Slide

  72. GraphQL Conf 2019
    @__xuorig__
    •Make sure you're not designing your API according to how your
    services are split up.

    •Dynamic / runtime "stitching" is scary, proceed with care.

    •Consider statically building a gateway from a distributed schema
    using tooling as a middle of the road approach.

    View Slide

  73. GraphQL Conf 2019
    @__xuorig__
    The GraphQL Gateway of my
    dreams

    View Slide

  74. GraphQL Conf 2019
    @__xuorig__
    •Configuration based / make it hard to do the wrong thing.

    •Thinest layer achievable

    •The API Gateway is "just" another service.

    •If distributed schemas are necessary, make it easy to "import" them into the gateway.

    •However, the gateway schema is its own thing that represents the source of truth for
    our exposed API. It requires

    •Resolution is done through your service communication of choice.

    View Slide

  75. GraphQL Conf 2019
    @__xuorig__
    Often a tradeoff between
    development & operational
    complexity

    View Slide

  76. GraphQL Conf 2019
    @__xuorig__
    Alternative: Make developer
    experience great by building tooling
    and best practices

    View Slide

  77. GraphQL Conf 2019
    @__xuorig__
    300+ Contributors to GitHub's
    GraphQL Schema

    View Slide

  78. GraphQL Conf 2019
    @__xuorig__
    Megabytes of schema definitions

    View Slide

  79. GraphQL Conf 2019
    @__xuorig__
    Multiple teams working on one
    schema has not been the hard part

    View Slide

  80. GraphQL Conf 2019
    @__xuorig__
    It is very hard to operate a large
    GraphQL schema at scale, even as a
    single schema

    View Slide

  81. GraphQL Conf 2019
    @__xuorig__
    GraphQL has all we need to build a
    great aggregator or gateway!

    View Slide

  82. GraphQL Conf 2019
    @__xuorig__
    Before choosing a solution, ask
    yourself if the schema itself needs to
    be distributed

    View Slide

  83. Thank you
    Come see me after the talk!

    or

    @__xuorig__ on Twitter

    View Slide