Slide 1

Slide 1 text

GraphQL Radoslav Stankov 15/08/2015

Slide 2

Slide 2 text

Radoslav Stankov @rstankov http://rstankov.com
 http://blog.rstankov.com http://github.com/rstankov

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

https://github.com/rstankov/it-tour

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

GET /api/events/15/sessions { id: 175, start_at: "11:00", title: "GraphQL", speaker_id: 1, track: 2, }

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

GET /api/speakers/1 { id: "1", name: "Radoslav Stankov" }

Slide 17

Slide 17 text

Rest Client Rest Server

Slide 18

Slide 18 text

Rest Client Rest Server /api/event/15/sessions

Slide 19

Slide 19 text

Rest Client Rest Server /api/event/15/sessions /api/speakers/{x} (for 13 speakers)

Slide 20

Slide 20 text

Rest Client Rest Server /api/event/15/sessions /api/speakers/{x} (for 13 speakers)

Slide 21

Slide 21 text

…approach 2

Slide 22

Slide 22 text

GET /api/events/15/sessions { id: 175, start_at: "11:00", title: "GraphQL", track: 2, speaker: { id: 1, name: "Radoslav Stankov" } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

GET /api/events/15/sessions { id: 175, start_at: "11:00", title: "GraphQL", track: 2, speaker: { id: 1, name: "Radoslav Stankov",
 photo: "/speaker/photo/1/thumb.jpg" } }

Slide 29

Slide 29 text

…but over time

Slide 30

Slide 30 text

{ id: 175, start_at: "11:00", title: "GraphQL", track: 2, description: "... stuff about Rado", speaker: { id: 1, name: "Radoslav Stankov",
 photo: "/speaker/photo/1/thumb.jpg",
 big_photo: "/speaker/photo/1/big.jpg",
 description: "... stuff about Rado", links: [ "http://rstankov.com", "http://blog.rstankov.com", "http://producthunt.com",
 ], GET /api/events/15/sessions

Slide 31

Slide 31 text

id: 175, start_at: "11:00", title: "GraphQL", track: 2, description: "... stuff about Rado", speaker: { id: 1, name: "Radoslav Stankov",
 photo: "/speaker/photo/1/thumb.jpg",
 big_photo: "/speaker/photo/1/big.jpg",
 description: "... stuff about Rado", links: [ "http://rstankov.com", "http://blog.rstankov.com", "http://producthunt.com",
 ], alabla1: "foo bar", alabla2: "foo bar", alabla3: "foo bar" },
 alabla1: "foo bar", alabla2: "foo bar", alabla3: "foo bar" } GET /api/events/15/sessions

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

…approach 3

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Rest Client Rest Server

Slide 36

Slide 36 text

Rest Client Rest Server endpoint 1 feature 1

Slide 37

Slide 37 text

Rest Client Rest Server endpoint 1 feature 1 endpoint 2 feature 2

Slide 38

Slide 38 text

Rest Client Rest Server endpoint 1 feature 1 endpoint 2 feature 2 endpoint 3 feature 3

Slide 39

Slide 39 text

Rest Client Rest Server endpoint 1 feature 1 endpoint 2 feature 2 endpoint 3 feature 3 endpoint N feature N

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

{ id: 175, start_at: "11:00", title: "GraphQL", track: 2, speaker: { id: 1, name: "Radoslav Stankov",
 photo: "/speaker/photo/1/thumb.jpg" } }

Slide 45

Slide 45 text

{ id start_at title track speaker: { id name
 photo } }

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

GET /graphql Request Body Response Body

Slide 48

Slide 48 text


 { event(id: 15) { sessions { id start_at title track speaker { id name picture } } } }
 
 GET /graphql Request Body Response Body

Slide 49

Slide 49 text


 { event(id: 15) { sessions { id start_at title track speaker { id name picture } } } }
 
 GET /graphql Request Body Response Body

Slide 50

Slide 50 text


 { event(id: 15) { sessions { id start_at title track speaker { id name picture } } } }
 
 { "data": { "event": { "sessions": [{ "id": 175, "start_at": "11:00", "title": "GraphQL", "track": 2, "speaker": { "id": 1, "name": "Rado", "picture": null } }] } } } GET /graphql Request Body Response Body

Slide 51

Slide 51 text

It is that simple

Slide 52

Slide 52 text


 https://facebook.github.io/graphql


Slide 53

Slide 53 text

GraphQL is a query language created by Facebook in 2012 which provides a common interface between the client and the server for data fetching and manipulations. The client asks for various data from the GraphQL server via queries.

Slide 54

Slide 54 text

Architecture 
 GraphQL Library
 (Per-Language)
 
 Type
 Definitions
 
 Application
 Code


Slide 55

Slide 55 text

GraphQL • Single endpoint • Not just a library • Application-Layer Protocol • Server agnostic • Strongly-typed • Client-specified queries • Hierarchical

Slide 56

Slide 56 text

Tries to solve • N+1 API queries • API response bloat • Documentation • Ad Hoc Endpoints • Structure issues

Slide 57

Slide 57 text


 https://facebook.github.io/react


Slide 58

Slide 58 text


 https://facebook.github.io/relay


Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Query 
 { event(id: 15) { sessions { id start_at title track speaker { id name picture } } } }
 
 


Slide 61

Slide 61 text

Query 
 { event(id: 15) { sessions { id start_at title track speaker { id name picture } } } }
 
 
 { "data": { "event": { "sessions": [{ "id": 175, "start_at": "11:00", "title": "GraphQL", "track": 2, "speaker": { "id": 1, "name": "Rado", "picture": null } }] } } }

Slide 62

Slide 62 text

Mutation 
 mutation RootMutationType { createSpeaker(name: "Rado") { id } } { "data": { "createSpeaker": { "id": 2 } } }

Slide 63

Slide 63 text

Documentation 
 { __schema { types { name fields { name type { name kind ofType { name kind } } } } } } { "data": { "__schema": { "types": [{ "name": "RootQueryType", "fields": [ { "name": "events", "type": { "name": null, "kind": "LIST", "ofType": { "name": "Event", "kind": "OBJECT" } } }, { "name": "event", "type": {

Slide 64

Slide 64 text

{ id start_at title track speaker: { id name
 smallPhoto: photo(size: 100)
 bigPhoto: photo(size: 400) } } Cool tricks

Slide 65

Slide 65 text

query withFragments { user(id: 4) { friends(first: 10) { ...friendFields } mutualFriends(first: 10) { ...friendFields } } } fragment friendFields on User { id name profilePic(size: 50) } Cool tricks

Slide 66

Slide 66 text

Demo

Slide 67

Slide 67 text

Issues • New, just a draft specification • No reliable graphql server libraries, yet • No reliable graphql client libraries, yet • Mutations are weird • Best practices and solutions • Large datasets

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

http://jsonapi.org/

Slide 71

Slide 71 text

{ "links": { "self": "http://example.com/events/15/sessions", "next": "http://example.com/events/15/sessions?page[offset]=2", "last": "http://example.com/events/15/sessions?page[offset]=10" }, "data": [{ "type": "sesssion", "id": "175", "attributes": { "title": "GraphQL", "track": "2", "start_at": "11:00" }, "relationships": { "speaker": { "links": { "self": "http://varnaconf.com/sessions/175/relationships/speaker", "related": "http://varnaconf.com/speaker/1" }, "data": { "type": "speaker", "id": "1" } }, }, "links": { "self": "http://example.com/sessions/175" } }],

Slide 72

Slide 72 text

"attributes": { "title": "GraphQL", "track": "2", "start_at": "11:00" }, "relationships": { "speaker": { "links": { "self": "http://varnaconf.com/sessions/175/relationships/speaker", "related": "http://varnaconf.com/speaker/1" }, "data": { "type": "speaker", "id": "1" } }, }, "links": { "self": "http://example.com/sessions/175" } }], "included": [{ "type": "speaker", "id": "1", "attributes": { "name": "Radoslav Stankov", "picture": "/photos/1/small.jpg", }, "links": { "self": "http://varnaconf.com/speaker/1" } }]

Slide 73

Slide 73 text

https://speakerdeck.com/rstankov/graphql

Slide 74

Slide 74 text

https://github.com/rstankov/talks-code

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

@rstankov Thanks :)

Slide 78

Slide 78 text

Questions?