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

February 28, 2019
Tweet

More Decks by Marc-Andre Giroux

Other Decks in Technology

Transcript

  1. How people build software
    !
    "
    So you Want to Distribute your GraphQL Schema?
    @__xuorig__
    GraphQL Day
    Feb. 28th, Toronto

    View Slide

  2. How people build software
    !
    Marc-André Giroux
    @__xuorig__
    Montreal, Canada
    2
    !

    View Slide

  3. How people build software
    "
    Distributed GraphQL?

    View Slide

  4. How people build software
    "
    GraphQL

    API
    query {
    allPlanets {
    edges {
    node {
    population
    }
    }
    }
    }

    View Slide

  5. How people build software
    "
    • Independent development
    • Independent scalability
    • Availability
    • Strong Boundaries
    • A ton of other good reasons

    View Slide

  6. How people build software
    "
    Schema 1
    query {
    allPlanets {
    edges {
    node {
    population
    }
    }
    }
    }
    Schema 2 Schema 3 Schema n

    View Slide

  7. How people build software
    "
    Lots of solutions in the past few years...

    View Slide

  8. How people build software
    "
    Schema Stitching ✨

    View Slide

  9. How people build software
    "
    ✨ Schema Federation

    View Slide

  10. How people build software
    "
    Schema Modules ✨

    View Slide

  11. How people build software
    "
    ✨ Schema Composition

    View Slide

  12. How people build software
    "
    Namespaces ✨

    View Slide

  13. How people build software
    "
    ✨ GraphQL Gateway

    View Slide

  14. How people build software
    "
    Apollo's "Principled GraphQL"

    View Slide

  15. How people build software
    "
    1. One Graph (One exposed schema)
    2. Federated Implementation (Distributed schema)

    View Slide

  16. How people build software
    "
    Is this the only way going forward?

    View Slide

  17. How people build software
    "
    Maybe ¯\_(ツ)_/¯

    View Slide

  18. How people build software
    "
    But first... a bit of history.
    Why are we here today?

    View Slide

  19. How people build software
    "
    Endpoint Based APIs

    View Slide

  20. How people build software
    "
    • Very well optimized for one use case
    • Easily cacheable
    • Provide links to related resources (REST)

    View Slide

  21. How people build software
    "
    Very optimized for one use case

    View Slide

  22. How people build software
    "
    API
    Web Browsers
    Mobile
    Servers
    Analytics
    Gaming Consoles
    Your fridge
    Internal Services

    View Slide

  23. How people build software
    "
    Each client usually has different data requirements and
    even slightly different use cases!

    View Slide

  24. How people build software
    "
    GET /planets
    GET /planets_for_fridge
    GET /planets?version=playstation
    GET /planets?partial=minimal
    GET /planets?fields=name,population
    GET /planets?fields=reinventaquerylanguage
    Accept Header
    ...

    View Slide

  25. How people build software
    "
    GraphQL is not the first time
    we try to solve this problem!

    View Slide

  26. How people build software
    "
    ~6 years ago @ Netflix

    View Slide

  27. How people build software
    "
    https://medium.com/netflix-techblog/embracing-the-differences-inside-the-netflix-api-redesign

    View Slide

  28. How people build software
    "
    ~6 years ago @ SoundCloud

    View Slide

  29. How people build software
    "
    http://philcalcado.com/2015/09/18/the_back_end_for_front_end_pattern_bff.html

    View Slide

  30. How people build software
    "
    What was going on at Facebook back then?

    View Slide

  31. How people build software
    "

    View Slide

  32. How people build software
    "
    GraphQL is kind of a BFF!

    View Slide

  33. How people build software
    "
    GraphQL is kind of an client-based adapter engine!

    View Slide

  34. How people build software
    "
    Let's talk distributed schemas!

    View Slide

  35. How people build software
    "
    Schema Stitching

    View Slide

  36. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    }
    FILM SERVICE CHARACTER SERVICE

    View Slide

  37. How people build software
    "
    type Query {
    films: [Film!]!
    character(id: ID!): Character
    }
    type Film {
    name: String!
    }
    type Character {
    name: String!
    }
    GATEWAY

    View Slide

  38. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    characters: [Character!]!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    films: [Film!]!
    }
    FILM SERVICE CHARACTER SERVICE

    View Slide

  39. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    characters: [Character!]!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    films: [Film!]!
    }
    FILM SERVICE CHARACTER SERVICE
    ? ?

    View Slide

  40. How people build software
    "
    Two Options

    View Slide

  41. How people build software
    "
    1. ID References

    View Slide

  42. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    characters: [Character!]!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    films: [Film!]!
    }
    FILM SERVICE CHARACTER SERVICE
    ? ?

    View Slide

  43. How people build software
    "
    type Query {
    films: [Film!]!
    filmsById(ids: [ID!]!: [Film!]!
    }
    type Film {
    name: String!
    characterIDs: [ID!]!
    }
    type Query {
    characters(ids: [ID!]): [Character!]!
    }
    type Character {
    name: String!
    filmIDs: [ID!]!
    }
    FILM SERVICE CHARACTER SERVICE

    View Slide

  44. How people build software
    "
    • One of GraphQL's great feature is how we can expand relationships within a single
    query, we've lost that ability because every query requires `ids` to have been fetched
    before.
    • To avoid many round trips, schemas that use this approach will try to load as many
    things as possible using the ID of a certain resource.
    • Our schema design is affected by the fact our schema is separated in multiple services.
    Ideally we'd like our clients not be affected by the technical decision of using a
    distributed architecture.

    View Slide

  45. How people build software
    "
    2. Relations at merge time

    View Slide

  46. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    characters: [Character!]!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    films: [Film!]!
    }
    FILM SERVICE CHARACTER SERVICE
    ? ?

    View Slide

  47. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    }
    FILM SERVICE CHARACTER SERVICE

    View Slide

  48. How people build software
    "
    extend Film {
    characters: [Character!]!
    }
    extend Character {
    films: [Film!]!
    }
    LINKS

    View Slide

  49. How people build software
    "
    gateway = merge(merge(films, characters), links)

    View Slide

  50. How people build software
    "
    type Query {
    films: [Film!]!
    character(id: ID!): Character
    }
    type Film {
    name: String!
    }
    type Character {
    name: String!
    }
    extend Film {
    characters: [Character!]!
    }
    extend Character {
    films: [Film!]!
    }

    View Slide

  51. How people build software
    "
    Gateway (Stitched Schema, Links)
    Film Service & Schema Character Service & Schema

    View Slide

  52. How people build software
    "
    Did we actually decentralize?

    View Slide

  53. How people build software
    "
    Gateway (Stitched Schema, Links)
    Film Service & Schema Character Service & Schema
    Adding new functionality
    Add a new type to the
    character schema
    Add a link to "plug in" the new feature
    with the rest of the schema

    View Slide

  54. How people build software
    "
    • Schema modifications now often require a two step process (Add new functionality to
    the right schema, extend at gateway to allow for relationships)
    • We end up with something still quite centralized because of where the relationships
    are formed and the final schema is built
    • It's not very clear where new schema use cases go. We risk that a lot of domain logic
    will leak into the gateway, making our GraphQL gateway a monolithic API, the thing we
    wanted to avoid in the first place using this solution.

    View Slide

  55. How people build software
    "

    View Slide

  56. How people build software
    "
    What should an API Gateway even do?
    What it should it not do?

    View Slide

  57. How people build software
    "
    The Goal: Decouple clients from the services involved in
    consumed use cases

    View Slide

  58. How people build software
    "
    Avoid: Business logic leaking into the gateway itself

    View Slide

  59. How people build software
    "
    Avoid: Single Point of Failure

    View Slide

  60. How people build software
    "
    "We remain concerned about business logic and process orchestration implemented in
    middleware, especially where it requires expert skills and tooling while creating single points
    of scaling and control. 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

    View Slide

  61. How people build software
    "
    What about schema composition/federation?

    View Slide

  62. How people build software
    "
    type Query {
    films: [Film!]!
    }
    type Film {
    name: String!
    }
    extend Character {
    films: [Film!]!
    }
    type Query {
    character(id: ID!): Character
    }
    type Character {
    name: String!
    }
    extend Film {
    characters: [Character!]!
    }
    FILM SERVICE CHARACTER SERVICE

    View Slide

  63. How people build software
    "
    • Schema Stithing but without
    the "links"
    • Choreography vs Orchestration
    • The "Gateway" does almost
    nothing in theory

    View Slide

  64. How people build software
    "
    Type Dependencies

    View Slide

  65. How people build software
    "
    Testing / Development

    View Slide

  66. How people build software
    "
    Who's in charge of the "query plans"?

    View Slide

  67. How people build software
    "
    Tradeoff between development & operational complexity

    View Slide

  68. How people build software
    "
    I worry about splitting up schemas too early

    View Slide

  69. How people build software
    "
    Never forget that we're trying to enable use cases, and
    don't actually care about the services involved

    View Slide

  70. How people build software
    "
    Splitting schemas per service may be leading us to designing a
    schema that maps too much to our internal view of things
    rather than focusing on use cases

    View Slide

  71. How people build software
    "
    Takeaways

    View Slide

  72. How people build software
    "
    GraphQL seems like a real great fit to expose an API and
    decouple clients from the services that enable use cases

    View Slide

  73. How people build software
    "
    But it's not clear to me we need to split up schemas across
    the network.

    View Slide

  74. How people build software
    "
    Use GraphQL as your special "Backend for Frontend"

    View Slide

  75. How people build software
    "
    Make it as thin as possible

    View Slide

  76. How people build software
    "
    Ask yourself if the GraphQL API itself needs to be split up

    View Slide

  77. How people build software
    "
    Chances are you might not need to yet

    View Slide

  78. How people build software
    "
    If it really does... consider splitting in larger "use case"
    chunks versus using a lot of small modules

    View Slide

  79. How people build software
    "
    Keep an eye out for awesome solutions that will be coming
    up. But... always remember what our initials goals were!

    View Slide

  80. How people build software
    "
    Thank you!
    @__xuorig__

    View Slide