I'm always excited to take on new projects and collaborate with innovative minds.

Email

contact@niteshsynergy.com

Website

https://www.niteshsynergy.com/

GraphQL

1. Introduction to GraphQL

🔹 What is GraphQL?

  • A query language for APIs and a runtime for fulfilling those queries with existing data.

  • Developed by Facebook in 2012, released in 2015.

  • Declarative: The client specifies what data it needs, and the server returns exactly that.

🔹 Why was GraphQL created?

  • Pain point in REST APIs:

    • Over-fetching: Receiving more data than needed.

    • Under-fetching: Making multiple calls to get related data (N+1 problem).

    • Versioning hell: REST APIs evolve by versioning (/v1, /v2) — GraphQL handles this more gracefully.

    • Client-specific needs: REST responses are fixed; GraphQL allows clients to tailor responses.

🔹 When to consider GraphQL?

  • Multiple clients (web, mobile, IoT) consuming the same API.

  • Frontend teams need faster iteration cycles.

  • Need to reduce network bandwidth (ideal for mobile).

  • Backend is microservice-heavy or integrates many data sources.

  • Want to decouple frontend and backend development cycles.


2. GraphQL vs REST: Key Differences

FeatureRESTGraphQL
Data FetchingMultiple endpoints, fixed structureSingle endpoint, dynamic queries
Over/Under FetchingCommonAvoided
VersioningOften versioned (v1, v2)Schema evolves, no need for versioning
Response FormatFixed, server-definedClient-defined
Aggregating DataMultiple requestsOne request with nested queries
Tooling (introspection)Manual docs (Swagger etc.)Auto-generated schema & docs (GraphiQL, Playground)

 

3. Core GraphQL Operations

GraphQL has only 3 operation types:

🔸 Query

  • For reading/fetching data.

query {
 user(id: "123") {
   name
   email
 }
}
 

🔸 Mutation

  • For creating/updating/deleting data.

mutation {
 createUser(input: { name: "Nitesh", email: "nitesh@example.com" }) {
   id
   name
 }
}
 

🔸 Subscription

  • For real-time updates over WebSocket.

subscription {
 messageAdded {
   id
   content
   sender
 }
}
 

4. GraphQL Schema & Resolvers

🔸 Schema

  • Defines types, queries, mutations, and subscriptions.

  • Think of it as the contract between frontend and backend.

type User {
 id: ID!
 name: String!
 email: String!
}

type Query {
 getUser(id: ID!): User
}
 

🔸 Resolvers

  • The actual functions that run when a field is requested.

  • Tightly linked to schema types.

  • In Java (using graphql-java or Spring GraphQL), resolvers are methods mapped to schema fields.

 

public class UserResolver {
 public User getUser(String id) {
   return userService.findById(id);
 }
}
 

5. Advantages of GraphQL

BenefitExplanation
✅ Precise DataNo more over-fetching; client asks for what it needs.
✅ Fewer RequestsNo need to call multiple endpoints.
✅ Strong TypingSchema defines exact structure.
✅ Rapid Frontend DevFrontend teams move independently.
✅ IntrospectionAuto-generates docs and tools like GraphiQL.
✅ Aggregation LayerCan stitch multiple services or DBs into one API.

 

6. Practical Use Cases

  • Mobile-first apps: Network performance and dynamic data needs.

  • E-commerce platforms: Complex product relationships and variants.

  • Microservice gateways: Aggregating data from multiple backend systems.

  • Analytics dashboards: Tailored data from various domains.

  • CMS & content delivery: Dynamic data structure, multiple clients.

     

Key Features of GraphQL:

  1. Flexible Queries: Clients can request exactly the data they need and nothing more, reducing over-fetching and under-fetching of data.
  2. Single Endpoint: Unlike REST, which requires multiple endpoints for different resources, GraphQL uses a single endpoint to handle all queries.
  3. Strongly Typed: The schema defines the structure of the data and the operations allowed, providing a clear contract between the client and the server.
  4. Real-time Data with Subscriptions: GraphQL supports real-time updates via subscriptions, which allows the client to receive data updates without needing to poll the server.

Basic GraphQL Concepts:

  • Query: A request to read data.
  • Mutation: A request to modify data (e.g., create, update, delete).
  • Subscription: A real-time connection for updates.
  • Schema: Defines types, queries, mutations, and subscriptions, providing a contract for the API.

Example of GraphQL Query:

query {
 user(id: 1) {
   firstName
   lastName
 }
}

 

This query retrieves the firstName and lastName of a user with id = 1.

Example of GraphQL Mutation:

