GraphQL is a powerful query language for APIs that gives clients the exact data they need. When combined with Node.js, it enables fast, flexible, and strongly typed APIs.
This guide walks you through:
Learn software engineering online
GraphQL is a query language created by Facebook that allows clients to request exactly the data they need.
GraphQL in Node.js has three main parts:
Defines what data can be queried, mutated, or subscribed to.
Functions that fetch or manipulate the data.
Apollo Server or Express GraphQL handles the API endpoint.
mkdir graphql-node
cd graphql-node
npm init -y We’ll use Apollo Server (most popular GraphQL server for Node.js).
npm install @apollo/server graphql express body-parser cors
npm install @apollo/server/express4 Create a file: schema.js
import { gql } from "graphql-tag";
export const typeDefs = gql`
type Book {
id: ID!
title: String!
author: String!
}
type Query {
books: [Book!]
book(id: ID!): Book
}
type Mutation {
addBook(title: String!, author: String!): Book!
}
`;
Create a file: resolvers.js
export const resolvers = {
Query: {
books: () => books,
book: (_, args) => books.find((b) => b.id === args.id),
},
Mutation: {
addBook: (_, args) => {
const newBook = {
id: String(books.length + 1),
title: args.title,
author: args.author,
};
books.push(newBook);
return newBook;
},
},
};
// Temporary in-memory database
const books = [
{ id: "1", title: "Atomic Habits", author: "James Clear" },
{ id: "2", title: "Deep Work", author: "Cal Newport" },
];
Create server.js:
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import { typeDefs } from "./schema.js";
import { resolvers } from "./resolvers.js";
const app = express();
// Create Apollo Server instance
const server = new ApolloServer({
typeDefs,
resolvers,
});
async function startServer() {
await server.start();
app.use(
"/graphql",
cors(),
bodyParser.json(),
expressMiddleware(server)
);
app.listen(4000, () => {
console.log("🚀 GraphQL Server running at http://localhost:4000/graphql");
});
}
startServer();
node server.js Visit:
📌 http://localhost:4000/graphql
You will see Apollo Sandbox where you can run queries.
query {
books {
id
title
author
}
} query {
book(id: "1") {
title
}
}
mutation {
addBook(title: "Clean Code", author: "Robert C. Martin") {
id
title
}
}
graphql-node/
│── package.json
│── server.js
│── schema.js
│── resolvers.js
GraphQL works great with:
Example with Mongoose:
Query: {
books: async () => await BookModel.find(),
},
Mutation: {
addBook: async (_, args) => await BookModel.create(args),
}
Connecting Node.js with GraphQL involves:
/graphql endpointLatest tech news and coding tips.
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…
1. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…