Slide 1

Slide 1 text

Implementing GraphQL with
 JavaScript Radoslav Stankov 21/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

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 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Rest Client Rest Server

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

…approach 2

Slide 18

Slide 18 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 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

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

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 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 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 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

…approach 3

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Rest Client Rest Server

Slide 35

Slide 35 text

Rest Client Rest Server endpoint 1 feature 1

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

No content

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

No content

Slide 45

Slide 45 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 46

Slide 46 text

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

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

GET /graphql Request Body Response Body

Slide 49

Slide 49 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 50

Slide 50 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 51

Slide 51 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 52

Slide 52 text

It’s that simple

Slide 53

Slide 53 text


 https://facebook.github.io/graphql


Slide 54

Slide 54 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 55

Slide 55 text

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


Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Code var RootQueryType = new GraphQLObjectType({ name: 'RootQueryType', fields: { post: { type: PostType, description: 'Find post by id', args: { id: { description: 'Post id' type: new GraphQLNonNull(GraphQLInt) } }, resolve: function(_, params) { return data.posts.find(params.id); } }, });

Slide 59

Slide 59 text

Code var PostType = new GraphQLObjectType({ name: 'Post', fields: function() { return { id: { type: GraphQLInt, }, title: { type: GraphQLString, }, user: { type: UserType, description: "Creator of the post", resolve: function(post) { return data.users.find(post.user_id); } }, }; } });

Slide 60

Slide 60 text

Code var [TYPE] = new GraphQLObjectType({ name: [NAME],
 description: [DESCRIPTION], fields: function() { return { [FIELD_NAME]: { description: [DESCRIPTION], deprecation: [DEPRECATION MARKER], type: [TYPE],
 args: [LIST OF FIELD ARGUMENT],
 resolve: [FUNCTION] }, } } });

Slide 61

Slide 61 text

Code var PostType = new GraphQLObjectType({ name: 'Post', fields: function() { return { id: { type: GraphQLInt,
 resolve: function(post) { return post.id; } }, user: { type: UserType, resolve: function(post) { return data.users.find(post.user_id); } }, }; } });

Slide 62

Slide 62 text

Code var PostType = new GraphQLObjectType({ name: 'Post', fields: function() { return { id: { type: GraphQLInt }, user: { type: UserType, resolve: function(post) { return data.users.find(post.user_id); } }, }; } });

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

Code var RootMutationType = new GraphQLObjectType({ name: 'RootMutationType', fields: { createPost: { type: PostType, args: { title: { type: new GraphQLNonNull(GraphQLString) }, tagline: { type: new GraphQLNonNull(GraphQLString) }, user_id: { type: new GraphQLNonNull(GraphQLInt) }, }, resolve: function(_, params) { if (!data.users.find(params.user_id)) { throw "Invalid user id - " + params.user_id; } return data.posts.create(params); }, }, }, });

Slide 66

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

Slide 67 text

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

Slide 68

Slide 68 text

Code var UserType = new GraphQLObjectType({ name: 'User', fields: function() { return { // ... some field definitions ... picture: { type: GraphQLString, args: { size: { type: GraphQLInt }, }, resolve: function(user, params) { var size = params.size || 400; return 'file.example.com/' + user.picture_uuid + "?size=" + size; } }, }; } });

Slide 69

Slide 69 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 70

Slide 70 text


 https://facebook.github.io/react


Slide 71

Slide 71 text


 https://facebook.github.io/relay


Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Demo

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

@rstankov Thanks :)

Slide 82

Slide 82 text

Questions?