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:
⭐ 2. Basic GraphQL Server (express-graphql)
server.js
Run:
Visit http://localhost:4000/graphql and run:
⭐ 3. GraphQL Schema Basics
A GraphQL schema contains:
-
Types
-
Queries
-
Mutations
Example Schema:
⭐ 4. GraphQL Query Example
Output will contain only requested fields:
⭐ 5. GraphQL Mutations (Insert / Update / Delete)
⭐ 6. Complete CRUD Example (Users API)
Schema
Data (Demo)
Resolvers
⭐ 7. GraphQL with MongoDB (Mongoose)
-
Install:
-
Build MongoDB models
-
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
