Slide 1

Slide 1 text

From RESTful to GraphQL José María Rodríguez Hurtado @durbon [email protected] 1 #CodemotionMadrid

Slide 2

Slide 2 text

2 I’m Chema Android Team Lead at @JobandtalentEng Editor Genbeta @durbon

Slide 3

Slide 3 text

3 “Instead of working with rigid server-defined endpoints, you can send queries to get exactly the data you’re looking for in one request”

Slide 4

Slide 4 text

Agenda Reviewing RESTful Working on product teams with APIs GraphQL 4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

¿Using REST correctly? Nick Schrock. React London Meetup. 2015 6

Slide 7

Slide 7 text

Reviewing RESTful - Define separate resources. Fetch via URI - These resources are manipulated using HTTP requests where the method (GET, POST, PUT, PATCH, DELETE) has specific meaning. - Discover resources, thanks to HATEOAS (Hypertext As The Engine Of Application State). 7

Slide 8

Slide 8 text

8 https://medium.com/@leeshapton/mental-maps-for-teaching-graphql-to-beginners-9db9b85ac957

Slide 9

Slide 9 text

Collections of API RESTFul Endpoint resources GET /posts - Retrieves a list of posts GET /posts/12 - Retrieves a specific post GET /posts/12/comments - Retrieves comments GET /posts/12/comments/42/ - Retrieve specific comment POST /posts - Creates a new post PUT /posts/12 - Updates post #12 with payload provided PATCH /posts/12 - Partially updates post #12 DELETE /posts/12 - Deletes post #12 9

Slide 10

Slide 10 text

