Slide 1

Slide 1 text

Introduction to GraphQL Radoslav Stankov 08/10/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

No content

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

GET /api/posts?date=2015-10-31 { id: 1, title: "Post title", tagline: "Post headline", votes_count: 1, comments_count: 1, url: '/category/slug', thumbnail_url: 'assets.producthunt.com/uuid', hunter_id: 1 }

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

GET /api/users/1 { id: 1, name: 'User Name', handle: 'user_name', avatar_url: 'assets.producthunt.com/uuid' }

Slide 16

Slide 16 text

Rest Client Rest Server

Slide 17

Slide 17 text

Rest Client Rest Server /api/posts?date=2015-10-31

Slide 18

Slide 18 text

Rest Client Rest Server /api/posts?date=2015-10-31 /api/users/{x} (for X users)

Slide 19

Slide 19 text

Rest Client Rest Server /api/posts?date=2015-10-31 /api/users/{x} (for X users)

Slide 20

Slide 20 text

…approach 2

Slide 21

Slide 21 text

GET /api/posts?date=2015-10-31 { id: 1, title: "Post title", tagline: "Post headline", votes_count: 1, comments_count: 1, url: '/category/slug', thumbnail_url: 'assets.producthunt.com/uuid', hunter: { id: 1, name: 'User Name', handle: 'user_name', avatar_url: 'assets.producthunt.com/uuid' } }

Slide 22

Slide 22 text

No content

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/posts?date=2015-10-31 { id: 1, title: "Post title", tagline: "Post headline", votes_count: 1, comments_count: 1, url: '/category/slug', thumbnail_url: 'assets.producthunt.com/uuid', hunter: { id: 1, name: 'User Name', handle: 'user_name', avatar_url: 'assets.producthunt.com/uuid' }, makers: [{ id: 2, name: 'Second User', handle: 'second_user', avatar_url: 'assets.producthunt.com/uuid' }] }

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

GET /api/posts?date=2015-10-31 { id: 1, title: "Post title", tagline: "Post headline", votes_count: 1, comments_count: 1, url: '/category/slug', thumbnail_url: 'assets.producthunt.com/uuid', hunter: { id: 1, name: 'User Name', handle: 'user_name', avatar_url: 'assets.producthunt.com/uuid' }, makers: [{ id: 2, name: 'Second User', handle: 'second_user', avatar_url: 'assets.producthunt.com/uuid' }], platform: [{ name: 'iPhone', }, { name: 'Mac', }, { name: 'Web', }] }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

…approach 3

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Rest Client Rest Server

Slide 38

Slide 38 text

Rest Client Rest Server endpoint 1 feature 1

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

{ id: 1, title: "Post title", tagline: "Post headline", votes_count: 1, hunter: { id: 1, avatar_url: 'assets.producthunt.com/uuid' }, makers: [{ id: 2, avatar_url: 'assets.producthunt.com/uuid' }] }

Slide 49

Slide 49 text

{ id title tagline votes_count hunter { id avatar_url } makers { id avatar_url } }

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

GET /graphql Request Body Response Body

Slide 52

Slide 52 text


 query { post(id: 1){ id title tagline votes_count hunter { id avatar_url } makers { id avatar_url } } } GET /graphql Request Body Response Body

Slide 53

Slide 53 text


 query { post(id: 1){ id title tagline votes_count hunter { id avatar_url } makers { id avatar_url } } } GET /graphql Request Body Response Body

Slide 54

Slide 54 text


 query { post(id: 1){ id title tagline votes_count hunter { id avatar_url } makers { id avatar_url } } } GET /graphql Request Body Response Body { "data": { "post": { "id": 1, "title": "title", "tagline": "tagline", "votes_count": 0, "hunter": { "id": 1, "avatar_url": "assets.producthu }, "makers": [ { "id": 1, "avatar_url": "assets.product } ] } } }

Slide 55

Slide 55 text

It’s that simple

Slide 56

Slide 56 text


 https://facebook.github.io/graphql


Slide 57

Slide 57 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 58

Slide 58 text

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


Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

QueryType = GraphQL::ObjectType.define do name 'Query' description 'The query root for this schema' field :post do type PostType description 'The post' argument :id, types.Int, 'Post id' resolve -> (obj, args, context) do Post.find args['id'] end end end Ruby Example

Slide 62

Slide 62 text

PostType = GraphQL::ObjectType.define do name 'Post' field :id, types.Int field :title, types.String field :tagline, types.String field :votes_count, types.Int field :comments_count, types.Int field :thumbnail_url, types.String field :hunter do type -> { UserType } resolve -> (obj, args, context) do obj.user end end field :makers do type -> { types[UserType] } resolve -> (obj, args, context) do obj.makers end end end Ruby Example

Slide 63

Slide 63 text

Query 
 query { post(id: 1){ id title tagline votes_count hunter { id avatar_url } makers { id avatar_url } } } { "data": { "post": { "id": 1, "title": "title", "tagline": "tagline", "votes_count": 0, "hunter": { "id": 1, "avatar_url": "assets.producthun }, "makers": [ { "id": 1, "avatar_url": "assets.producth } ] } } }

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Documentation 
 query { __schema { types { name fields { name type { name kind ofType { name kind } } } } } } { "data": { "__schema": { "types": [ { "name": "Query", "fields": [ { "name": "user", "type": { "name": "Non-Null", "kind": "NON_NULL", "ofType": { "name": "User", "kind": "OBJECT" } } }, { "name": "post", "type": { "name": "Non-Null", "kind": "NON_NULL", "ofType": { "name": "Post", "kind": "OBJECT" } } } ] },

Slide 66

Slide 66 text

{ id hunter: { id name
 small_avatar_url: avatar_url(size: 100)
 big_avatar_url: avatar_url(size: 400) } } Cool tricks

Slide 67

Slide 67 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 68

Slide 68 text


 https://facebook.github.io/react


Slide 69

Slide 69 text


 https://facebook.github.io/relay


Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

PostContainer = Relay.createContainer(Post, { fragments: { post: () => Relay.QL` fragment on Post { id title tagline votes_count } `, }, }); Relay

Slide 72

Slide 72 text

Demo

Slide 73

Slide 73 text

Issues • New, just a draft specification • Mutations are weird • Best practices and solutions • Large datasets

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

https://speakerdeck.com/rstankov/introduction-to-graphql

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

@rstankov Thanks :)

Slide 80

Slide 80 text

Questions?