Top Banner
APIs The New Way Volodymyr Tsukur
76

GraphQL - APIs The New Way

Jan 21, 2018

Download

Technology

Vladimir Tsukur
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: GraphQL - APIs The New Way

APIs The New Way

Volodymyr Tsukur

Page 2: GraphQL - APIs The New Way

API,

Volodymyr Tsukur

not DB!

Page 3: GraphQL - APIs The New Way

Volodymyr Tsukur

Lead Engineer,Guild Master @

Partner @EASYHUNT

Program Committee @JEEConf, XP Days

flushdia vtsukur

Page 4: GraphQL - APIs The New Way

Lithuania

Ukraine

Kyiv

Dnipro

Vilnius

Локации Wix Engineering

Israel

Tel Aviv

Be’er Sheva

~ 110M+ users*

~ 700 engineers~ 1800 employees

~ 500 microservices

Page 5: GraphQL - APIs The New Way
Page 6: GraphQL - APIs The New Way
Page 7: GraphQL - APIs The New Way

titlepriceimagesdescriptioninfo...

{

}

GET /products/:id

Page 8: GraphQL - APIs The New Way

imagesimages(1)pricepricetitletitle

[

]

description

GET /products

...info

{

}

Page 9: GraphQL - APIs The New Way

10%!

Page 10: GraphQL - APIs The New Way

OVER-fetching

01

Page 11: GraphQL - APIs The New Way

GET /carts/:id

items [ { product quantity total }

{

}

]subTotal

Page 12: GraphQL - APIs The New Way

GET /carts/1

GET /products/59eb838e3f

GET /products/59eb88c400

GET /products/59eb838e40

Page 13: GraphQL - APIs The New Way

- UNDER-fetching- extra requests

02

Page 14: GraphQL - APIs The New Way

GET /carts/1?projection=with-products

...

Page 15: GraphQL - APIs The New Way

GET /carts/1?projection=summary

GET /carts/1?projection=

GET /carts/1?projection=

GET /carts/1?projection=with-products

Page 16: GraphQL - APIs The New Way

- no unified & widely adopted standard :( *

- harder to collaborate :(

03

Page 17: GraphQL - APIs The New Way
Page 18: GraphQL - APIs The New Way

How to @deprecate andphase out outdated stuff?

04

Page 19: GraphQL - APIs The New Way
Page 20: GraphQL - APIs The New Way

2012

Page 21: GraphQL - APIs The New Way
Page 22: GraphQL - APIs The New Way
Page 23: GraphQL - APIs The New Way

2015

Page 24: GraphQL - APIs The New Way

“... is a query language for APIs and a runtime for fulfilling those queries with your existing data”

Page 25: GraphQL - APIs The New Way

type User { name: String friends: [User] city: City}

type City { name: String population: Int citizens: [User]}

1. Define schema

{ user(name: String): User ...}

Page 26: GraphQL - APIs The New Way

2. Ask for data user(name: String): User

Page 27: GraphQL - APIs The New Way

2. Ask for data{ user(name: “Vanya Unicorn”) { friends { name } city { name population } }}

Page 28: GraphQL - APIs The New Way

3. Get it!{ user(name: “Vanya Unicorn”) { friends { name } city { name population } }}

{ “data”: { “user”: { “friends”: [ { “name”: “Lena” }, { “name”: “Stas” } ] “city”: { “name”: “Kyiv”, “population”: 2928087 } } }}

Page 29: GraphQL - APIs The New Way

only necessary data{ “data”: { “user”: { “friends”: [ { “name”: “Lena” }, { “name”: “Stas” } ] “city”: { “name”: “Kyiv”, “population”: 2928087 } } }}

{ user(name: “Vanya Unicorn”) { friends { name } city { name population } }}

Page 30: GraphQL - APIs The New Way

in one request *

{ “data”: { “user”: { “friends”: [ { “name”: “Lena” }, { “name”: “Stas” } ] “city”: { “name”: “Kyiv”, “population”: 2928087 } } }}

{ user(name: “Vanya Unicorn”) { friends { name } city { name population } }}

HTTP POST

Page 31: GraphQL - APIs The New Way

let us see how it works!

Page 32: GraphQL - APIs The New Way

WYSIWYG

efficient fetching

easier to develop

analytics

any data source and transport

Page 33: GraphQL - APIs The New Way
Page 34: GraphQL - APIs The New Way

graphql-java

Page 35: GraphQL - APIs The New Way

HTTP

Cart Service

Product Service

Page 36: GraphQL - APIs The New Way

let us rock and roll!

Page 37: GraphQL - APIs The New Way
Page 38: GraphQL - APIs The New Way

running query

Page 39: GraphQL - APIs The New Way

{ cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal }}

1)

