Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

◦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

Slide 3

Slide 3 text

○REST ○SOAP ○gRPC ○OData ○And many more … API Technologies @initial_spark

Slide 4

Slide 4 text

○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

Slide 5

Slide 5 text

Sounds good… So why are we looking at GraphQL? @initial_spark

Slide 6

Slide 6 text

○Efficiency ○Predictability ○Versioning ○Security ○Caching, tooling, platforms, documentation and more Modern API considerations @initial_spark

Slide 7

Slide 7 text

Lets look at an example… @initial_spark

Slide 8

Slide 8 text

@initial_spark

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Building modern APIs can be hard @initial_spark Q: So how does GraphQL fit in?

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

○Built on specification ○Hierarchical ○Product centric ○Strongly typed What is GraphQL? @initial_spark (http://graphql.org)

Slide 17

Slide 17 text

○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

Slide 18

Slide 18 text

Clients Server Relay Apollo

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Efficiency @initial_spark

Slide 21

Slide 21 text

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":[] } } }

Slide 22

Slide 22 text

Efficiency GraphQL API CLIENT 1 CLIENT 2

Slide 23

Slide 23 text

Versioning Evolution @initial_spark

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Tooling & Documentation @initial_spark

Slide 26

Slide 26 text

Tooling & Documentation @initial_spark

Slide 27

Slide 27 text

REST GraphQL Persistence Business Logic HTTP Authentication Authorisation Technology Stack @initial_spark

Slide 28

Slide 28 text

REST GraphQL Conceptual Model Resources Graph Related operations Yes No Introspection No Yes Data typing Weak Strong Real-Time No Yes Comparison @initial_spark

Slide 29

Slide 29 text

GraphQL Core Concepts @initial_spark

Slide 30

Slide 30 text

Schema Type System Operations GraphQL Core Concepts

Slide 31

Slide 31 text

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] }

Slide 32

Slide 32 text

Operations | queries @initial_spark

Slide 33

Slide 33 text

Operations – Queries @initial_spark

Slide 34

Slide 34 text

Operations | mutations @initial_spark

Slide 35

Slide 35 text

Operations - Mutations @initial_spark

Slide 36

Slide 36 text

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); } };

Slide 37

Slide 37 text

Schema @initial_spark

Slide 38

Slide 38 text

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 }) }) });

Slide 39

Slide 39 text

DEMO @initial_spark

Slide 40

Slide 40 text

Considerations @initial_spark

Slide 41

Slide 41 text

Caching ◦ Client and app server ◦ Can’t use network caching e.g. Varnish, Squid etc ◦ Solution: Cache queries (normalised cache)

Slide 42

Slide 42 text

Performance ◦ Optimise queries/mutations ◦ N+1 problem ◦ Solution: Loaders - Batching

Slide 43

Slide 43 text

Security ◦ Don’t expose anything you don’t want to be public ◦ Malicious queries ◦ Solution: Timeouts, max query depth/query complexity analysis

Slide 44

Slide 44 text

Error handling ◦ Can’t use HTTP codes to provide contextual information ◦ Surfacing errors to user(s) ◦ Solution: Validation, return errors in response object

Slide 45

Slide 45 text

Q: Use cases? @initial_spark

Slide 46

Slide 46 text

○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

Slide 47

Slide 47 text

Thank you ! You can find me at @initial_spark & initialspark.co.uk