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

DDD 12 - Goodbye REST; Hello GraphQL

DDD 12 - Goodbye REST; Hello GraphQL

Do you want a single endpoint to access your data? Fetch only the data your client needs all in a single request? Do away with API versioning? Well if the answer is yes to any of these questions then GraphQL might just be what you are looking for. GraphQL is a query language for your application and not your database. Unlike RESTful API’s, with GraphQL, developers can use composable typed queries to request and receive only what’s required from the server.

We’ll walk through an introduction to GraphQL, drawing comparisons with traditional RESTful API’s, highlighting the implications on server and client design. Live coding examples will be used to illustrate how to get started creating a GraphQL client including: defining a schema, connecting to a database, fetching/manipulating data and much more.


Sandeep Singh

June 13, 2017


  1. GOODBYE REST; HELLO GRAPHQL Sandeep Singh initialspark.co.uk @initial_spark

  2. ◦Modern API technologies & challenges ◦What it is, what it’s

    not and what GraphQL aims to solves? ◦GraphQL core concepts ◦Demo ◦Considerations Agenda @initial_spark
  3. ◦REST ◦SOAP ◦gRPC ◦OData ◦And many more … API Technologies

  4. ◦Architectural style of the web ◦Resources ! a single resource

    e.g. api/patients (nouns) ◦Verbs ! GET, PUT, DELETE & POST ◦HATEOAS (Hypermedia As The Engine Of Application State) REST @initial_spark
  5. Sounds good… So why are we looking at GraphQL? @initial_spark

  6. ◦Efficiency ◦Predictability ◦Versioning ◦Security ◦Caching, tooling, platforms, documentation and more

    Modern API considerations @initial_spark
  7. Lets look at an example… @initial_spark

  8. @initial_spark

  9. Multiple round-trips (Under-fetching) Code Code REST API /patients /medications /allergies

  10. Includes REST API /patients CLIENT api/patients/1?include=meds,allergies … /medications /allergies

  11. Over-fetching REST API /patients CLIENT … api/patients/1?include=meds,allergies

  12. Ad-hoc endpoints Code Code REST API /patients /medications /allergies /patients_summary

  13. Ad-hoc endpoints CLIENT 1 REST API CLIENT 2 /patients_summary /patients_summary_mobile

  14. Building modern APIs can be hard @initial_spark Q: So how

    does GraphQL fit in?
  15. GraphQL is a query language for your API. GraphQL allows

    developers to compose typed queries to request and receive only the data that’s required from the server in a single network request. “ @initial_spark
  16. ◦Built on specification ◦Hierarchical ◦Product centric ◦Strongly typed What is

    GraphQL? @initial_spark (http://graphql.org)
  17. ◦About graph databases ◦Assumes nothing about: ! Transport protocol !

    Data storage ◦A solution for binary streams e.g. file upload ◦Limited to JavaScript What GraphQL isn’t @initial_spark
  18. Clients Server Relay Apollo

  19. Q: What problems does it help us solve? @initial_spark

  20. Efficiency @initial_spark

  21. Efficiency query{ patient(id:"22344667"){ id firstName surname dateOfBirth gender nhsNumber medications(top:5){

    name dose prescribedOn } allergies { type recorded severity } } } { "data": { "patient": { "id": 1, "firstName": "Tom", "surname": "Smith", "dateOfBirth": "12/12/1980", "gender": "MALE", "nhsNumber": "12345678911", "medications": [ { "dose": "500mg twice a day", "name": "Amoxicillin", "prescribedOn": "10/01/2016" }, { "dose": "10mg once a day", "name": "Prednisolone", "prescribedOn": "01/05/2011" } ], "allergies":[] } } }
  22. Efficiency GraphQL API CLIENT 1 CLIENT 2

  23. Versioning Evolution @initial_spark

  24. Versioning @initial_spark (http://graphql.org)

  25. Tooling & Documentation @initial_spark

  26. Tooling & Documentation @initial_spark

  27. REST GraphQL Persistence Business Logic HTTP Authentication Authorisation Technology Stack

  28. REST GraphQL Conceptual Model Resources Graph Related operations Yes No

    Introspection No Yes Data typing Weak Strong Real-Time No Yes Comparison @initial_spark
  29. GraphQL Core Concepts @initial_spark

  30. Schema Type System Operations GraphQL Core Concepts

  31. Type system @initial_spark • Object type • Scalar types •

    Enumeration Types • Lists • Interfaces • Unions type Patient{ id:Integer identifier:String! firstName:String surname:String dateOfBirth:String isDeceased:Boolean medications: [MedicationType] }
  32. Operations | queries @initial_spark

  33. Operations – Queries @initial_spark

  34. Operations | mutations @initial_spark

  35. Operations - Mutations @initial_spark

  36. Operations - Resolve @initial_spark • Call business logic • Map

    object • Call existing REST API • Query and mutate data • Applies to all fields { type: PatientType, resolve(obj, {args}, ctx) { return ctx.db.getPatient(args); } };
  37. Schema @initial_spark

  38. Schema @initial_spark const schema = new GraphQLSchema({ query: new GraphQLObjectType({

    name: 'RootQueryType', fields: () => ({ patient: PatientQueries.patient, allPatients: PatientQueries.allPatients, medications: PatientQueries.medications, allergies: PatientQueries.allergies }) }), mutation: new GraphQLObjectType({ name: 'RootMutation', fields: () => ({ addPatient: AddPatientMutation, deletePatient: DeletePatientMutation }) }) });
  39. DEMO @initial_spark

  40. Considerations @initial_spark

  41. Caching ◦ Client and app server ◦ Can’t use network

    caching e.g. Varnish, Squid etc ◦ Solution: Cache queries (normalised cache)
  42. Performance ◦ Optimise queries/mutations ◦ N+1 problem ◦ Solution: Loaders

    - Batching
  43. Security ◦ Don’t expose anything you don’t want to be

    public ◦ Malicious queries ◦ Solution: Timeouts, max query depth/query complexity analysis
  44. Error handling ◦ Can’t use HTTP codes to provide contextual

    information ◦ Surfacing errors to user(s) ◦ Solution: Validation, return errors in response object
  45. Q: Use cases? @initial_spark

  46. ◦http://graphql.org/learn/ ◦https://github.com/chentsulin/awesome-graphql ◦https://github.com/apollographql/apollo-client ◦https://github.com/initialspark/goodbye-rest-hello-graphql ◦http://graphql.org/swapi-graphql/ Resources @initial_spark

  47. Thank you ! You can find me at @initial_spark &