API Fundamentals Course
API Fundamentals
/
Intermediate

What is GraphQL?

Definition

A query language for your API, and a server-side runtime for executing queries. Developed by Facebook, it allows clients to request exactly the data they need and nothing more.

Explain Like I'm New

In REST, the server decides what data to give you. If you ask for a User, you get 50 fields of data even if you only wanted their name. In GraphQL, the CLIENT decides. The client sends a query saying 'Give me User 1, but ONLY give me their name'. The server returns exactly that.

Real World Example

Facebook News Feed. To render the feed in REST, the app would have to fetch the posts, then fetch the authors for each post, then fetch the likes for each post (Overfetching and Underfetching). With GraphQL, the app sends one massive, specific query, and gets all the nested data back in one trip.

Common Use Cases

  • •Complex UI data requirements
  • •Mobile bandwidth saving
  • •Microservice aggregation

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Who originally created the GraphQL specification?

intermediate

  • What problem does GraphQL solve regarding 'Overfetching'?

Flash Cards

Question

Who created?

Click to reveal answer
Answer

Facebook (in 2012, open-sourced in 2015).

Question

What is Overfetching?

Click to reveal answer
Answer

Overfetching is when an API returns way more data than the client actually needs (wasting bandwidth and mobile data). GraphQL completely eliminates this because the client specifies the exact fields it wants.