What Is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries against a type system that you define for your data.
Unlike a traditional REST API, where the server exposes multiple endpoints such as /users, /users/42, and /users/42/posts, GraphQL generally exposes a single endpoint through which the client describes the data it needs.
The important idea is that the client does not simply ask the server for a resource. It asks for a specific shape of data.
For example, a client might request:
query {
user(id: "42") {
id
name
email
}
}
The response follows the structure requested by the client:
{
"data": {
"user": {
"id": "42",
"name": "Suriya",
"email": "suriya@example.com"
}
}
}
GraphQL itself does not require MongoDB, Node.js, React, Apollo, or any particular programming language. It defines a specification for describing an API schema, querying that schema, validating operations, and executing them.
A GraphQL server typically has three important responsibilities:
- Define the schema that describes available data and operations.
- Validate incoming operations against that schema.
- Execute valid operations using resolver functions.
This separation between the API contract and the underlying data sources is one of GraphQL's most important architectural characteristics.
- Type:
API query language and runtime - :
Client specifies the required data shape - :
/graphql
query GetUser {
user(id: "42") {
id
name
email
}
}GetUser. The user field represents an operation exposed by the GraphQL schema. The id argument identifies the requested user. Inside the selection set, the client explicitly chooses id, name, and email.
GraphQL validates this entire structure before execution. If email is not a field on the User type, the operation is rejected instead of silently returning an unexpected response.- GraphQL is a query language for APIs and a runtime for executing queries against a schema.
- Clients describe the exact fields they need.
- The schema acts as the contract between clients and the server.
- GraphQL is not tied to a specific database or programming language.
What is the primary characteristic that distinguishes a GraphQL query from a typical REST resource request?