So you Want to Distribute your GraphQL Schema?

So you Want to Distribute your GraphQL Schema?

F34d97ba1bfea0ff5e35a9c198562402?s=128

Marc-Andre Giroux

February 28, 2019
Tweet

Transcript

  1. How people build software ! " So you Want to

    Distribute your GraphQL Schema? @__xuorig__ GraphQL Day Feb. 28th, Toronto
  2. How people build software ! Marc-André Giroux @__xuorig__ Montreal, Canada

    2 !
  3. How people build software " Distributed GraphQL?

  4. How people build software " GraphQL
 API query { allPlanets

    { edges { node { population } } } }
  5. How people build software " • Independent development • Independent

    scalability • Availability • Strong Boundaries • A ton of other good reasons
  6. How people build software " Schema 1 query { allPlanets

    { edges { node { population } } } } Schema 2 Schema 3 Schema n
  7. How people build software " Lots of solutions in the

    past few years...
  8. How people build software " Schema Stitching ✨

  9. How people build software " ✨ Schema Federation

  10. How people build software " Schema Modules ✨

  11. How people build software " ✨ Schema Composition

  12. How people build software " Namespaces ✨

  13. How people build software " ✨ GraphQL Gateway

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

  15. How people build software " 1. One Graph (One exposed

    schema) 2. Federated Implementation (Distributed schema)
  16. How people build software " Is this the only way

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

  18. How people build software " But first... a bit of

    history. Why are we here today?
  19. How people build software " Endpoint Based APIs

  20. How people build software " • Very well optimized for

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

    case
  22. How people build software " API Web Browsers Mobile Servers

    Analytics Gaming Consoles Your fridge Internal Services
  23. How people build software " Each client usually has different

    data requirements and even slightly different use cases!
  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 ...
  25. How people build software " GraphQL is not the first

    time we try to solve this problem!
  26. How people build software " ~6 years ago @ Netflix

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

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

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

  30. How people build software " What was going on at

    Facebook back then?
  31. How people build software "

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

    BFF!
  33. How people build software " GraphQL is kind of an

    client-based adapter engine!
  34. How people build software " Let's talk distributed schemas!

  35. How people build software " Schema Stitching

  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
  37. How people build software " type Query { films: [Film!]!

    character(id: ID!): Character } type Film { name: String! } type Character { name: String! } GATEWAY
  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
  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 ? ?
  40. How people build software " Two Options

  41. How people build software " 1. ID References

  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 ? ?
  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
  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.
  45. How people build software " 2. Relations at merge time

  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 ? ?
  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
  48. How people build software " extend Film { characters: [Character!]!

    } extend Character { films: [Film!]! } LINKS
  49. How people build software " gateway = merge(merge(films, characters), links)

  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!]! }
  51. How people build software " Gateway (Stitched Schema, Links) Film

    Service & Schema Character Service & Schema
  52. How people build software " Did we actually decentralize?

  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
  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.
  55. How people build software "

  56. How people build software " What should an API Gateway

    even do? What it should it not do?
  57. How people build software " The Goal: Decouple clients from

    the services involved in consumed use cases
  58. How people build software " Avoid: Business logic leaking into

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

  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
  61. How people build software " What about schema composition/federation?

  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
  63. How people build software " • Schema Stithing but without

    the "links" • Choreography vs Orchestration • The "Gateway" does almost nothing in theory
  64. How people build software " Type Dependencies

  65. How people build software " Testing / Development

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

    "query plans"?
  67. How people build software " Tradeoff between development & operational

    complexity
  68. How people build software " I worry about splitting up

    schemas too early
  69. How people build software " Never forget that we're trying

    to enable use cases, and don't actually care about the services involved
  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
  71. How people build software " Takeaways

  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
  73. How people build software " But it's not clear to

    me we need to split up schemas across the network.
  74. How people build software " Use GraphQL as your special

    "Backend for Frontend"
  75. How people build software " Make it as thin as

    possible
  76. How people build software " Ask yourself if the GraphQL

    API itself needs to be split up
  77. How people build software " Chances are you might not

    need to yet
  78. How people build software " If it really does... consider

    splitting in larger "use case" chunks versus using a lot of small modules
  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!
  80. How people build software " Thank you! @__xuorig__