Introduction
GraphQL and REST are two ways to get data from a server. Knowing their differences helps you choose the best way for your app.
GraphQL query example:
{
user(id: "1") {
name
email
}
}
REST example:
GET /users/1
Response:
{
"name": "Alice",
"email": "alice@example.com"
}GraphQL query to get user name and email: { user(id: "1") { name email } }
REST request to get user data: GET /users/1
GraphQL query to get user and their posts: { user(id: "1") { name posts { title } } }
REST requests to get user and posts: GET /users/1 GET /users/1/posts
# GraphQL query example
query GetUserAndPosts {
user(id: "1") {
name
email
posts {
title
}
}
}
# REST example calls
GET /users/1
GET /users/1/postsquery { book(id: "1") { title author { name } } }query { user(id: 5) { name, email } }Pass the test and this pattern moves unknown