Explain REST vs GraphQL

EasyPhone Screen
AmazonFull Stack Engineer
245

Compare REST and GraphQL APIs. What are the pros and cons of each approach? When would you choose one over the other?

1 Answer

189
Top Answer

REST

  • Multiple endpoints (/users, /posts)
  • Fixed data structure per endpoint
  • Over-fetching or under-fetching common
  • HTTP methods define operations (GET, POST, PUT, DELETE)

GraphQL

  • Single endpoint (/graphql)
  • Client specifies exactly what data it needs
  • No over-fetching or under-fetching
  • All requests are POST with a query

When to use REST:

  • Simple CRUD applications
  • Caching is important (HTTP caching works well)
  • Team is familiar with REST
  • Public APIs with many consumers

When to use GraphQL:

  • Complex data requirements
  • Mobile apps (minimize data transfer)
  • Rapidly evolving frontend needs
  • Multiple clients with different data needs

Example

# GraphQL - get exactly what you need
query {
  user(id: "1") {
    name
    posts {
      title
    }
  }
}

vs REST: 2 requests to /users/1 and /users/1/posts

APIArchitect

Share Your Answer

Help others by sharing your knowledge and experience with this question.

Coming soon...

Related Questions

View all

More from Amazon

View all