This project serves as a practical implementation of GraphQL concepts using Apollo Server. It's designed as a gaming review system to demonstrate various GraphQL features and patterns.
- Types are defined using the GraphQL Schema Definition Language (SDL)
!indicates a required field- Types can be:
- Scalar (Int, Float, String, Boolean, ID)
- Object (custom types like Game, Review, Author)
- Input (used for mutations)
- Enum (not used in this project)
type Game {
id: ID!
title: String!
platform: [String!]! # Array of non-null strings
reviews: [Review!] # Optional array of reviews
}- Every GraphQL server must have a
Querytype - Defines entry points to the graph
- Can be nested (e.g., getting reviews for a game)
- Example query structure:
query {
games {
title
platform
reviews {
rating
author {
name
}
}
}
}- Used for modifying data
- Must be explicitly defined in the schema
- Can return the modified data
- Example mutation:
mutation {
addGame(game: {
title: "New Game"
platform: ["PC"]
}) {
id
title
}
}- Functions that handle the logic for each field
- Can be nested to handle relationships
- Parent argument contains data from parent resolver
- Example resolver structure:
const resolvers = {
Query: {
games() {
return db.games;
}
},
Game: {
reviews(parent) {
return db.reviews.filter(r => r.game_id === parent.id);
}
}
};npm install @apollo/server graphql- Uses Apollo Server v4
- ES Modules enabled
- Runs on port 4000
- In-memory database for demonstration
- One-to-Many: Game to Reviews
- One-to-Many: Author to Reviews
- Many-to-One: Review to Game and Author
- Type safety with required fields
- Nested query support
- Input types for mutations
- Proper resolver structure
- Clear separation of schema and implementation
- Fetch specific fields
- Nested queries
- Filter by ID
- Get related data
- Add new records
- Update existing records
- Delete records
- Return modified data
- Type validation
- Required field checking
- ID existence verification