Slide 1

Slide 1 text

GraphQL Stefan Kanev http://skanev.com/ @skanev Slovenia Ruby User Group 28 March 2018 Ljubljana Why is it exciting?

Slide 2

Slide 2 text

Hi, I’m Stefan

Slide 3

Slide 3 text

skanev skanev skanev.com about.me/skanev

Slide 4

Slide 4 text

Chief Technical Officer @

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Despite the management title, I still tend to think of myself as a programmer

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

This should be part talk, part Q&A

Slide 9

Slide 9 text

1 2 3 4 Walkthrough Demonstration Benefits React and Relay

Slide 10

Slide 10 text

1 Walkthrough

Slide 11

Slide 11 text

It’s not a technology It’s a standard

Slide 12

Slide 12 text

queries vs. mutations “gets” data changes stuff

Slide 13

Slide 13 text

queries vs. mutations “gets” data changes stuff

Slide 14

Slide 14 text

Queries data on the server is represented as a graph 
 the client specifies which part of the graph to fetch 
 server returns only those parts 
 the input is in a special language 
 the output is in JSON

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

query { hero { name friends { name } } } Query { "data": { "hero": { "name": "R2-D2", "friends": [ { "name": "Luke Skywalker" }, { "name": "Han Solo" }, { "name": "Leia Organa" } ] } } } Response

Slide 18

Slide 18 text

Query query { hero { name appearsIn friends { name appearsIn } } } Response { "data": { "hero": { "name": "R2-D2", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"], "friends": [ { "name": "Luke Skywalker", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"] }, { "name": "Han Solo", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"] }, { "name": "Leia Organa", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"] } ] } } }

Slide 19

Slide 19 text

Schema the graph is composed of objects 
 each object is of a specific type 
 each type defines a number of fields 
 each field has a type 
 there is a form of polymorphism

Slide 20

Slide 20 text

type Character { name: String! friends: [Character]! appearsIn: [Episode]! } type Character { name: String! friends: [Character!]! appearsIn: [Episode!]! } By the way, this… …is better in a different way: Anybody see the difference?

Slide 21

Slide 21 text

queries vs. mutations “gets” data changes stuff

Slide 22

Slide 22 text

Mutations each mutation is a separate endpoint 
 it’s RPC-like (conceptually similar to REST mutations) 
 it still goes through the GraphQL language 
 we’ll see an example later

Slide 23

Slide 23 text

2 Demonstration

Slide 24

Slide 24 text

3 Benefits

Slide 25

Slide 25 text

Caveat emptor It might be a better fit for when you control the client, as opposed to letting anybody create a client 
 The complexity on the backend is a magnitude bigger

Slide 26

Slide 26 text

Self-documenting

Slide 27

Slide 27 text

Decouples clients from backend once the graph is large enough, clients won’t need to request (as many) endpoints for reading 
 it’s closer to “write once”

Slide 28

Slide 28 text

Versionless evolution Versioning APIs is a bitch 
 You can get away with not doing it to a large extent 
 (example)

Slide 29

Slide 29 text

Traffic optimisation You get only what you asked for 
 You can get multiple things with a single request

Slide 30

Slide 30 text

Structure It provides a lot of structure to build 3rd party tools on
 (especially in comparison to REST) 
 GraphiQL is a good example 
 Apollo Engine is another one 
 Relay is the most interesting example

Slide 31

Slide 31 text

4 React and Relay

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

?