Getting Started with GraphQL in Node.js

Table of contents
Modern APIs are evolving, and GraphQL is at the center of this shift. If you're building applications with Node.js and looking for a more flexible, powerful way to work with APIs, GraphQL is worth learning. In this beginner-friendly tutorial, we’ll explore what GraphQL is, how it compares to REST, and how to build your very first GraphQL server using Node.js.
What is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries with your existing data. It was developed by Facebook and open-sourced in 2015. Unlike REST, which exposes multiple endpoints for different resources, GraphQL provides a single endpoint and allows clients to request exactly the data they need — no more, no less.
Here’s what makes GraphQL stand out:
Single endpoint: All data is fetched via a unified endpoint.
Precise querying: Clients define the structure of the response.
Strongly typed schema: Everything is defined by a schema, offering great tooling and validation.
No over-fetching or under-fetching: Just request the fields you need.
How is GraphQL Different from REST?
While REST APIs rely on multiple endpoints (e.g. /users
, /posts
), GraphQL uses a single endpoint and lets clients specify the shape of the data. Here’s a quick comparison:
REST | GraphQL |
Multiple endpoints | Single endpoint |
Fixed data structure | Customizable queries |
Versioning required | No versioning (evolves via schema) |
Over/under-fetching common | Client controls what to fetch |
Setting Up a GraphQL Server in Node.js
Let’s create a simple GraphQL API that returns a list of books. We’ll use:
Node.js
express
- lightweight web frameworkgraphql
- core GraphQL libraryexpress-graphql
- connects GraphQL with Express
Step 1: Initialize a New Project
mkdir graphql-node-tutorial
cd graphql-node-tutorial
npm init -y
npm install express express-graphql graphql
Step 2: Create the Entry File (index.js
)
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
// Sample data
const books = [
{ title: 'PatanGrid', author: 'Patan Sharukhan' },
{ title: 'GridPatan', author: 'Sharukhan Patan' }
];
// Define schema
const schema = buildSchema(`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`);
// Define resolver
const root = {
books: () => books
};
// Create server
const app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true, // Enables the browser tool
}));
app.listen(4000, () => {
console.log('Server running at http://localhost:4000/graphql');
});
Step 3: Run the Server
node index.js
Open your browser and go to http://localhost:4000/graphql. You’ll see GraphiQL, an in-browser IDE for running queries.
Step 4: Try Your First Query
{
books {
title
author
}
}
The response should look like this:
{
"data": {
"books": [
{ "title": "PatanGrid", "author": "Patan Sharukhan" },
{ "title": "GridPatan", "author": "Sharukhan Patan" }
]
}
}
Understanding Schema, Types, Queries, and Resolvers
Here’s what each part of the code does:
Schema: Defines what data is available and how it's structured. In this case, we defined a
Book
type and abooks
query.Query: Represents the entry point for reading data. Clients use it to ask for specific fields.
Resolver: Functions that tell GraphQL how to fetch the data for a type or field.
What’s Next?
Now that you’ve built a basic GraphQL server, you can start exploring more advanced topics like:
Creating and updating data with mutations
Using variables in your queries
Connecting to a real database like MongoDB or PostgreSQL
Adding authentication with JWT
Pagination and filtering
Final Thoughts
GraphQL may seem intimidating at first, but once you get the hang of its core concepts — schema, types, queries, and resolvers — it becomes a powerful tool in your web development toolkit. With just a few lines of code, you've created a flexible and customizable API that's easy to test and scale.
In future tutorials, we’ll build on this foundation by adding real databases, authentication, and even fullstack integrations.
Subscribe to my newsletter
Read articles from Sharukhan Patan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
