# apollographql/apollo-ios architecture

> Architecture diagram of the GitHub repository apollographql/apollo-ios, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apollographql/apollo-ios
- Repository: https://github.com/apollographql/apollo-ios

## Overview

##### Main Components

a. Core Components:
- Apollo Client (Central Component)
- Network Layer
- Caching System
- Code Generation System
- WebSocket Support
- SQLite Integration

b. Supporting Components:
- Interceptor Chain
- Schema Configuration
- Type System
- Test Support

##### Component Relationships and Architecture

Layer 1 - Client Interface:
**ApolloClient / ApolloClientProtocol**

Layer 2 - Operation Processing:
- GraphQLOperation Handling
- Query Watching
- Mutation Processing
- Subscription Management

Layer 3 - Network & Caching:
**Left Side:                    Right Side:**
**Network Transport            Caching System**
- HTTP                       - In-Memory Cache
- WebSocket                  - SQLite Cache
- Split Transport           - Normalized Cache

Layer 4 - Interceptor Chain:
- Network Fetch
- Response Parsing
- Cache Reading/Writing
- Error Handling
- Automatic Persisted Queries

##### Specific Implementation Details

a. Data Flow:
**Client Request → Interceptor Chain → Network/Cache → Response Processing → Typed Results**

b. Key Features:
- Type-safe operation execution
- Normalized caching
- Real-time subscriptions
- Offline support
- Automatic code generation

##### Integration Points

Connections to:
- GraphQL Server
- iOS Application
- SQLite Storage
- WebSocket Server

##### Design Patterns to Highlight

- Chain of Responsibility (Interceptors)
- Protocol-oriented design
- Type-safe architecture
- Cache normalization

- Component types
- Relationship types
- Data flow directions
- Integration points

- Apollo (Core functionality)
- ApolloAPI (API definitions)
- ApolloSQLite (SQLite integration)
- ApolloWebSocket (WebSocket support)
- ApolloTestSupport (Testing utilities)

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client["Client Interface"]
        AC["Apollo Client"]:::core
        ACP["Apollo Client Protocol"]:::core
    end

    subgraph Operations["Operation Processing"]
        GO["GraphQL Operation"]:::core
        QW["Query Watcher"]:::core
        MP["Mutation Processing"]:::core
        SM["Subscription Management"]:::core
    end

    subgraph Network["Network & Caching Layer"]
        direction TB
        subgraph NT["Network Transport"]
            direction TB
            HTTP["HTTP Transport"]:::network
            WS["WebSocket Transport"]:::network
            ST["Split Transport"]:::network
        end

        subgraph Cache["Caching System"]
            direction TB
            IMC["In-Memory Cache"]:::cache
            SQLC["SQLite Cache"]:::cache
            NC["Normalized Cache"]:::cache
        end
    end

    subgraph IC["Interceptor Chain"]
        direction TB
        NF["Network Fetch"]:::interceptor
        RP["Response Parsing"]:::interceptor
        CRW["Cache Read/Write"]:::interceptor
        EH["Error Handling"]:::interceptor
        APQ["Auto Persisted Queries"]:::interceptor
    end

    subgraph Support["Supporting Components"]
        direction TB
        SC["Schema Configuration"]:::support
        TS["Type System"]:::support
        TEST["Test Support"]:::support
    end

    %% Relationships
    Client --> Operations
    Operations --> Network
    Operations --> IC
    IC --> NT
    IC --> Cache
    NT --> |External| GQL[("GraphQL Server")]:::external
    WS --> |WebSocket| WSS[("WebSocket Server")]:::external
    SQLC --> |Storage| SQL[("SQLite DB")]:::external

    %% Component Mappings
    click AC "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/ApolloClient.swift"
    click ACP "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/ApolloClientProtocol.swift"
    click HTTP "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/NetworkTransport.swift"
    click NF "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/NetworkFetchInterceptor.swift"
    click IMC "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/InMemoryNormalizedCache.swift"
    click NC "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/NormalizedCache.swift"
    click WS "https://github.com/apollographql/apollo-ios/blob/main/Sources/ApolloWebSocket/WebSocketTransport.swift"
    click ST "https://github.com/apollographql/apollo-ios/blob/main/Sources/ApolloWebSocket/SplitNetworkTransport.swift"
    click SQLC "https://github.com/apollographql/apollo-ios/blob/main/Sources/ApolloSQLite/SQLiteNormalizedCache.swift"
    click IC "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/InterceptorRequestChain.swift"
    click SC "https://github.com/apollographql/apollo-ios/blob/main/Sources/ApolloAPI/SchemaConfiguration.swift"
    click TS "https://github.com/apollographql/apollo-ios/tree/main/Sources/ApolloAPI/SchemaTypes"
    click TEST "https://github.com/apollographql/apollo-ios/tree/main/Sources/ApolloTestSupport"
    click GO "https://github.com/apollographql/apollo-ios/blob/main/Sources/ApolloAPI/GraphQLOperation.swift"
    click QW "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/GraphQLQueryWatcher.swift"
    click RP "https://github.com/apollographql/apollo-ios/blob/main/Sources/Apollo/GraphQLResponse.swift"

    %% Styling
    classDef core fill:#2374ab
    classDef network fill:#57a773
    classDef cache fill:#ff8c42
    classDef interceptor fill:#984447
    classDef support fill:#61707d
    classDef external fill:#f2f2f2

    %% Legend
    subgraph Legend
        direction TB
        L1["Core Components"]:::core
        L2["Network Components"]:::network
        L3["Cache Components"]:::cache
        L4["Interceptors"]:::interceptor
        L5["Support Components"]:::support
        L6["External Systems"]:::external
    end
```
