Node.js GraphQL

Node.js GraphQL – Introduction

GraphQL is a query language for APIs created by Facebook.
It allows clients to request exactly the data they need — nothing more, nothing less.

Compared to REST:

REST GraphQL
Multiple endpoints Single endpoint /graphql
Returns fixed data Returns only requested fields
Over-fetching / under-fetching Client controls data shape
Multiple round-trips Usually single request

1. Install GraphQL in Node.js

You can build GraphQL APIs using libraries like:

  • express-graphql (simple)

  • Apollo Server (powerful, recommended)

Install Express + GraphQL:

npm install express express-graphql graphql

2. Basic GraphQL Server (express-graphql)

server.js

const express = require("express");
const { graphqlHTTP } = require("express-graphql");
const { buildSchema } = require("graphql");
const app = express();

// GraphQL Schema
const schema = buildSchema(
type Query {
hello: String
}
);

// Resolver
const root = {
hello: () => “Hello World from GraphQL!”
};

// GraphQL Endpoint
app.use(“/graphql”, graphqlHTTP({
schema,
rootValue: root,
graphiql: true // GraphQL Playground
}));

app.listen(4000, () => {
console.log(“Server running on http://localhost:4000/graphql”);
});

Run:

node server.js

Visit http://localhost:4000/graphql and run:

{
hello
}

3. GraphQL Schema Basics

A GraphQL schema contains:

  • Types

  • Queries

  • Mutations

Example Schema:

type User {
id: ID
name: String
age: Int
}

4. GraphQL Query Example

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

Output will contain only requested fields:

{
"data": {
"user": {
"name": "John",
"age": 30
}
}
}

5. GraphQL Mutations (Insert / Update / Delete)

mutation {
addUser(name: "Vipul", age: 25) {
id
name
}
}

6. Complete CRUD Example (Users API)

Schema

const schema = buildSchema(
type User {
id: ID
name: String
age: Int
}
type Query {
users: [User]
user(id: ID!): User
}

type Mutation {
addUser(name: String!, age: Int!): User
updateUser(id: ID!, name: String, age: Int): User
deleteUser(id: ID!): String
}

);

Data (Demo)

let users = [
{ id: 1, name: "John", age: 28 },
{ id: 2, name: "Peter", age: 32 }
];

Resolvers

const root = {
users: () => users,
user: ({ id }) => users.find(u => u.id == id),
addUser: ({ name, age }) => {
const user = { id: users.length + 1, name, age };
users.push(user);
return user;
},

updateUser: ({ id, name, age }) => {
const user = users.find(u => u.id == id);
if (!user) return null;

if (name) user.name = name;
if (age) user.age = age;
return user;
},

deleteUser: ({ id }) => {
users = users.filter(u => u.id != id);
return “User deleted”;
}
};


7. GraphQL with MongoDB (Mongoose)

  1. Install:

npm install mongoose apollo-server graphql
  1. Build MongoDB models

  2. Use Apollo Server (recommended for advanced GraphQL)

If you want, I can provide full working MongoDB + GraphQL CRUD project.


8. GraphiQL – Built-in Playground

With graphiql: true, you get:

  • Auto-suggestions

  • Query editor

  • Documentation explorer

You may also like...