Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Learning Notes

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.

GraphQL Core Concepts

Schema Definition

  • 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 System Example

type Game {
    id: ID!
    title: String!
    platform: [String!]!  # Array of non-null strings
    reviews: [Review!]    # Optional array of reviews
}

Query Types

  • Every GraphQL server must have a Query type
  • 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
            }
        }
    }
}

Mutation Types

  • 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
    }
}

Resolvers

  • 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);
        }
    }
};

Project Implementation Notes

Setup

npm install @apollo/server graphql

Server Configuration

  • Uses Apollo Server v4
  • ES Modules enabled
  • Runs on port 4000
  • In-memory database for demonstration

Data Relationships

  • One-to-Many: Game to Reviews
  • One-to-Many: Author to Reviews
  • Many-to-One: Review to Game and Author

Best Practices Demonstrated

  1. Type safety with required fields
  2. Nested query support
  3. Input types for mutations
  4. Proper resolver structure
  5. Clear separation of schema and implementation

Common GraphQL Patterns

Query Patterns

  • Fetch specific fields
  • Nested queries
  • Filter by ID
  • Get related data

Mutation Patterns

  • Add new records
  • Update existing records
  • Delete records
  • Return modified data

Error Handling

  • Type validation
  • Required field checking
  • ID existence verification

Resources

About

the stuff

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages