RESTful APIs vs GraphQL APIs by Example

Samer Buna
Mar 11, 2016 · Unlisted
{
"data": {
"person": {
"name": "Darth Vader",
"birthYear": "41.9BBY",
"planet": {
"name": "Tatooine"
},
"films": [
{ "title": "A New Hope" },
{ "title": "The Empire Strikes Back" },
{ "title": "Return of the Jedi" },
{ "title": "Revenge of the Sith" }
]
}
}
}
// The Container Component:
<PersonProfile person={data.person} ></PersonProfile>
// The PersonProfile Component:
Name: {person.name}
Birth Year: {person.birthYear}
Planet: {person.planet.name}
Films: {person.films.map(film => film.title)}

GET - /people/{id}
{
"name": "Darth Vader",
"birthYear": "41.9BBY",
"planetId": 1
"filmIds": [1, 2, 3, 6],
*** other information we do not need ***
}
GET - /planets/1
GET - /films/1
GET - /films/2
GET - /films/3
GET - /films/6
GET - /people/{id}/films

GET or POST - /graphql?query={...}
{
person(ID: ...) {
name,
birthYear,
planet {
name
},
films {
title
}
}
}
{
person(personID: 4) {
name,
birthYear,
homeworld {
name
},
filmConnection {
films {
title
}
}
}
}

If you’re interested to learn more about GraphQL (and Relay), I wrote a book about them.


jsComplete EdgeCoders

We write about the new and leading edge technologies with a focus on JavaScript

Unlisted

Samer Buna

Written by

Author for Pluralsight, O'Reilly, Manning, and LinkedIn Learning. Curator of jsComplete.com

jsComplete EdgeCoders

We write about the new and leading edge technologies with a focus on JavaScript