GraphQL Introduction

Last Updated : 10 Mar, 2026

Traditional REST APIs handle client–server communication well, but modern applications often need a more flexible and efficient way to fetch data. GraphQL addresses this by allowing clients to request exactly the data they need in a single query.

  • GraphQL is an open-source query language for APIs and a server-side runtime for executing queries.
  • The GraphQL server processes queries and returns only the requested data.
  • Unlike REST, where multiple endpoints may be required, GraphQL retrieves specific data through a single query.
  • It was developed by Facebook and later open-sourced for public use.

Example: Suppose we have a REST API for a blog. If we want to get a blog post and its author information then We have to make two separate requests to the server:

  1. one for the blog post.
  2. another for the author's details.

But In GraphQL, we will request both in one query and It will reduce network overhead.

Query:

query {
  post(id: "1") {
    title
    content
    author {
      name
      email
    }
  }
}

Response:

{
  "data": {
    "post": {
      "title": "Introduction to GraphQL",
      "content": "GraphQL is a powerful query language for APIs...",
      "author": {
        "name": "Alice Smith",
        "email": "alice@example.com"
      }
    }
  }
}
  • The query requests a post with ID "1".
  • It also requests the author’s name and email within the same query.
  • The server returns both the post details and author information in a single response, reducing the need for multiple API requests.

Components of GraphQL

GraphQL consists of several core components that define how data is structured, queried, and modified in an API. Understanding these components helps developers build and interact with GraphQL services efficiently.

1. Schema

It defines the data types that can be queried and their relationships. GraphQL uses its own language that is Schema Definition Language (SDL) for writing the schema. It is a human-readable language and It does not depends upon any specific language or framework. Schemas has two main types:

  1. Queries (for retrieving data)
  2. Mutations (for modifying data).

2. Types

GraphQL defines custom types to define the structure of data. There are two main types of Type:

  • Scalar Types: It represent values like integers, strings, booleans, and floats.
  • Object Types: It represent complex objects with fields. Fields can be scalars or other object types. For example, A "User" object type with fields like "id", "name", and "email".

3. Queries

It is used to retrieve data from a GraphQL server. A query specifies the fields and data that the client wants to retrieve. It is similar to GET requests in REST APIs but allow to request exactly the data we need. It is reducing over-fetching or under-fetching.

4. Mutations

It is used to modify data on the server. It can be used for creating, updating, or deleting data. Mutations are similar to POST, PUT, or DELETE requests in REST APIs.

GraphQL Basic Schema Design

The GraphQL schema defines the structure of the API, including the available data types, queries, and mutations. The following example demonstrates a basic schema design for managing books.

type Book {
  id: ID!
  title: String!
  author: String!
}

type Query {
  books: [Book!]!
  book(id: ID!): Book
}

type Mutation {
  createBook(title: String!, author: String!): Book
  updateBook(id: ID!, title: String, author: String): Book
  deleteBook(id: ID!): Boolean
}

Explanation:

We have defined three main types:

1. Book Type:

The Book type represents a book object with three fields:

  • id: An ID field, which is non-nullable (! indicates that it cannot be null).
  • title: A String field, which is non-nullable.
  • author: A String field, which is non-nullable.

2. Query Type:

The Query type defines fields used to retrieve data from the server.

  • books: Returns a list of Book objects, ! indicates a non-null list of non-null Book objects.
  • book(id: ID!): Takes an id argument and returns a single Book object.

3. Mutation Type:

A mutation is used to modify data on the server. Mutations can perform operations such as:

  • createBook: Creates a new book with title and author arguments and returns a Book.
  • updateBook: Updates an existing book with id, title, and author arguments and returns a Book.
  • deleteBook: Deletes a book with the given id and returns a Boolean indicating success.

Working of GraphQL

GraphQL works by allowing clients to send queries to a GraphQL server to request specific data. The server processes these queries and returns only the requested fields in a structured format, usually JSON.

The working process of GraphQL typically involves the following steps:

  1. Client Sends Query: The client sends a query to the GraphQL server specifying the exact data it needs.
  2. Query Validation: The server validates the query using the GraphQL schema to ensure that the requested fields and types are valid.
  3. Resolver Execution: Resolvers fetch the required data from databases, APIs, or other services.
  4. Data Processing: The server collects and organizes the fetched data according to the structure of the query.
  5. Response Sent to Client: The server returns the response in JSON format containing only the requested data.

Features of GraphQL

GraphQL has several features that set it apart from traditional REST APIs, offering developers a more flexible and efficient way to manage data. Let's explore these features as follows:

  1. Flexible Queries: Clients can request exactly the data they need, avoiding over-fetching and under-fetching.
  2. Strongly Typed: GraphQL schemas provide clear data structures and types, reducing runtime errors.
  3. Real-time Updates: GraphQL supports subscriptions for real-time data interactions.
  4. Single Endpoint: Unlike REST, GraphQL typically uses a single endpoint for all data requests
  5. Introspection: Clients can explore the schema's capabilities through introspection queries.
  6. Batching: Multiple queries can be sent in a single request to minimize network overhead.
  7. Efficient for Mobile: GraphQL can be more efficient for mobile devices by reducing data transfer.
  8. Versioning: It eliminates the need for versioning in APIs, as changes can be made without breaking existing clients.

Advantages of GraphQL

GraphQL offers several advantages that make it a popular choice for modern API development.

  • Efficient Data Fetching: Clients can request only the required fields, reducing unnecessary data transfer.
  • Single Endpoint: All queries and operations are handled through a single endpoint, simplifying API management.
  • Strongly Typed Schema: The schema clearly defines available data types and relationships, improving reliability.
  • Reduced Network Requests: Multiple related data can be retrieved in a single query, improving performance.
  • Better Developer Experience: Built-in tools like GraphQL Playground and introspection make APIs easier to explore and test.

GraphQL vs REST API

Here are some key differences between GraphQL and REST API:

GraphQL

REST API

GraphQL uses a single endpoint to perform all operations such as fetching, creating, updating, or deleting data.

REST APIs use multiple endpoints for different resources and operations.

In GraphQL, the client specifies exactly what data it needs in the query.

In REST, the server defines the structure of the response returned by each endpoint.

GraphQL reduces over-fetching and under-fetching of data by returning only the requested fields.

REST APIs may cause over-fetching or under-fetching, requiring multiple requests.

GraphQL supports real-time data updates using subscriptions.

REST APIs usually rely on polling or WebSockets for real-time updates.

GraphQL typically does not require versioning, as clients request only the needed fields.

REST APIs often use versioning (v1, v2, etc.) when the API changes.

GraphQL has a strongly typed schema, which clearly defines available data types and relationships.

REST APIs do not always enforce a strict schema structure.

GraphQL is commonly used in modern applications with complex data requirements.

REST APIs are widely used and have a large, mature ecosystem of tools and libraries.

Comment