Junior — Middle
Explain what GraphQL is and how it differs from REST API.
sobes.tech AI
Answer from AI
GraphQL is a query language for APIs and a runtime environment that allows clients to specify exactly what data they need. Unlike REST, where the server defines the structure and volume of data, GraphQL gives clients the ability to request only the necessary fields, reducing redundancy and the number of requests.
Main differences from REST API:
- Flexible queries: the client constructs a query with the required fields, rather than receiving fixed resources.
- Single entry point: all requests go to one endpoint, rather than multiple URLs.
- Fetching related data in one request: related objects and nested data can be requested in a single query.
Example of a GraphQL query:
{
user(id: "1") {
name
posts {
title
}
}
}
This will return the user's name and the titles of their posts in one request.