A simple GraphQL server built with Apollo Server and Node.js that demonstrates basic query and mutation operations for managing a book collection.
- GraphQL API with Apollo Server
- Query Operations: Retrieve books, count books, and find specific books
- Mutation Operations: Add new books with validation
- Type Safety with TypeScript
- Error Handling with custom GraphQL errors
- Computed Fields for derived data
- Advanced Learning Examples: Comprehensive tutorials and examples
This project includes comprehensive learning materials perfect for mastering GraphQL:
- TUTORIAL.md - π Complete step-by-step GraphQL tutorial from basics to advanced
- LEARNING_GUIDE.md - π― Detailed learning objectives with structured examples
- TESTING_GUIDE.md - π§ͺ Systematic testing approaches and validation
- examples.md - β‘ Ready-to-copy interactive examples for Apollo Studio
- CHEAT_SHEET.md - π Quick reference for GraphQL syntax and patterns
Perfect for learning GraphQL fundamentals! π
- Start with TUTORIAL.md - Follow the step-by-step guide
- Practice with examples.md - Copy examples into Apollo Studio
- Test systematically with TESTING_GUIDE.md - Validate your understanding
- Reference CHEAT_SHEET.md - Quick syntax lookup
- Deep dive with LEARNING_GUIDE.md - Advanced concepts
- Node.js (v16 or higher)
- npm or yarn
-
Clone the repository:
git clone https://github.com/emisasal/graphql-server-example.git cd graphql-server-example -
Install dependencies:
npm install
-
Compile TypeScript:
npm run compile
-
Start the server:
npm start
The server will be available at http://localhost:4000
type Book {
id: ID!
title: String!
author: Author! # Relationship to Author type
genre: Genre # Enum type
publishedYear: Int
pages: Int
isbn: String
isAvailable: Boolean!
fullData: String! # Computed field: "title by author"
summary: String
}type Author {
id: ID!
name: String!
bio: String
birthYear: Int
books: [Book!]! # One-to-many relationship
}enum Genre {
FICTION
NON_FICTION
MYSTERY
ROMANCE
SCIENCE_FICTION
FANTASY
BIOGRAPHY
HISTORY
}query {
books {
id
title
author {
name
bio
}
genre
isAvailable
}
}query {
findBook(title: "1984") {
id
title
author {
name
birthYear
}
publishedYear
summary
}
}query {
booksByGenre(genre: SCIENCE_FICTION) {
title
author {
name
}
publishedYear
}
}query {
searchBooks(query: "dystopian", genre: SCIENCE_FICTION) {
title
author {
name
}
summary
}
}mutation {
addAuthor(input: {
name: "Isaac Asimov"
bio: "American science fiction writer"
birthYear: 1920
}) {
id
name
bio
}
}mutation {
addBook(input: {
title: "Foundation"
authorId: "6"
genre: SCIENCE_FICTION
publishedYear: 1951
pages: 244
summary: "A galactic empire in decline and the science of psychohistory."
}) {
id
title
author {
name
}
fullData
}
}mutation {
updateBook(id: "1", input: {
pages: 200
summary: "Updated summary of the book"
}) {
id
title
pages
summary
}
}mutation {
resetData
}- Start the server
- Open your browser and navigate to
http://localhost:4000 - Apollo Studio will open automatically
- Try the example queries and mutations above
curl -X POST http://localhost:4000 \
-H "Content-Type: application/json" \
-d '{"query": "{ books { id title author fullData } }"}'curl -X POST http://localhost:4000 \
-H "Content-Type: application/json" \
-d '{"query": "mutation { addBook(title: \"New Book\", author: \"New Author\") { id title author fullData } }"}'graphql-server-example/
βββ src/
β βββ index.ts # Main server file with schema and resolvers
βββ dist/ # Compiled JavaScript files
βββ package.json # Project dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ LICENSE # License file
βββ README.md # This file
npm run compile- Compile TypeScript to JavaScriptnpm start- Compile and start the servernpm run dev- Start TypeScript compiler in watch modenpm run serve- Start the server (requires compiled files)npm run build- Clean and compile the projectnpm run clean- Remove compiled files
The server comes with sample book data:
- "The Awakening" by Kate Chopin
- "City of Glass" by Paul Auster
- "1984" by George Orwell
- "To Kill a Mockingbird" by Harper Lee
- "The Great Gatsby" by F. Scott Fitzgerald
The API includes validation for:
- Duplicate book titles: Returns a GraphQL error if you try to add a book with an existing title
- Required fields: Ensures all required fields are provided
Example error response:
{
"errors": [
{
"message": "Book title must be unique",
"extensions": {
"code": "BAD_REQUEST",
"invalidArgs": "Existing Book Title"
}
}
]
}- Apollo Server - GraphQL server implementation
- GraphQL - Query language for APIs
- TypeScript - Type-safe JavaScript
- Node.js - JavaScript runtime
- Database integration (PostgreSQL, MongoDB)
- User authentication and authorization
- Book categories and genres
- Pagination for large datasets
- Input validation with custom scalars
- Subscriptions for real-time updates
- Unit and integration tests
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.