Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
APIs The New Way
Volodymyr Tsukur
API,
Volodymyr Tsukur
not DB!
Volodymyr Tsukur
Lead Engineer,
Guild Master @
Partner @
EASYHUNT
Program Committee @
JEEConf, XP Days
flushdia vtsukur
Lithuania
Ukraine
Kyiv
Dnipro
Vilnius
Локации Wix Engineering
Israel
Tel Aviv
Be’er Sheva
~ 110M+ users*
~ 700 engineers
~...
title
price
images
description
info
...
{
}
GET /products/:id
imagesimages(1)
priceprice
titletitle
[
]
description
GET /products
...
info
{
}
10%!
OVER-fetching
01
GET /carts/:id
items [
{
product
quantity
total
}
{
}
]
subTotal
GET /carts/1
GET /products/59eb838e3f
GET /products/59eb88c400
GET /products/59eb838e40
- UNDER-fetching
- extra requests
02
GET /carts/1?projection=with-products
...
GET /carts/1?projection=summary
GET /carts/1?projection=
GET /carts/1?projection=
GET /carts/1?projection=with-products
- no unified & widely adopted
standard :( *
- harder to collaborate :(
03
How to @deprecate and
phase out outdated stuff?
04
2012
2015
“... is a query language for APIs and a runtime for
fulfilling those queries with your existing data”
type User {
name: String
friends: [User]
city: City
}
type City {
name: String
population: Int
citizens: [User]
}
1. Defin...
2. Ask for data
user(name: String): User
2. Ask for data
{
user(name: “Vanya Unicorn”) {
friends {
name
}
city {
name
population
}
}
}
3. Get it!
{
user(name: “Vanya Unicorn”) {
friends {
name
}
city {
name
population
}
}
}
{
“data”: {
“user”: {
“friends”: ...
only necessary data
{
“data”: {
“user”: {
“friends”: [
{ “name”: “Lena” },
{ “name”: “Stas” }
]
“city”: {
“name”: “Kyiv”,
...
in one request *
{
“data”: {
“user”: {
“friends”: [
{ “name”: “Lena” },
{ “name”: “Stas” }
]
“city”: {
“name”: “Kyiv”,
“po...
let us see how it works!
WYSIWYG
efficient fetching
easier to develop
analytics
any data source and transport
graphql-java
HTTP
Cart Service
Product Service
let us rock and roll!
running query
{
cart(id: 1) {
items {
product {
title
price
images(limit: 1)
sku
}
quantity
total
}
subTotal
}
}
1)
{
cart(id: 1) {
items {
product {
title
price
images(limit: 1)
sku
}
quantity
total
}
subTotal
}
}
2) SELECT ... FROM Cart...
{
cart(id: 1) {
items {
product {
title
price
images(limit: 1)
sku
}
quantity
total
}
subTotal
}
}
3)
+ n x
product {
titl...
e x p e c t a t i o n s
R
e
a
l
i
t
y
{
cart(id: 1) {
items {
product {
title
price
images(limit: 1)
sku
}
quantity
total
}
subTotal
}
}
+
GET /products?ids=1,2...
let’s fix it!
Security
{
user(name: “Vanya Unicorn”) {
friends {
name
friends {
name
friends {
name
friends {
name
...
}
}
}
}
}
}
limit query depth
limit query complexity
let us defend!
throttling / rate limiting
set timeouts
pagination
schema {
query: Query
}
schema {
query: Query,
mutation: Mutation
}
type Mutation {
addProductToCart(cart: ID!,
product: ID!,
count: Int = 1): Cart
}
let us mutate!
+
+
I want it !!!
but let us see the BAD parts
caching
security
error handling
arbitrary resources / file uploads
operation idempotency?
operation naming
hypermedia
Java ecosystem
Alternatives
https://philsturgeon.uk/api/2017/01/24/graphql-vs-rest-overview/
https://blog.runscope.com/posts/you-might-no...
NO !!!
but it shines
as an
integrated
solution
schema {
query: Query,
mutation: Mutation,
subscription: Subscription
}
type Subscription {
productAdded(cart: ID!): Cart
}
subscription {
productAdded(cart: 123) {
items {
product ...
}
subTotal
}
}
{
“data”: {
“productAdded”: {
“items”: [
{ “pr...
{
“data”: {
“productAdded”: {
“items”: [
{ “product”: ... },
{ “product”: ... },
{ “product”: ... },
{ “product”: ... }
]
...
https://github.com/vtsukur/graphql-java-store
sample, not
production
ready code!
Questions?
volodymyrt@wix.com
flushdia vtsukur
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
GraphQL - APIs The New Way
Upcoming SlideShare
Loading in …5
×

GraphQL - APIs The New Way

975 views

Published on

REST-ish APIs have become the de-facto standard in the last 10 years. But is it always the right choice?

Facebook, GitHub, and Pinterest challenge status quo in network communication and embrace GraphQL - query language for the APIs. This evolving standard also gives you runtime to execute the queries and enables comprehensive dev tooling, making it juicy for a developer to try and explore. That's what we, at Wix, did too!

In this talk, we will see GraphQL in real, practical action under Java sauce. We'll elaborate on our experience with GraphQL at Wix, see how it compares to REST, and, like with every other technology, understand its limits and shortcomings.

So, is GraphQL right for you?

Published in: Technology
  • Be the first to comment

GraphQL - APIs The New Way

  1. 1. APIs The New Way Volodymyr Tsukur
  2. 2. API, Volodymyr Tsukur not DB!
  3. 3. Volodymyr Tsukur Lead Engineer, Guild Master @ Partner @ EASYHUNT Program Committee @ JEEConf, XP Days flushdia vtsukur
  4. 4. Lithuania Ukraine Kyiv Dnipro Vilnius Локации Wix Engineering Israel Tel Aviv Be’er Sheva ~ 110M+ users* ~ 700 engineers ~ 1800 employees ~ 500 microservices
  5. 5. title price images description info ... { } GET /products/:id
  6. 6. imagesimages(1) priceprice titletitle [ ] description GET /products ... info { }
  7. 7. 10%!
  8. 8. OVER-fetching 01
  9. 9. GET /carts/:id items [ { product quantity total } { } ] subTotal
  10. 10. GET /carts/1 GET /products/59eb838e3f GET /products/59eb88c400 GET /products/59eb838e40
  11. 11. - UNDER-fetching - extra requests 02
  12. 12. GET /carts/1?projection=with-products ...
  13. 13. GET /carts/1?projection=summary GET /carts/1?projection= GET /carts/1?projection= GET /carts/1?projection=with-products
  14. 14. - no unified & widely adopted standard :( * - harder to collaborate :( 03
  15. 15. How to @deprecate and phase out outdated stuff? 04
  16. 16. 2012
  17. 17. 2015
  18. 18. “... is a query language for APIs and a runtime for fulfilling those queries with your existing data”
  19. 19. type User { name: String friends: [User] city: City } type City { name: String population: Int citizens: [User] } 1. Define schema { user(name: String): User ... }
  20. 20. 2. Ask for data user(name: String): User
  21. 21. 2. Ask for data { user(name: “Vanya Unicorn”) { friends { name } city { name population } } }
  22. 22. 3. Get it! { user(name: “Vanya Unicorn”) { friends { name } city { name population } } } { “data”: { “user”: { “friends”: [ { “name”: “Lena” }, { “name”: “Stas” } ] “city”: { “name”: “Kyiv”, “population”: 2928087 } } } }
  23. 23. only necessary data { “data”: { “user”: { “friends”: [ { “name”: “Lena” }, { “name”: “Stas” } ] “city”: { “name”: “Kyiv”, “population”: 2928087 } } } } { user(name: “Vanya Unicorn”) { friends { name } city { name population } } }
  24. 24. 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
  25. 25. let us see how it works!
  26. 26. WYSIWYG efficient fetching easier to develop analytics any data source and transport
  27. 27. graphql-java
  28. 28. HTTP Cart Service Product Service
  29. 29. let us rock and roll!
  30. 30. running query
  31. 31. { cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal } } 1)
  32. 32. { cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal } } 2) SELECT ... FROM Cart INNER JOIN Item ...
  33. 33. { 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/1 GET /products/2 GET /products/3
  34. 34. e x p e c t a t i o n s
  35. 35. R e a l i t y
  36. 36. { cart(id: 1) { items { product { title price images(limit: 1) sku } quantity total } subTotal } } + GET /products?ids=1,2,3 @Batched
  37. 37. let’s fix it!
  38. 38. Security
  39. 39. { user(name: “Vanya Unicorn”) { friends { name friends { name friends { name friends { name ... } } } } } }
  40. 40. limit query depth limit query complexity
  41. 41. let us defend!
  42. 42. throttling / rate limiting set timeouts pagination
  43. 43. schema { query: Query }
  44. 44. schema { query: Query, mutation: Mutation }
  45. 45. type Mutation { addProductToCart(cart: ID!, product: ID!, count: Int = 1): Cart }
  46. 46. let us mutate!
  47. 47. + +
  48. 48. I want it !!!
  49. 49. but let us see the BAD parts
  50. 50. caching security error handling arbitrary resources / file uploads
  51. 51. operation idempotency? operation naming hypermedia Java ecosystem
  52. 52. Alternatives https://philsturgeon.uk/api/2017/01/24/graphql-vs-rest-overview/ https://blog.runscope.com/posts/you-might-not-need-graphql
  53. 53. NO !!!
  54. 54. but it shines as an integrated solution
  55. 55. schema { query: Query, mutation: Mutation, subscription: Subscription }
  56. 56. type Subscription { productAdded(cart: ID!): Cart }
  57. 57. subscription { productAdded(cart: 123) { items { product ... } subTotal } } { “data”: { “productAdded”: { “items”: [ { “product”: ... }, { “product”: ... }, { “product”: ... }, { “product”: ... } ] “subTotal”: 289.33 } } } ://
  58. 58. { “data”: { “productAdded”: { “items”: [ { “product”: ... }, { “product”: ... }, { “product”: ... }, { “product”: ... } ] “subTotal”: 289.33 } } } subscription { productAdded(cart: 123) { items { product ... } subTotal } } ://
  59. 59. https://github.com/vtsukur/graphql-java-store sample, not production ready code!
  60. 60. Questions? volodymyrt@wix.com flushdia vtsukur

×