It has been a long time since I wrote my last article. In fact, I was working on many new topics. I will write more articles on these topics that I have explored recently. However, today we will discuss GraphQL.

Why I Decided to Learn GraphQL

I was working on REST APIs for 7 years. It's been a long journey. REST concepts were in my blood. Everything was going great for the first 2–3 years. But after that, when I put my hands into big projects, I started facing some issues. I will tell you all the issues with examples.

You can read the full story for free by clicking here

Example 1 — Overfetching in large APIs

In one of the projects, I had a REST endpoint like:

/api/users/:id

It returns everything related to the user — user profiles, roles, address, preferences, posts, etc. For some modules, I only needed the user's basic info, but the endpoint returned the entire user data.

It worked very well initially, but after scaling, it caused slow responses and heavy payloads.

Example 2 — Underfetching across multiple modules

In another part of the project, I needed data from multiple modules — user, activity, and analytics. REST architecture forced me to create multiple endpoints for each module and then merge everything manually on the frontend.

As the data grew, chaining of requests became more complex and difficult to maintain.

Example 3 — Versioning problem

As the project grew, API versioning became a headache.

For every change — adding a feature, changing a structure- I needed to create a new version.

/api/v1/users
/api/v2/users

After facing these issues and many more, I began to find solutions. Then, I bumped into GraphQL. I started exploring how GraphQL can solve these problems.

What Confused Me at First

I was still confused about how GraphQL would solve these problems. What is the need for GraphQL? Why can't REST alone heal all my pains?

In the case of underfetching, what if I could make one single API that can provide results from all these modules, i.e., user details, activities, and analytics? But if I were to do this, I would be stuck in the case of overfetching. It means that I would always face one of these issues, either overfetching or underfetching.

And honestly, I had already fallen into that trap.

That's when I understood, it's not about whether REST can do it or not, it's about how cleanly and efficiently I can handle growing data needs.

What can GraphQL do?

You only get what you have requested. No more, no less. No overfetching, no underfetching.

That's the main idea behind GraphQL. It fixed my problems in just a snap.

As for the versioning issue, GraphQL is completely different from REST. It doesn't create multiple endpoint URLs — there's just a single endpoint.

https://api.example.com/graphql

If you only need the user's name and email, you just ask for that, nothing more.

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

If you also need the user's posts, you can include them in the same query.

{
  user(id: 1) {
    name
    email
    posts {
      title
      published
    }
  }
}

That's when I realized the power of GraphQL. You can control what data you fetch.

My First Simple Query (with Code Snippet)

This was my first simple GraphQL query. You can also run it on your computer. Create an index.js file and copy the code below.

import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'

const typeDefs = `#graphql
    type User {
        id: ID!
        name: String!
        email: String!
    }
    type Query {
        users: [User]
    }
`

const resolvers = {
    Query: {
        users() {
            return [
                { id: '1', name: 'John Doe', email: 'john.doe@example.com' },
                { id: '2', name: 'Jane Smith', email: 'jane.smith@example.com' },
                { id: '3', name: 'Michael Johnson', email: 'michael.johnson@example.com' },
                { id: '4', name: 'Emily Davis', email: 'emily.davis@example.com' },
                { id: '5', name: 'David Wilson', email: 'david.wilson@example.com' },
            ];
        }
    }
}

const server = new ApolloServer({
    typeDefs,
    resolvers
})

const { url } = await startStandaloneServer(server, {
    listen: { port: 4000 },
})

console.log(`Server ready at url: ${url}`);

Now run the following commands to make this work.

npm init --yes
npm install @apollo/server graphql

After installing the required libraries, run the server.

node index.js

Now your server has been started. Open the URL in your browser.

http://localhost:4000/graphql

You will see an interface something like this.

None
GraphQL Playground

Now, in the operation section, write the following query.

{
  users {
    name
  }
}

Hit the play button, and you will see results.

None
GraphQL Playground

Now, play with queries. Try adding email to the query. Try fetching results from a database like MongoDB.

{
  users {
    name
    email
  }
}

If you want a complete tutorial on this topic, do let me know in the comments section.

What I Liked About It vs REST

When I started using GraphQL, I found the following key benefits:

Only one endpoint

In REST, I had to create and manage multiple endpoints. With GraphQL, everything happens through a single endpoint.

No more overfetching or underfetching

I get exactly what I request. No extra fields. No missing data. It feels so clean and efficient.

Easy to evolve APIs

I don't have to worry about versions now. I can just add or remove fields in the schema without breaking existing queries.

Self-documenting nature

I can explore all available queries, mutations, and types in the Playground itself.

Developer Experience

Testing queries directly in the Playground is smoother than switching between multiple tools like Postman.

Conclusion

As you have read my journey from REST to GraphQL, I hope this article has helped you to clarify the key differences and benefits of using GraphQL. If you are currently struggling with complex APIs or data-fetching issues, I encourage you to give GraphQL a try. Feel free to experiment, explore more advanced features, and let me know about your experiences or questions in the comments below.

Keep making beautiful innovations. Enjoy the journey!

I appreciate you sticking with me through to the end. I hope you liked this one! Remember to follow me 😇 and give me a hearty clap 👏. You can subscribe me to get an email whenever I publish article. Please feel free to ask any questions in the comments section below.

Feel free to get in touch with me. X (Formerly Twitter): https://x.com/mhtkumar46 Email: mhtkumar46@gmail.com Github: https://github.com/mohitsingla46 LinkedIn: https://www.linkedin.com/in/mohit-singla-a16b5851/

I'm currently available for hire! Let's collaborate and build something amazing together. If this article was helpful, consider buying me a coffee to show your support. Thank you!