# expediagroup/graphql-component architecture

> Architecture diagram of the GitHub repository expediagroup/graphql-component, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/expediagroup/graphql-component
- Repository: https://github.com/expediagroup/graphql-component

## Overview

##### Main Components
- GraphQLComponent Core: Represent the central class that manages schema creation, combining type definitions and resolvers. Illustrate its key responsibilities such as handling:
- Schema composition (merging sub-schemas using graphql-tools stitchSchemas or makeExecutableSchema)
- Apollo Federation integration (via buildSubgraphSchema)
- Importing/sub-component management
- Composition Example: Illustrates a plain Apollo Server environment that builds a composite schema from multiple components (like listing, property, or reviews components).
- Federation Example: Illustrates how your GraphQLComponent is used to generate Apollo Federated sub-schemas that are then aggregated by an Apollo Gateway.
- Supporting Modules in the Library (lib):
- Context Management (lib/context): For decorating and transforming the request context.
- Data Source Integration (lib/datasource): For integrating data sources via context injection with a proxy for stateful operations.
- Resolver and Transformation Layers (lib/resolvers and lib/transforms): For processing GraphQL operations.

- “Inputs”: types, resolvers, imports, directives, dataSources, context options.
- “Operations”: using graphql-tools functions – either stitchSchemas() for composition or Apollo Federation’s buildSubgraphSchema() for federated use.
- “Output”: an executable GraphQLSchema object accessible via the ‘schema’ getter.
- The core GraphQLComponent receives configuration options and then builds a schema.
- Data flows from client requests (through something like Apollo Server or Gateway) to the schema, and further to resolvers and, if applicable, data sources.

##### Architectural Patterns
- Componentization: Break down the schema into discrete, re-usable components that can be composed (via the ‘imports’ option).
- Context and Data Source Injection: Emphasize how the GraphQLComponent injects a global context (with possible middleware transformations), and wraps provided data sources so that their methods get the context parameters seamlessly.

##### Relevant Technologies and Libraries
- GraphQL.js – as the underlying GraphQLSchema representation.
- graphql-tools – used for schema stitching and schema building.
- Apollo Federation – for federated schema construction when the ‘federation’ flag is true.
- Apollo Server – in the examples, used to serve the resulting schema.
- Debug – used for logging during component construction, which can be noted as part of the operational flow.

##### Final Touches
- That you cover both the primary library functionality (inside the lib directory) and how it’s used in the examples (composition vs. federation).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input
    UC["User Configuration"]:::input

    %% Library Core
    subgraph "Library Core"
        GC["GraphQLComponent Core"]:::library
        CONT["Context Management"]:::library
        DS["Data Source Integration"]:::library
        RES["Resolvers Processing"]:::library
        TRF["Transformations Module"]:::library
    end

    %% Schema Output
    ES["Executable GraphQLSchema"]:::library

    %% External Dependencies
    GT["graphql-tools"]:::external
    APF["Apollo Federation"]:::external

    %% Composition Example Branch
    subgraph "Composition Example"
        COMP["Composition Example Use Case"]:::example
        APS["Apollo Server"]:::example
    end

    %% Federation Example Branch
    subgraph "Federation Example"
        FED["Federation Example Use Case"]:::example
        AGW["Apollo Gateway"]:::example
    end

    %% Client Requests (External)
    CR["Client Requests (Composition)"]:::external
    CRF["Client Requests (Federation)"]:::external

    %% Relationships
    UC -->|"providesOptions"| GC
    GC -->|"usesContext"| CONT
    GC -->|"usesDatasource"| DS
    GC -->|"usesResolvers"| RES
    GC -->|"usesTransforms"| TRF
    GC -->|"buildsSchema_with_gt"| GT
    GC -->|"buildsSchema_with_apf"| APF
    GC -->|"producesSchema"| ES
    ES -->|"feedsComposition"| COMP
    ES -->|"feedsFederation"| FED
    COMP -->|"servedBy"| APS
    FED -->|"aggregatedBy"| AGW
    APS -->|"receivesQueries"| CR
    AGW -->|"receivesQueries"| CRF

    %% Styles
    classDef input fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px,color:#000000;
    classDef library fill:#E3F2FD,stroke:#2196F3,stroke-width:2px,color:#000000;
    classDef external fill:#FFCDD2,stroke:#F44336,stroke-width:2px,color:#000000;
    classDef example fill:#C8E6C9,stroke:#388E3C,stroke-width:2px,color:#000000;

    %% Click Events
    click GC "https://github.com/expediagroup/graphql-component/blob/master/lib/index.js"
    click CONT "https://github.com/expediagroup/graphql-component/blob/master/lib/context/index.js"
    click DS "https://github.com/expediagroup/graphql-component/blob/master/lib/datasource/index.js"
    click RES "https://github.com/expediagroup/graphql-component/blob/master/lib/resolvers/index.js"
    click TRF "https://github.com/expediagroup/graphql-component/blob/master/lib/transforms/index.js"
    click COMP "https://github.com/expediagroup/graphql-component/tree/master/examples/composition/"
    click FED "https://github.com/expediagroup/graphql-component/tree/master/examples/federation/"
```
