What is GraphQL

GraphQL is a way to talk to a database or server and get only the data you need. It’s like ordering food in a restaurant:

Vivek Rastogi

8/1/20253 min read

GraphQL is a way to talk to a database or server and get only the data you need. It’s like ordering food in a restaurant:

🧑‍💻 You (the client) say: “I want 1 burger, 1 coke.”

🍽️ Instead of getting the whole menu (like in REST), the waiter (server) gives you just what you ordered.

Why Should You Learn GraphQL?

If you are a:

  • ✅ Frontend Developer (React, Vue, etc.)

  • ✅ Backend Developer building APIs

  • ✅ Full Stack Developer

REST vs GraphQL — Easy Example:

Imagine you want this info from a social media site:

  • User's name

  • Their posts with only titles

REST (Traditional Way)

You might need to hit:

  • /users/1

  • /users/1/posts

Then filter unnecessary fields like email, timestamps, etc.

✅ Just one request
✅ Only the fields you want

🔥 Main Advantages of GraphQL
  • 🧠 Smart: Only gets what you need

  • 🚀 Fast: Fewer network calls

  • 🔄 Easy to update frontend without backend changes

  • ✅ Strongly typed: You know exactly what’s returned

  • 🛠 One single endpoint (/graphql) for all operations

🎯 Real Life Use Cases
  • Facebook (they built it!)

  • GitHub GraphQL API

  • Shopify Storefront API

  • Instagram, Twitter-like apps

  • Dashboard apps, admin panels, etc.

🧱 Basic Building Blocks of GraphQL:

1. Query – Get data

Step 1: Create a data file

📄 data.js

Step 2: Setup GraphQL API

📄 index.js

const { createSchema, createYoga } = require('graphql-yoga');

const { createServer } = require('http');

const { products } = require('./data');

let nextId = 3;

const typeDefs = /* GraphQL */ `

type Product {

id: ID!

name: String!

price: Float!

}

type Query {

products: [Product!]!

}

type Mutation {

addProduct(name: String!, price: Float!): Product!

updateProduct(id: ID!, name: String, price: Float): Product

deleteProduct(id: ID!): Product

}

`;

const resolvers = {

Query: {

products: () => products

},

Mutation: {

addProduct: (_, { name, price }) => {

const newProduct = { id: String(nextId++), name, price };

products.push(newProduct);

return newProduct;

},

updateProduct: (_, { id, name, price }) => {

const product = products.find(p => p.id === id);

if (!product) return null;

if (name) product.name = name;

if (price !== undefined) product.price = price;

return product;

},

deleteProduct: (_, { id }) => {

const index = products.findIndex(p => p.id === id);

if (index === -1) return null;

const removed = products.splice(index, 1)[0];

return removed;

}

}

};

const yoga = createYoga({

schema: createSchema({ typeDefs, resolvers }),

});

const server = createServer(yoga);

server.listen(4000, () => {

console.log("GraphQL server running on http://localhost:4000/graphql");

});

Summary

You just built a mini GraphQL API to:

  • 🔁 Query all products

  • ➕ Add a new product

  • ✏️ Update a product

  • ❌ Delete a product