Perfect — let’s go through GraphQL with Apollo Server in Node.js, which is essential for building flexible, type-safe APIs.
1️⃣ What is GraphQL?
-
GraphQL = query language and runtime for APIs
-
Allows clients to request exactly the data they need
-
Benefits over REST:
-
No over-fetching or under-fetching
-
Single endpoint for all data
-
Strongly typed schema
-
Self-documenting
-
2️⃣ Apollo Server
-
Popular Node.js GraphQL server implementation
-
Works with Express, Koa, Fastify
-
Supports schema definition, resolvers, subscriptions, and middleware
Install
npm install apollo-server graphql
3️⃣ Basic Apollo Server Setup
const { ApolloServer, gql } = require("apollo-server");
// Define schema
const typeDefs = gql`
type User {
id: ID!
name: String!
email: String!
}
type Query {
users: [User]
}
type Mutation {
addUser(name: String!, email: String!): User
}
`;
// Sample data
const users = [];
// Define resolvers
const resolvers = {
Query: {
users: () => users,
},
Mutation: {
addUser: (_, { name, email }) => {
const newUser = { id: users.length + 1, name, email };
users.push(newUser);
return newUser;
},
},
};
// Create Apollo Server
const server = new ApolloServer({ typeDefs, resolvers });
// Start server
server.listen(4000).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
-
Run and navigate to
http://localhost:4000
to use GraphQL Playground -
Allows testing queries and mutations interactively
4️⃣ Example Queries & Mutations
Query
query {
users {
id
name
email
}
}
Mutation
mutation {
addUser(name: "Tyson", email: "tyson@example.com") {
id
name
}
}
5️⃣ Integration with Express
const express = require("express");
const { ApolloServer } = require("apollo-server-express");
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
server.applyMiddleware({ app, path: "/graphql" });
app.listen(3000, () => console.log("Server running at http://localhost:3000/graphql"));
- Allows using GraphQL alongside REST routes
6️⃣ Advanced Features
-
Subscriptions → real-time updates over WebSockets
-
Schema stitching / Federation → combine multiple GraphQL APIs
-
Middleware / Context → inject auth, loaders, or DB connections
-
DataLoader → batch and cache requests to optimize DB access
7️⃣ Key Takeaways
-
GraphQL gives flexible, type-safe queries
-
Apollo Server simplifies server setup and schema management
-
Resolvers separate data fetching logic from schema
-
Integrates easily with Express for hybrid apps
-
Supports mutations, subscriptions, and middleware for advanced use cases
Next, we could cover WebSockets + GraphQL subscriptions for real-time APIs or combining GraphQL with service/repository patterns for a scalable architecture.
Do you want me to go there next?