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

GraphQL - A query language to empower your API consumers (NDC Sydney 2017)

GraphQL - A query language to empower your API consumers (NDC Sydney 2017)

The shift to microservices, cloud native and rich web apps have made it challenging to deliver compelling API experiences. REST, as specified in Roy Fielding’s seminal dissertation, has become the architectural pattern of choice for APIs and when applied correctly allows for clients and servers to evolve in a loosely coupled manner. There are areas however where REST can deliver less than ideal client experiences. Often many HTTP requests are required to render a single view.
While this may be a minor concern for a web app running on a WAN with low latency and high bandwidth, it can yield poor client experiences for mobile clients in particular. GraphQL is Facebook’s response to this challenge and it is quickly proving itself as an exciting alternative to RESTful APIs for a wide range of contexts. GraphQL is a query language that provides a clean and simple syntax for consumers to interrogate your APIs. These queries are strongly types, hierarchical and enable clients to retrieve only the data they need.

In this session, we will take a hands-on look at GraphQL and see how it can be used to build APIs that are a joy to use.

Rob Crowley

August 18, 2017
Tweet

More Decks by Rob Crowley

Other Decks in Programming

Transcript

  1. Goals for the session -Explore the motivation behind GraphQL and

    the category of problems it helps to solve. -Take a tour of GraphQL’s features and see it in action. -Investigate how to effectively leverage GraphQL in your delivery process. -Most of all sufficiently motivate you to continue to learn GraphQL after leaving this conference.
  2. The shift to microservices, cloud native and rich web apps

    have made it challenging to deliver compelling API experiences
  3. Image Service Cross Sell Service Product Service Product Service Product

    Service Review Service Price Service Inventory Service Promotions Service Other Sellers Service Price Service Image Service Inventory Service Order Service Image Service Price Service
  4. Cross Sell Component Header Component Product Image Component Product Review

    Component Product Price Component Product Inventory Component Product Purchase Component Alternate Sellers Component Product Details Component Offers Component Product Header Component
  5. Leveraging an API Gateway to reduce latency Microservice Microservice Microservice

    Microservice API Gateway Wait, we support more than one customer experience channel. How to handle these? Component Component Component Component
  6. Introducing Backends for Frontends (BFFs) Microservice Microservice Microservice Microservice Mobile

    BFF Desktop BFF Wearables BFF What about the different data requirements between versions? V4, V5… On page reload V1, V2…
  7. GraphQL as the translation layer Microservice Relational DB Graph DB

    Web Service GraphQL API Retrieve exactly the data every version of each channel experience requires in one round trip. optimised { data }
  8. Our Example Domain Album Artist Label User Album Review compose

    submit favourite released on refers to 1 * * 1 1 * * 1 1 * artistId: int name: string albumId: int title: string labelId: int name: string founded: int userId: int name: string reviewId: int albumId: int review: string rating: int
  9. Hey, just what are you trying to pull here!? Couldn’t

    we already do this with REST? Absolutely, but GraphQL makes it trivial and ensures a consistent interface.
  10. “Reliability problems largely occur at module boundaries, and most result

    from inconsistencies in both sides expectations” - Bertrand Meyer
  11. let queryArtist = (_, {id}, {dao}) => { // arbitrary

    code to retrieve artist return dao.getArtist(id); } let artistName = artist => { return artist.name; }
  12. root query { artist(id: “5”) { name albums { title

    label { name } } } } Label Producer Album User Artist Review
  13. root query { artist(id: “5”) { name albums { }

    } } Label Producer Album User Artist Review
  14. root Label Producer Album User Artist Review query { artist(id:

    “5”) { name albums { title label { } } } }
  15. root Label Producer Album User Artist Review query { artist(id:

    “5”) { name albums { title label { name } } } }
  16. Capabilities exposed as schema Requirements expressed as query GraphQL focusses

    on accessing data exposed by the graph This is a fundamental difference to REST which is concerned with modelling resources and state transitions. Clients Single /graphql endpoint *
  17. GraphQL Operations -Query -> Read ( 1 : 1 )

    -Mutation -> Write ( 1 : 1 ) -Subscription -> Observe Event ( 1 : 0..* )
  18. -The second killer feature of GraphQL -Enables ‘reflection’ type queries

    on fields, types and schema -Extremely powerful for tooling support - Autocomplete (as seen in Graphiql) - Code generation - Documentation Introspection
  19. Efficiency -Avoid overfetching and underfetching (particularly across the slow portion

    of the network to the client). -Reduced data handling complexity in the client. -Simpler extension model for backend API developers.
  20. GraphQL is protocol agnostic. So long as the protocol supports

    exchanging character data you’re set.
  21. “Every time time you change a version identifier, you’re potentially

    orphaning clients who ‘speak’ that language.” - Mark Nottingham
  22. In place of versioning prefer an extension model to enable

    graceful evolution of the API contract
  23. GraphQL is not a shortcut to great API design. Solid

    modelling skills are still table stakes. (sorry, not sorry)
  24. “There are only two hard things in Computer Science: cache

    invalidation and naming things.” - Phil Karlton
  25. Caching Approaches for RESTful APIs Client Side Caching Server Side

    Caching HTTP offers rich caching functionality via caching headers: Cache-Control, ETag, Expires, Pragma & Vary.
  26. Caching Approaches for RESTful APIs Client Side Caching Server Side

    Caching Intermediate Caching Proxy HTTP offers rich caching functionality via caching headers: Cache-Control, ETag, Expires, Pragma & Vary.
  27. Caching Approaches for GraphQL APIs Client Side Caching Server Side

    Caching Intermediate Caching Proxy GraphQL does not play nice with the rest of the web, as it treats HTTP as a dump pipe. As such we need to rely on server and client caching.
  28. Server Caching - Utilise a dedicated cache layer to minimize

    traffic downstream. - DataLoader implementations assist with per request batching and caching.
  29. Client Caching - GraphQL clients such as Apollo or Relay

    help massively in this regard. - Hierarchical responses are flattened to create normalized caches where each record is stored exactly once. - Clients can leverage this cache to generate network efficient queries. query: { album (id: “1”): { name: “Endtroducing…" artist: { name: “DJ Shadow" } } } Map { 1: Map { name: “Endtroducing…”, author: Link(2), }, 2: Map { name: ‘DJ Shadow' }, }; Hierarchical Normalized
  30. Inter API Communication - Inter API calls are easily supported

    in REST APIs based of the media type semantics. - In fact, Hypermedia is the under- pinning architectural style of the web. GET /artistsapi/artist/1 HTTP/1.1 ContentType: application/vnd.hal+json { “_links”: { “events” : { “href”: “/eventsapi/performers/8”, “type” : “application/vnd.siren+json” } /* … other fields … */ } Events API Artists API
  31. Inter API Communication - Inter API calls are easily supported

    in REST APIs based of the media type semantics. - In fact, Hypermedia is the under- pinning architectural style of the web. GET /eventsapi/events/8 HTTP/1.1 ContentType: application/vnd.siren+json { "class": [ “event" ], "properties": { “startDate": “2017-09-18T15:00:00+10:00”, “endDate: “2017-08-18T16:00:00+10:00”, “availableTicketCount": 42 }, /* … other fields … */ } Events API Artists API
  32. { “data”: { “artist” : { “events”: [ { “url”:

    “/eventsapi/performers/8” } ] } } } Inter API Communication - There is no easy way to federate calls across GraphQL schemas. - Ideally model these as additional nodes and edges in the same GraphQL schema. - Failing this we must defer to ‘out of band’ sources such as API documentation. POST /graphql HTTP/1.1 { artist { events { url } } } Events API GraphQL API
  33. { “data”: { “artist” : { “events”: [ { “url”:

    “/eventsapi/performers/8” } ] } } } Inter API Communication - There is no easy way to federate calls across GraphQL schemas. - Ideally model these as additional nodes and edges in the same GraphQL schema. - Failing this we must defer to ‘out of band’ sources such as API documentation. POST /graphql HTTP/1.1 { artist { events { url } } } Events API GraphQL API ?
  34. As a User, I want to be the only one

    permitted to see my personal details So that my right to privacy is respected
  35. This approach works well for 1st party systems. What about

    3rd party systems with delegated access?
  36. Authentication Authentication HTTP TCP based protocol REST RPC Coarse Grained

    Authorization GraphQL Business Logic Authorization
  37. Image by toffehoff via https://www.flickr.com/photos/toffehoff/244870161/ So far we have been

    pulling data what if we could we enable the pushing of data
  38. As a User, I want to be notified when my

    favourite artists release albums So that I can keep to up to date with the music that I love
  39. Real Time APIs -Pull -Polling -Push -Live Queries (standard query

    with infinitely fast polling) -Subscriptions (event based)
  40. Event Based Subscriptions -Concept: Server pushes message to client when

    ‘something’ occurs -Read only -Expressed using GraphQL syntax -Not a replacement for GraphQL queries!
  41. Example subscription workflow / use case User Admin GraphQL Server

    Connect to WS endpoint (ws://api.example.com/subscriptions)
  42. Example subscription workflow / use case Subscribe to advertised event

    (http://api.example.com/graphql) subscription onAlbumAdded { albumAdded(userId: “1”) { album { title } artist { name } } } User Admin GraphQL Server Web socket connection open and waiting for published events
  43. Example subscription workflow / use case User Admin mutation addAlbum

    { addAlbum(album: {title: "Cold Water Music", artistId: 1, labelId: 1 }) { artist { id name } } } Admin user adds new album using addAlbum mutation GraphQL Server Web socket connection open and waiting for published events
  44. Album added event document pushed to client Example subscription workflow

    / use case User Admin { albumAdded: { album: { title: “Cold Water Music” } artist: { name: “Aim” } } } GraphQL Server
  45. GraphQL Dev Workflow - Front end and back end teams

    agree on schema. - UI is developed using mocked data based on schema - API is built out with any changes being communicated to front end team - Integrate front and back ends - Ship - Repeat Schema App Api Product
  46. GraphQL Dev Workflow - Front end and back end teams

    agree on schema. - UI is developed using mocked data based on schema - API is built out with any changes being communicated to front end team - Integrate front and back ends - Ship - Repeat Schema App Api Product
  47. GraphQL Dev Workflow - Front end and back end teams

    agree on schema. - UI is developed using mocked data based on schema - API is built out with any changes being communicated to front end team - Integrate front and back ends - Ship - Repeat Schema App Api Product
  48. GraphQL Dev Workflow - Front end and back end teams

    agree on schema. - UI is developed using mocked data based on schema - API is built out with any changes being communicated to front end team - Integrate front and back ends - Ship - Repeat Schema App Api Product
  49. { real { data } fake { data } }

    { real { data } } { real { data: “resolved” } } { real { data: “resolved” } fake { data: “stubbed” } } IDL application/graphql application/json GraphQL Faker facilitates low friction experimentation when modelling schema. It’s quicker to change IDL than the API.
  50. Resources - Official Docs: http://graphql.org - Specification: https://github.com/facebook/graphql - Reference:

    https://github.com/graphql/graphql-js - Community: http://graphql.org/community - Awesome List: https://github.com/chentsulin/awesome-graphql - Humble Additions - Code: https://github.com/robcrowley/graphql-demo - Slides: https://bit.ly/ndcsydney-graphql