Example Payload JSON { "name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond", "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/1/", "films": [ "http://swapi.co/api/films/6/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/", "http://swapi.co/api/films/1/", "http://swapi.co/api/films/7/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [ "http://swapi.co/api/vehicles/14/", "http://swapi.co/api/vehicles/30/" ], "starships": [ "http://swapi.co/api/starships/12/", "http://swapi.co/api/starships/22/" 10

Slide 11

Slide 11 text

Problems Working with APIs 11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

13 Where is my API? Where are my endpoints? Why does it not work? How many endpoint will be needed? How many changes should we do?

Slide 14

Slide 14 text

Excessive Round Trips Client - R 14

Slide 15

Slide 15 text

Payload and bandwidth 15

Slide 16

Slide 16 text

/filmsWithDirectorAndTag /filmsWithPrincipalsCharacter /filmSummaries/1 /filmsDetails/1 /filmSummaryWithActors/1 Custom Endpoints or BFF (aka “spaghettification”) 16

Slide 17

Slide 17 text

Versioning APIs: breaking changes 17 What is the correct way to version my API? - The URL way? /api/v1/post/12232 V2, v3, v4….?? - The Header way? GET /api/article/1234 HTTP/1.1 Accept: application/vnd.api.article+xml; version=1.0

Slide 18

Slide 18 text

Why not like REST? - Most REST API are really Ad-hoc RPC with custom endpoint definitions. Time spent maintaining many versioned endpoint - REST requires many requests (many Roundtrips) - With REST, you request too much or too little data REST often turns into a maze of poorly-documented endpoints - Weak Typing and Poor Metadata 18

Slide 19

Slide 19 text

19

Slide 20

Slide 20 text

GraphQL isn't a new technology. Facebook: It has been delivering data to mobile News Feed since 2012 20

Slide 21

Slide 21 text

21

Slide 22

Slide 22 text

22

Slide 23

Slide 23 text

GraphQL is a query language, like SQL? You don’t connect GraphQL directly to your backend or database. It’s an API layer, just like REST. GraphQL can be layered over multiple backends and databases, without the client being aware of where the data is coming from, just like REST. 23

Slide 24

Slide 24 text

“GraphQL is a query language designed to build client applications by providing an intuitive and flexible syntax and system for describing their data requirements and interactions.” - GraphQL Specification https://facebook.github.io/graphql/ 24

Slide 25

Slide 25 text

Single Endpoint /graphql 25

Slide 26

Slide 26 text

26 https://medium.com/@leeshapton/mental-maps-for-teaching-graphql-to-beginners-9db9b85ac957

Slide 27

Slide 27 text

Query Language “hello world” Query { me { name } } { "me": { "name": "@durbon" } } 27

Slide 28

Slide 28 text

{ speaker(id: 27) { name profilePics { width height url } } } { "speaker": { "name": "@durbon" "profilePics" : { "width": 250, "height": 250, "url": "http://avatarurl.com/250.jpg" } } } Ask for you want 28

Slide 29

Slide 29 text

{ speaker(id: 27) { name twitter profilePics(size: 640) { width height url } } } { "speaker": { "name": "Txema Rodríguez" "twitter": "@durbon" "profilePics" : { "width": 640, "height": 640, "url": "http://avatar.com/640.jpg" } } } Ask for you want 29

Slide 30

Slide 30 text

{ speaker(id: 27) { name, licPic: profilePics (size: 90){ width, height, url }, bigPic: profilePics (size: 800){ width, height, url } } } "speaker": { "name": "Txema Rodríguez" "licPic" : { "width": 90, "height": 90, "url": "http://avatar.com/90.jpg" }, "bigPic" : { "width": 800, "height": 800, "url": "http://avatar.com/800.jpg" } } } Ask for you want 30

Slide 31

Slide 31 text

Nested connection resources { me { name talks { title } } } { "me": { "name": "@durbon" "talks" : [ { title: "GraphQL is cool" }, { title: "API REST is dead" }, { title: "Kotlin in Android" },.. 31

Slide 32

Slide 32 text

{ latestTalk { title, speaker { name, twitter } }, allSpeakers { name } } { "data": { "latestTalk": { "title": "¿Por qué deberíamos abandonar REST?", "speaker": { "name": "Txema Rodríguez", "twitter": "@durbon", } }, "allSpeakers": [ { "name": "Txema Rodriguez" }, { "name": "David Bonilla" } ] } } 32 Combining Queries

Slide 33

Slide 33 text

33

Slide 34

Slide 34 text

Mutation: also you can write using GraphQL: mutation { writePost (input : { author: “durbon”, title: “graphql for beginners” content: "it's awesome." }) { id, title } } 34

Slide 35

Slide 35 text

GraphQL Core principles 35

Slide 36

Slide 36 text

Type System GraphQL schema language it's similar to the query language, and allows us to talk about GraphQL schemas in a language-agnostic way. 36

Slide 37

Slide 37 text

Schemas and types - Object types with fields: speakers, talks,... - Scalar types: Int, Float, String, Boolean, ID - Lists and Non-Null - Arguments: every field on a GraphQL object type can have zero or more arguments - Enumeration types: a special kind of scalar that is restricted to a particular set of allowed values 37

Slide 38

Slide 38 text

The GraphQL schema provides a clear contract for client-server communication Client Backend Possibilities GraphQL Type System GraphQL Schema Requirements GraphQL Languages Queries Mutation App Server View 38

Slide 39

Slide 39 text

Schema type Query { me: Speaker speaker (id: Int): Speaker! } type ProfilePicture { width: Int! height: Int! url: String! } type Speaker { name: String! profilePicture(size: Int = 50): ProfilePicture talks: [Talks!] } 39 input TalkInput{ content: String! author: Speaker! } type Mutation { createTalk: (input: TalkInput): Talk } type Talk { author: Speaker! content: String! }

Slide 40

Slide 40 text

Introspection Creating documentation, or rich IDE experiences 40

Slide 41

Slide 41 text

IntrospectionQuery { __type(name: "Speaker") { name description } } { "data": { "__type": { "name": "Speaker", "description": "The speaker of a talk.", } } } 41

Slide 42

Slide 42 text

IntrospectionQuery query IntrospectionQueryTypeQuery { __schema { queryType { name fields { name description type { name kind } } } } } { "data": { "__type": { "name": "Developer", “Description”: “Name developer” "fields": [ { "name": "id", "type": { "name": null, "kind": "NON_NULL" } }, { "name": "name", "type": { "name": null, "kind": "NON_NULL" } 42

Slide 43

Slide 43 text

Documentation by default Generates API documentation for our consumers, and prevents us from spending precious coding time on documentation. 43

Slide 44

Slide 44 text

44

Slide 45

Slide 45 text

Static validation & IDE Integration 45

Slide 46

Slide 46 text

Code Generation: write queries, not code 46

Slide 47

Slide 47 text

Execution After being validated, a GraphQL query is executed by a GraphQL server which returns a result that mirrors the shape of the requested query 47

Slide 48

Slide 48 text

48

Slide 49

Slide 49 text

Database “Microservices” Remote Services whatever your tech stack Your existing code GraphQL Execution 49

Slide 50

Slide 50 text

Object, consisting of two major components: ● the schema definition ● the actual implementation in the form of resolver functions 50 Structure vs Behaviour

Slide 51

Slide 51 text

Think in Graph, not endpoint Fields independently of each other. Apply different context in order to retrieve such data 51

Slide 52

Slide 52 text

Each field on each type is backed by a function called the resolver which is provided by the GraphQL server When a field is executed, the corresponding resolver is called to produce the next value. 52

Slide 53

Slide 53 text

53 https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e

Slide 54

Slide 54 text

54 Every feature you build first manifests itself in the GraphQL schema — then gets implemented through corresponding resolvers it allows frontend developers to start working against a mocked API, before it is actually implemented by backend developers Schema First development

Slide 55

Slide 55 text

55 Catching

Slide 56

Slide 56 text

56

Slide 57

Slide 57 text

57

Slide 58

Slide 58 text

58 Stitching in a gateway

Slide 59

Slide 59 text

59 Stitching in a gateway

Slide 60

Slide 60 text

60

Slide 61

Slide 61 text

61 GraphQL.org

Slide 62

Slide 62 text

Draft RFC Specification for GraphQL https://facebook.github.io/graphql/ 62

Slide 63

Slide 63 text

Reference implementation of GraphQL https://github.com/graphql/graphql-js 63

Slide 64

Slide 64 text

64

Slide 65

Slide 65 text

Javascript Python (graphene) Scala (sangria) Ruby Java Clojure Go PHP C# .NET Elixir Swift …. graphql.org/code/ 65

Slide 66

Slide 66 text

Client library 66 Relay React iOS Android

Slide 67

Slide 67 text

67

Slide 68

Slide 68 text

Public GraphQL APIs 68 Early Github GraphQL API https://developer.github.com/early-access/graphql/explorer/ Yelp https://www.yelp.es/developers/graphql/guides/intro SWAPI http://graphql.org/swapi-graphql/ More APIs https://github.com/APIs-guru/graphql-apis

Slide 69

Slide 69 text

Benefits of Exposing Data Through GraphQL - Boost your client development: combining and Querying Data and Picking Your Payload - Specification: the spec determines the validity, Strongly Typed, hierarchical with nested fields - Structure vs Behaviour. This allows you to focus on separation of concerns, not technologies. 69

Slide 70

Slide 70 text

@durbon 70 Thanks, #codemotionMadrid! We’re hiring candidates.jobandtalent.com/es/careers