Next js - Node Js - React JS

What is GraphQL? How is it Different than Traditional HTTP REST API ?

Interview Answer

GraphQL is a query language for APIs that allows clients to request exactly the data they need through a single endpoint. Unlike traditional REST APIs, which often require multiple endpoints and can lead to over-fetching or under-fetching of data, GraphQL lets the client define the response structure. It uses Queries for fetching data, Mutations for modifying data, and Resolvers on the server side to return the requested information. This makes GraphQL efficient and flexible for modern applications.

What is GraphQL?

GraphQL is a query language and API technology developed by Meta (Facebook) that allows clients to request exactly the data they need from a server.

Unlike REST APIs, GraphQL typically exposes a single endpoint and lets the client specify which fields to fetch.


Traditional HTTP REST API

Endpoint

GET /users/1

Response:

{
  "id": 1,
  "name": "Raj",
  "email": "raj@gmail.com",
  "phone": "9876543210",
  "address": "Bangalore",
  "salary": 50000,
  "department": "IT"
}

Suppose you only need:

name
email

Still, the server sends all fields.

This is called Over Fetching.


GraphQL Example

Query

query {
  user(id: 1) {
    name
    email
  }
}

Response:

{
  "data": {
    "user": {
      "name": "Raj",
      "email": "raj@gmail.com"
    }
  }
}

Only requested data is returned.


REST vs GraphQL

FeatureREST APIGraphQL
EndpointsMultipleSingle
Data FetchingFixed responseCustom response
Over FetchingPossibleNo
Under FetchingPossibleNo
Request TypeGET, POST, PUT, DELETEMostly POST
Versioningv1, v2, v3Usually not required
Learning CurveEasyModerate
PerformanceMultiple API callsSingle query possible

Example: User + Jobs

REST API

Need user details and jobs.

GET /users/1
GET /users/1/jobs

Two API calls.


GraphQL

query {
  user(id: 1) {
    name
    email

    jobs {
      id
      title
      salary
    }
  }
}

Response:

{
  "data": {
    "user": {
      "name": "Raj",
      "email": "raj@gmail.com",
      "jobs": [
        {
          "id": 1,
          "title": "React Developer",
          "salary": 1200000
        }
      ]
    }
  }
}

Single API call.


GraphQL Components

Schema

Defines data structure.

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

Query

Used to fetch data.

query {
  users {
    id
    name
  }
}

Equivalent to:

GET /users

Mutation

Used to insert, update, delete.

mutation {
  addUser(
    name: "Raj"
    email: "raj@gmail.com"
  ) {
    id
    name
  }
}

Equivalent to:

POST /users

Resolver

Contains business logic.

const resolvers = {
  Query: {
    users: async () => {
      return await User.find();
    }
  }
};

Example Using Node.js + GraphQL

Schema

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

type Query {
  users: [User]
}

Resolver

const resolvers = {
  Query: {
    users: async () => {
      return await User.find();
    }
  }
};

Client Query

fetch("http://localhost:5000/graphql", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    query: `
      query {
        users {
          id
          name
          email
        }
      }
    `
  })
});

When to Use GraphQL?

✅ Complex applications

✅ Mobile apps (less data transfer)

✅ Multiple related entities

✅ Dashboards

✅ Job Portals

✅ Social Media Apps

For your Next.js Job Portal, GraphQL is useful because you can fetch:

query {
  jobdetails(url: "react-developer") {
    title
    city
    salary

    company {
      company_name
    }
  }
}

without creating multiple REST endpoints.


Interview Answer

GraphQL is a query language for APIs that allows clients to request exactly the data they need through a single endpoint. Unlike traditional REST APIs, which often require multiple endpoints and can lead to over-fetching or under-fetching of data, GraphQL lets the client define the response structure. It uses Queries for fetching data, Mutations for modifying data, and Resolvers on the server side to return the requested information. This makes GraphQL efficient and flexible for modern applications.