Page 40: GraphQL - APIs The New Way

{ cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal }}

2) SELECT ... FROM Cart INNER JOIN Item ...

Page 41: GraphQL - APIs The New Way

{ cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal }}

3)

+ n x

product { title price images(limit: 1) sku }

product { title price images(limit: 1) sku }

GET /products/1GET /products/2GET /products/3

Page 42: GraphQL - APIs The New Way
Page 43: GraphQL - APIs The New Way

e x p e c t a t i o n s

Page 44: GraphQL - APIs The New Way

Reality

Page 45: GraphQL - APIs The New Way

{ cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal }}

+

GET /products?ids=1,2,3@Batched

Page 46: GraphQL - APIs The New Way

let’s fix it!

Page 47: GraphQL - APIs The New Way

Security

Page 48: GraphQL - APIs The New Way

{ user(name: “Vanya Unicorn”) { friends { name friends { name friends { name friends { name ... } } } } }}

Page 49: GraphQL - APIs The New Way
Page 50: GraphQL - APIs The New Way
Page 51: GraphQL - APIs The New Way

limit query depth

limit query complexity

Page 52: GraphQL - APIs The New Way

let us defend!

Page 53: GraphQL - APIs The New Way

throttling / rate limiting

set timeouts

pagination

Page 54: GraphQL - APIs The New Way

schema { query: Query}

Page 55: GraphQL - APIs The New Way

schema { query: Query, mutation: Mutation}

Page 56: GraphQL - APIs The New Way

type Mutation { addProductToCart(cart: ID!, product: ID!, count: Int = 1): Cart}

Page 57: GraphQL - APIs The New Way

let us mutate!

Page 58: GraphQL - APIs The New Way

+

+

Page 59: GraphQL - APIs The New Way
Page 60: GraphQL - APIs The New Way

I want it !!!

Page 61: GraphQL - APIs The New Way

but let us see the BAD parts

Page 62: GraphQL - APIs The New Way

caching

security

error handling

arbitrary resources / file uploads

Page 63: GraphQL - APIs The New Way

operation idempotency?

operation naming

hypermedia

Java ecosystem

Page 64: GraphQL - APIs The New Way
Page 65: GraphQL - APIs The New Way

Alternatives

https://philsturgeon.uk/api/2017/01/24/graphql-vs-rest-overview/

https://blog.runscope.com/posts/you-might-not-need-graphql

Page 66: GraphQL - APIs The New Way

NO !!!

Page 67: GraphQL - APIs The New Way

but it shines as an integrated solution

Page 68: GraphQL - APIs The New Way

schema { query: Query, mutation: Mutation, subscription: Subscription}

Page 69: GraphQL - APIs The New Way

type Subscription { productAdded(cart: ID!): Cart}

Page 70: GraphQL - APIs The New Way

subscription { productAdded(cart: 123) { items { product ... } subTotal }}

{ “data”: { “productAdded”: { “items”: [ { “product”: ... }, { “product”: ... }, { “product”: ... }, { “product”: ... } ] “subTotal”: 289.33 } }}

://

Page 71: GraphQL - APIs The New Way

{ “data”: { “productAdded”: { “items”: [ { “product”: ... }, { “product”: ... }, { “product”: ... }, { “product”: ... } ] “subTotal”: 289.33 } }}

subscription { productAdded(cart: 123) { items { product ... } subTotal }}

://

Page 72: GraphQL - APIs The New Way
Page 73: GraphQL - APIs The New Way
Page 74: GraphQL - APIs The New Way

https://github.com/vtsukur/graphql-java-store

sample, notproduction ready code!

Page 75: GraphQL - APIs The New Way
Page 76: GraphQL - APIs The New Way

Questions?

[email protected]

flushdia vtsukur