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
Tweet

More Decks by Sandeep Singh

Other Decks in Technology

Transcript

  1. ◦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
  2. ◦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
  3. 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
  4. ◦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
  5. 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":[] } } }
  6. REST GraphQL Conceptual Model Resources Graph Related operations Yes No

    Introspection No Yes Data typing Weak Strong Real-Time No Yes Comparison @initial_spark
  7. 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] }
  8. 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); } };
  9. 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 }) }) });
  10. Caching ◦ Client and app server ◦ Can’t use network

    caching e.g. Varnish, Squid etc ◦ Solution: Cache queries (normalised cache)
  11. Security ◦ Don’t expose anything you don’t want to be

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

    information ◦ Surfacing errors to user(s) ◦ Solution: Validation, return errors in response object