Kritim Yantra
Aug 12, 2025
When "Hello World" Isn’t Enough Anymore…
If you followed my last post, you probably made your first GraphQL query and thought:
"Wow, this is awesome!" 🎉
But let’s be honest — a “Hello, GraphQL world!” API isn’t going to impress your friends or power your startup idea.
The real magic happens when you connect GraphQL to an actual database. That’s when your app stops being a demo and starts being useful. So today, we’re taking the next step — building a real-world Express.js + GraphQL API with live data from a database.
Without a database, your app is like a whiteboard — you can write stuff, but it disappears as soon as you leave.
With a database:
For this tutorial, we’ll use MongoDB (because it’s super beginner-friendly and works well with Node.js).
If you don’t already have MongoDB installed locally, you can:
In your project folder:
npm install express express-graphql graphql mongoose
Create a file server.js
:
const express = require('express');
const mongoose = require('mongoose');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/graphql_demo', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.once('open', () => console.log('✅ Connected to MongoDB'));
Think of a model as the blueprint for your data.
const User = mongoose.model('User', new mongoose.Schema({
name: String,
email: String
}));
We’ll let users query all users or add a new one.
const schema = buildSchema(`
type User {
id: ID
name: String
email: String
}
type Query {
users: [User]
}
type Mutation {
addUser(name: String!, email: String!): User
}
`);
const root = {
users: async () => await User.find(),
addUser: async ({ name, email }) => {
const user = new User({ name, email });
return await user.save();
}
};
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true
}));
app.listen(4000, () => console.log('🚀 Server running at http://localhost:4000/graphql'));
Run:
node server.js
Then open:http://localhost:4000/graphql
Try:
mutation {
addUser(name: "Alice", email: "alice@example.com") {
id
name
email
}
}
And then:
{
users {
id
name
email
}
}
🎉 Congratulations! You now have a real Express.js + GraphQL API backed by MongoDB.
If you’re coming from REST APIs, mutations in GraphQL are just fancy ways of saying “this changes the data” — like POST
, PUT
, or DELETE
in REST.
Q1: Do I have to use MongoDB?
A: Nope! You can use any database — MySQL, PostgreSQL, or even Firebase. The GraphQL part stays the same.
Q2: Why do we need both a schema and a Mongoose model?
A: The schema tells GraphQL how clients can request data. The model tells MongoDB how to store it.
Q3: Can I deploy this?
A: Absolutely — host it on services like Render, Railway, or Heroku (though Heroku now has limited free options).
You’ve now leveled up from “Hello World” to a database-powered GraphQL API with Express.js.
This is the foundation for building:
Your turn: What’s the first real project you’d like to build with Express.js + GraphQL? Drop your ideas — I might make a tutorial for it next.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google