EVERYTHING YOU
WANT KNOW ABOUT
GRAPHQL
Ubiratan Soares
October / 2017
by
IS NOT
vs
WE SHOULD
TALK ABOUT
REST
2000
2017
REST WAS NOT DESIGNED
WITH NOWADAYS
APPLICATIONS, DEVICES
AND USERS IN MIND …
Design
DevelopMeasure
API
AS PRODUCT
HEADACHES FROM
REST
VERSIONING
GET https://api.xyz/v1/products/123456789?arg1=value1&arg2=value2
GET https://api.xyz/v2/products/123456789?arg1=value1&arg2=value2
GET https://api.xyz/v3/products/123456789?arg1=value1&arg2=value2
GET https://api-v1.xyz/products/123456789?arg1=value1&arg2=value2
GET https://api-v2.xyz/products/123456789?arg1=value1&arg2=value2
GET https://api.xyz/products/123456789?arg1=value1&arg2=value2&version=1
GET https://api.xyz/products/123456789?arg1=value1&arg2=value2&version=2
GET https://api.xyz/products/123456789?arg1=value1&arg2=value2
Accept: application/api.version.v1 + json
Accept: application/api.version.v2 + json
V1
V2
REST API
V1
V3
V3
V3
V2
V1
V3
V2
MULTIPLE ENDPOINTS
TO SOLVE A SPECIFIC
NEED FROM CLIENT
ENDPOINT EXPOSES MORE
DATA THAN CLIENT NEEDS
PER HTTP REQUEST
CORE
CONCEPTS
Single Endpoint
“Goodbye" versioning
headaches
1
Well defined contract
Client and server must respect
a schema for effective data
exchange
2
No more overfetching
GraphQL clients can fetch only
the data they really need for a
given purpose
3
4
No more underfetching
GraphQL clients can retrieve all the
data they need into a single HTTP
call : no more chained and/or
parallel HTTP requests
TYPE SYSTEM
AND SCHEMA
Field name != Type name
QUERY (IP)
LOCDATA
city : city
location : location LOCATION
latitude : String
longitude : String
time_zone : String
CITY
geoname_id : String
names : cityNames
ACCEPTS
RETURNS
.
.
.
.
.
.
CITYNAMES
de : String
en : String
ru : String
.
.
.
QUERIES AND
MUTATIONS
{
getLocation(ip: “172.59.226.170") {
country {
names {
en
}
geoname_id
iso_code
}
location {
latitude
longitude
}
}
}
Operation
Root
Query parameter
Data that actually matters
{
getLocation(ip: “172.59.226.170") {
country {
names {
en
}
geoname_id
iso_code
}
location {
latitude
longitude
}
}
}
"For the available data, return the
English name for the country"
{
getLocation(ip: “172.59.226.170") {
country {
names {
en
}
geoname_id
iso_code
}
location {
latitude
longitude
}
}
}
"For the available data, return the latitude and
the longitude from the geolocation info"
{
"data": {
"getLocation": {
"country": {
"names": {
"en": "Brazil"
},
"geoname_id": "3469034",
"iso_code": "BR"
},
"location": {
"latitude": "-23.5733",
"longitude": "-46.6417"
}
}
}
}
QUERY
MORE ABOUT OPERATIONS
• Data payload will mirror the type hierarchy defined on schema
• All data types for queries/mutations must be defined on schema
• Operation can accept parameters and/or variables
• We can form better, readable blocks for operations using aliases and
fragments
• Etc
CLIENT INTENT REST + HTTP GraphQL + HTTP GraphQL Operation
READ RESOURCE GET POST QUERY
CREATE RESOURCE POST POST MUTATION
UPDATE RESOURCE PUT POST MUTATION
REMOVE RESOURCE DELETE POST MUTATION
CAN GRAPHQL
HARM MY
BUSINESS ???
CONCLUSIONS
REMEMBER, FOLKS
• GraphQL is just a spec
• GraphQL aims to solve some of well know issues from REST
• There are lots of great options for clients and servers for GraphQL APIs
• GraphQL is mobile friendly, but it brings its own set of challenges
• GraphQL no longer uses the Triple-BSD + Patents licensing model
UBIRATAN
SOARES
Computer Scientist by ICMC/USP
Software Engineer, curious guy
Google Developer Expert for Android
Teacher, speaker, etc, etc
THANK YOU
@ubiratanfsoares
ubiratansoares.github.io
https://br.linkedin.com/in/ubiratanfsoares

InterCon 2017 - Tudo o que você quer saber sobre GraphQL - Ubiratan Soares