mutation {
 addUser(input: {firstName: "John", lastName: "Doe"}) {
   id
   firstName
   lastName
 }
}
 

 

To use GraphQL in place of Postman and implement it with a REST API (Spring Boot), you can follow these steps. We'll go through setting up GraphQL in a Spring Boot application, configuring it, and using GraphiQL (a web-based GraphQL IDE) or Postman for testing.

Steps to Implement GraphQL with Spring Boot

1. Set Up a Spring Boot Application

Make sure you have a Spring Boot project set up with the necessary dependencies. You can create a Spring Boot application using Spring Initializr and include the following dependencies:

  • Spring Web
  • Spring Boot DevTools (optional)
  • Spring Data JPA (optional)
  • GraphQL Spring Boot Starter

2. Add Required Dependencies

In your pom.xml file, add the following dependencies to include GraphQL support:

 

<dependencies>
   <dependency>
       <groupId>com.graphql-java</groupId>
       <artifactId>graphql-java</artifactId>
       <version>19.0</version> <!-- Ensure to use the latest version -->
   </dependency>
   <dependency>
       <groupId>com.graphql-java</groupId>
       <artifactId>graphql-spring-boot-starter</artifactId>
       <version>11.1.0</version> <!-- Latest version -->
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>
 

 

3. Create GraphQL Schema

Create a file schema.graphqls in the src/main/resources folder. This schema defines the structure of your data and the queries/mutations that clients can make.

For example, let's say we have a simple User model with an id, firstName, and lastName:

schema.graphqls:

 

type Query {
 getUser(id: Int!): User
}

type Mutation {
 addUser(firstName: String!, lastName: String!): User
}

type User {
 id: Int
 firstName: String
 lastName: String
}
 

This defines:

  • A Query to get a User by id.
  • A Mutation to add a new User.
  • A User type with id, firstName, and lastName.

4. Implement GraphQL Resolver

Now, you need to create a resolver class that implements the logic for the Query and Mutation defined in the schema.

User.java (Model class):

 

public class User {
   private int id;
   private String firstName;
   private String lastName;

   // Constructor, getters, setters, etc.
}
 

UserService.java (Service class):

import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
   private Map<Integer, User> users = new HashMap<>();
   private int userIdCounter = 1;

   public User getUserById(int id) {
       return users.get(id);
   }

   public User addUser(String firstName, String lastName) {
       User user = new User(userIdCounter++, firstName, lastName);
       users.put(user.getId(), user);
       return user;
   }
}
 

UserResolver.java (GraphQL Resolver class):

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.GraphQlQueryResolver;
import org.springframework.graphql.data.GraphQlMutationResolver;
import org.springframework.stereotype.Component;

@Component
public class UserResolver implements GraphQlQueryResolver, GraphQlMutationResolver {

   @Autowired
   private UserService userService;

   // Query resolver
   public User getUser(int id) {
       return userService.getUserById(id);
   }

   // Mutation resolver
   public User addUser(String firstName, String lastName) {
       return userService.addUser(firstName, lastName);
   }
}
 

5. Configure GraphQL in Application

Spring Boot with GraphQL should automatically configure the necessary components. If needed, configure GraphQL-related properties in application.properties:

 

# application.properties

graphql.schema-location=classpath:schema.graphqls
graphql.servlet.mapping=/graphql
graphql.servlet.enabled=true
 

6. Test with Postman

Postman can be used to interact with GraphQL APIs. Here’s how you can set it up:

  1. Open Postman and create a new POST request.
  2. Set the URL to your Spring Boot application's GraphQL endpoint. If running locally, it would be:

http://localhost:8080/graphql
 

  • In the Body tab of the request, select raw and set the type to JSON.
  • Now, write your GraphQL query or mutation in the body:

    GraphQL Query Example:

  • {
       "query": "{ getUser(id: 1) { id, firstName, lastName } }"
    }
     

    GraphQL Mutation Example:

  • {
       "query": "mutation { addUser(firstName: \"John\", lastName: \"Doe\") { id, firstName, lastName } }"
    }

 

Send the request, and Postman will return the response from the server. For the query, you will get the User data. For the mutation, it will return the newly created user.

7. Test with GraphiQL (Optional)

For an easier way to interact with GraphQL, you can use GraphiQL, which is a web-based interface.

To enable GraphiQL in your Spring Boot application, add the following dependency:

<dependency>
   <groupId>com.graphql-java</groupId>
   <artifactId>graphiql-spring-boot-starter</artifactId>
   <version>1.0.0</version>
</dependency>
 

After starting the application, you can visit:

http://localhost:8080/graphiql
 

Here, you can directly write GraphQL queries and mutations, and see the results instantly.

 

GraphQL is a query language for APIs that offers several advantages over traditional REST APIs. Here’s why you might choose to use GraphQL instead of a traditional REST API:

1. Flexible and Efficient Data Retrieval

With REST APIs, clients typically make multiple requests to different endpoints to fetch related data (e.g., getting a user, their posts, and comments). This can result in over-fetching or under-fetching of data.

In GraphQL, clients can request exactly the data they need in a single request. This makes GraphQL more efficient because:

  • Over-fetching: Clients only get the data they request.
  • Under-fetching: Clients can specify exactly what data they need, so there’s no need for multiple requests to different endpoints.

Example: For a user with their posts and comments, a typical REST API may require multiple calls:

  • GET /users/{id}
  • GET /users/{id}/posts
  • GET /users/{id}/comments

With GraphQL, the client can request everything in one query:

 

{
 user(id: 1) {
   id
   name
   posts {
     title
     content
   }
   comments {
     text
   }
 }
}
 

2. Single Endpoint

With REST, you have to manage multiple endpoints for different resources. As the application grows, the number of endpoints increases, making it harder to maintain.

GraphQL has a single endpoint (/graphql) that can handle all queries and mutations, making it much easier to maintain and scale.

3. Declarative and Flexible Queries

GraphQL allows clients to specify exactly what data they want. The backend only returns the data required, which is especially beneficial for mobile devices or environments with limited bandwidth.

This is a huge benefit for frontend developers as they can control the data structure without depending on backend developers to create new endpoints or change existing ones.

4. Strongly Typed Schema

GraphQL uses a strongly typed schema to define the shape of the data. The schema serves as a contract between the client and the server, ensuring that queries and mutations are validated and consistent.

This makes it easier to catch errors early, as both the client and server can validate the queries and mutations based on the schema. In REST APIs, this validation is often done through documentation, and there is no built-in mechanism for ensuring consistency.

5. Versionless API

With REST, you may need to create new versions of your API (/v1, /v2) as you change or add new features, leading to potential issues with backward compatibility.

With GraphQL, you don’t need to version your API. As long as the schema evolves in a backward-compatible way (e.g., adding new fields), the existing clients can continue to work without any changes.

6. Real-Time Capabilities with Subscriptions

While REST is typically request-response based, GraphQL supports subscriptions. This allows you to set up real-time features, such as live updates or notifications, without the need for additional technologies like WebSockets or long polling.

7. Tooling and Ecosystem

The GraphQL ecosystem provides great tools for both developers and testers:

  • GraphiQL: An in-browser IDE for interacting with GraphQL APIs.
  • Apollo Client: A popular JavaScript client for making GraphQL queries and managing application data.
  • Postman: While it's traditionally used for REST APIs, Postman now supports GraphQL, allowing for easy testing and integration.

8. Evolving API with Minimal Changes

With REST, changes to the backend may often require changes to the API endpoints, which can break client code. However, in GraphQL, as long as you follow backward-compatible changes, the client can continue to work seamlessly.

For example, you can add new fields to your schema without affecting the existing clients, making it easier to evolve the API over time.

Why Use GraphQL Instead of REST APIs?

  • Better performance: No more over-fetching or under-fetching of data.
  • Single endpoint: Makes it easier to maintain and scale the API.
  • Flexibility: Clients can decide what data they need without waiting for backend changes.
  • Versionless API: Avoids the complexity of managing multiple versions of an API.
  • Real-time capabilities: GraphQL subscriptions make real-time communication easier.

When to Use REST APIs Instead of GraphQL?

  • Simple APIs: If your API is simple and doesn’t need complex queries or relationships, REST APIs are still a good option.
  • Caching: REST is easier to cache since responses are based on URLs and can be stored in traditional HTTP caches.
  • Compatibility: If you have an existing REST API, switching to GraphQL can be a big change, so you might continue using REST until you need the additional features of GraphQL.

Conclusion

You would use GraphQL instead of Postman for testing and interacting with your API because:

  • GraphQL allows you to request specific data in a flexible way, reducing unnecessary API calls.
  • It simplifies client-server communication by using a single endpoint for all requests.
  • It offers tools like GraphiQL and Postman integration for easy testing.

While Postman can be used to test GraphQL APIs, GraphQL itself enhances the way APIs are built and consumed, providing a more efficient and dynamic method for data fetching compared to traditional REST APIs.
 

12 min read
nov 19, 2024
By Nitesh Synergy
Compartilhar