Slide 1

Slide 1 text

GraphQL Basics Radoslav Stankov 23/03/2017

Slide 2

Slide 2 text

Radoslav Stankov @rstankov http://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

GET /api/posts?date=2017-03-23 { id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, slug: "slug", thumbnail_url: "assets.producthunt.com/uuid", topicIds: [1, 2, 3] }

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

GET /api/topics/1 { id: 1, name: 'Web', slug: 'web', }

Slide 10

Slide 10 text

Rest Client Rest Server

Slide 11

Slide 11 text

/api/posts?date=2017-03-23 Rest Client Rest Server

Slide 12

Slide 12 text

/api/posts?date=2017-03-23 /api/topics/{x} (for X users) Rest Client Rest Server

Slide 13

Slide 13 text

/api/posts?date=2017-03-23 /api/topics/{x} (for X users) Rest Client Rest Server

Slide 14

Slide 14 text

GET /api/posts?date=2017-03-23 { id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, slug: "slug", thumbnailUrl: "assets.producthunt.com/uuid", topicIds: [1, 2, 3] }

Slide 15

Slide 15 text

GET /api/posts?date=2017-03-23 { id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, slug: "slug", thumbnailUrl: "assets.producthunt.com/uuid", topics: [ { id: 1, name: 'Topic 1', slug: 'topic1' }, { id: 2, name: 'Topic 2', slug: 'topic2' }, { id: 3, name: 'Topic 3', slug: 'topic3' }, ] }

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

GET /api/posts?date=2017-03-23 { id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, slug: "slug", thumbnailUrl: "assets.producthunt.com/uuid", topics: [ { id: 1, name: 'Topic 1', slug: 'topic1' }, { id: 2, name: 'Topic 2', slug: 'topic2' }, { id: 3, name: 'Topic 3', slug: 'topic3' }, ], }

Slide 22

Slide 22 text

GET /api/posts?date=2017-03-23 { id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, slug: "slug", thumbnailUrl: "assets.producthunt.com/uuid", topics: [ { id: 1, name: 'Topic 1', slug: 'topic1' }, { id: 2, name: 'Topic 2', slug: 'topic2' }, { id: 3, name: 'Topic 3', slug: 'topic3' }, ],
 hunter: { id: 1, name: "User Name", handle: "username", avatarUrl: "assets.producthunt.com/uuid" } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

GET /api/posts?date=2017-03-23 { id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, slug: "slug", thumbnailUrl: "assets.producthunt.com/uuid", topics: [ { id: 1, name: 'Topic 1', slug: 'topic1' }, { id: 2, name: 'Topic 2', slug: 'topic2' }, { id: 3, name: 'Topic 3', slug: 'topic3' }, ],
 hunter: { id: 1, name: "User Name", handle: "username', avatarUrl: "assets.producthunt.com/uuid" }, status: "featured", exclusive: null,


Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

…approach 3

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Rest Client Rest Server

Slide 30

Slide 30 text

Rest Client Rest Server endpoint 1 feature 1

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

{ id: 1, title: "Post title", tagline: "Post headline", votesCount: 1, commentsCount: 1, thumbnailUrl: "assets.producthunt.com/uuid",
 hunter: { id: 1, name: "User Name", handle: "username', avatarUrl: "assets.producthunt.com/uuid" } }

Slide 42

Slide 42 text

{ id title tagline votesCount commentsCount thumbnailUrl
 hunter: { id name handle avatarUrl } }

Slide 43

Slide 43 text

{ id title tagline votesCount commentsCount thumbnailUrl
 hunter: { id avatarUrl } }

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

GET /graphql Request Body Response Body

Slide 46

Slide 46 text


 query { post(id: 1) { id title tagline votesCount commentsCount thumbnailUrl
 hunter: { id avatarUrl } } } GET /graphql Request Body Response Body

Slide 47

Slide 47 text


 query { post(id: 1) { id title tagline votesCount commentsCount thumbnailUrl
 hunter: { id avatarUrl } } } GET /graphql Request Body Response Body

Slide 48

Slide 48 text


 query { post(id: 1) { id title tagline votesCount commentsCount thumbnailUrl
 hunter: { id avatarUrl } } } GET /graphql Request Body Response Body { "data": { "post": { "id": 1, "title": "title", "tagline": "tagline", "votesCount": 0, "commentsCount": 0,
 "thumbnailUrl": "assets.producthu "hunter": { "id": 1, "avatarUrl": "assets.producthun } } } }

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text


 https://facebook.github.io/graphql


Slide 51

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

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 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 ARGUMENTS],
 resolve: [FUNCTION] }, } } });

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

Query 
 query { post(id: 1){ id title tagline votesCount hunter { id avatarUrl } makers { id avatarUrl } } } { "data": { "post": { "id": 1, "title": "title", "tagline": "tagline", "votesCount": 0, "hunter": { "id": 1, "avatarUrl": "assets.producthunt }, "makers": [ { "id": 1, "avatarUrl": "assets.producthu } ] } } }

Slide 60

Slide 60 text

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

Slide 61

Slide 61 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) { return data.posts.create(params); }, }, }, });

Slide 62

Slide 62 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 63

Slide 63 text

{ id hunter: { id name
 smallAvatarUrl: avatarUrl(size: 100)
 bigAvatarUrl: avatarUrl(size: 400) } } Cool tricks

Slide 64

Slide 64 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 65

Slide 65 text


 https://github.com/graphql/graphiql


Slide 66

Slide 66 text


 https://facebook.github.io/react


Slide 67

Slide 67 text


 https://facebook.github.io/relay


Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text


 http://www.apollodata.com/


Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

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 } `, }, }); Apollo

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

PostContainer = Relay.createContainer(Post, { fragments: { post: () => Relay.QL` fragment on Post { id title tagline votes_count } `, }, }); Apollo fragment NewsletterCard on Newsletter { name slug imageUrl description }

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

PostContainer = Relay.createContainer(Post, { fragments: { post: () => Relay.QL` fragment on Post { id title tagline votes_count } `, }, }); Apollo fragment PostItem on Post { id title headline slug votesCount commentsCount imageUrl topics { slug name } }

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

query Homepage($page: Int!) { mainMenu { ...MenuItem } latestNewsletter { ...NewsletterCard } latestGiveaway { ...GiveawayCard } homepagePosts(page: $page) { ...PostItem } }

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

https://speakerdeck.com/rstankov/graphql-basics

Slide 82

Slide 82 text


 https://graphql-europe.org/


Slide 83

Slide 83 text

Thanks !

Slide 84

Slide 84 text

No content