Express.js with GraphQL + Database: Building Something Real

Author

Kritim Yantra

Aug 12, 2025

Express.js with GraphQL + Database: Building Something Real

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.


Why Add 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:

  • Your data sticks around 🗄
  • You can query it in real-time
  • You can scale your API beyond static arrays

For this tutorial, we’ll use MongoDB (because it’s super beginner-friendly and works well with Node.js).


Step 1: Project Setup

If you don’t already have MongoDB installed locally, you can:

  • Use MongoDB Atlas for a free cloud database
  • Or install it locally if you prefer offline development

In your project folder:

npm install express express-graphql graphql mongoose

Step 2: Connecting to MongoDB

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'));

Step 3: Defining a Mongoose Model

Think of a model as the blueprint for your data.

const User = mongoose.model('User', new mongoose.Schema({
  name: String,
  email: String
}));

Step 4: GraphQL Schema + Resolvers

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();
  }
};

Step 5: Hooking Up GraphQL to Express

const app = express();

app.use('/graphql', graphqlHTTP({
  schema,
  rootValue: root,
  graphiql: true
}));

app.listen(4000, () => console.log('🚀 Server running at http://localhost:4000/graphql'));

Step 6: Testing the API

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.


Pro Tip 💡

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.


Common Beginner Questions (FAQ)

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).


Wrapping Up

You’ve now leveled up from “Hello World” to a database-powered GraphQL API with Express.js.
This is the foundation for building:

  • User management systems
  • Blog APIs
  • E-commerce backends

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.

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts