Let's Move To GraphQL
on their website
Developed by Facebook in 2012
for their Mobile Apps
Open sourced first spec 2015
Graphql.js
Its a Query Language built on top of REST
but way better
Everything is a single POST call /endpoint
Query is sent as post body
Server defines a schema and relationship between objects
query {
users {
name
}
likes {
id
}
}
Sample Call
curl 'http://localhost/graphql?' \
-XPOST \
--data-binary
'query { users: { name } likes: { id } }'
users {
name : String
likes {
id: Int
}
}
likes {
id: Int
}
Sample Schema
query {
users {
name
likes: {
id
}
}
}
Sample Call
Who Wants What
Users
Faster Products
Developer
More Free Time
Sales
More Customers
Support
No Hard User Issues
Faster Products
More Customers
More Free Time
No Hard User Issues
Technical Writers
Lesser Documenting
Lesser Documenting
Use Case 1
Just Make Another Call
GET /users
GET /likes/1
GET /likes/2
GET /likes/3
No of Calls: 4
REST
3
Prathik
4
Saurabh
5
Armaan
When user list and likes are separate calls
System will Collapse
query {
users {
name
profilepic
bio
likes: {
id
}
}
}
No of Calls: 1
GraphQL
3
Prathik
4
Saurabh
5
Armaan
Use Case 2
Just Rename That Field
[{
name
profilepic
bio
}]
[{
name
pic
bio
}]
Things Change Everywhere
REST
When API renames a field
query {
users: {
name
profilepic: pic
bio
}
}
No Code Change
GraphQL
Use Case 3
Just Play Around
Its takes time to write documentation
REST
Its painful to keep it up to date
REST
Its tough to agree on one tool
Provides documentation free of cost
GraphQL
Swagger, Postman, GitBook all in one
GraphQL
Use Case 4
I Don't Need That
REST
GET /users
GET /users-without-pic
or
GET /users?includepic=false
Prathik
3
Saurabh
4
Armaan
33
Loki
300
3
Prathik
4
Saurabh
When you want to skip a field
query {
users: {
name
bio
pic
likes: {
id
}
}
}
GraphQL
query {
users: {
name
likes: {
id
}
}
}
Prathik
3
Saurabh
4
Armaan
33
Loki
300
3
Prathik
4
Saurabh
Client needs to understand single auth mechanism
Plus all advantages of single endpoint
Links
Namaste
Thank You
Lets Move to GraphQL
By Prathik S
Lets Move to GraphQL
- 477