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 full-size slide

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

    View full-size slide

  3. How people build software
    "
    Distributed GraphQL?

    View full-size slide

  4. How people build software
    "
    GraphQL

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. How people build software
    "
    Schema Stitching ✨

    View full-size slide

  9. How people build software
    "
    ✨ Schema Federation

    View full-size slide

  10. How people build software
    "
    Schema Modules ✨

    View full-size slide

  11. How people build software
    "
    ✨ Schema Composition

    View full-size slide

  12. How people build software
    "
    Namespaces ✨

    View full-size slide

  13. How people build software
    "
    ✨ GraphQL Gateway

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. How people build software
    "
    Endpoint Based APIs

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. How people build software
    "

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. How people build software
    "
    Schema Stitching

    View full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size slide

  40. How people build software
    "
    Two Options

    View full-size slide

  41. How people build software
    "
    1. ID References

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

  55. How people build software
    "

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

  64. How people build software
    "
    Type Dependencies

    View full-size slide

  65. How people build software
    "
    Testing / Development

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

  71. How people build software
    "
    Takeaways

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

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

    View full-size slide