web.rs

Overview

This file implements a GraphQL web server interface that serves queries against a SQLite database and integrates with an SDK client context. It provides two main GraphQL endpoints: a legacy playground (/graphql_old) and a modern GraphiQL interface (/graphql). The server is built using the Warp web framework and supports two modes controlled by a feature flag: an "extended" API mode with enhanced query capabilities, and a "standard" API mode with basic query support.

The file handles asynchronous connections to a SQLite database, GraphQL schema construction with data loaders for efficient batch loading, HTTP routing for GraphQL requests and UIs, and error recovery. It is designed for efficient operation in a multi-threaded asynchronous environment.


Functions

open_db

async fn open_db(db_path: PathBuf) -> anyhow::Result<Pool<Sqlite>>

Purpose

Attempts to asynchronously open a read-only connection pool to a SQLite database file located at db_path. It retries up to three times with a 3-second interval between attempts if the connection fails.

Parameters

Returns

Implementation Details

Usage Example

let db_path = PathBuf::from("data/mydb.sqlite");
let pool = open_db(db_path).await?;

start

pub async fn start(
    bind_to: String,
    db_path: PathBuf,
    sdk_client: Arc<ClientContext>,
) -> anyhow::Result<()>

Purpose

Starts the Warp-based HTTP server that serves the GraphQL API and UI endpoints. It binds to the specified socket address and serves GraphQL requests against the database and SDK client context.

Parameters

Returns

Implementation Details

Usage Example

let bind_address = "127.0.0.1:8000".to_string();
let db_path = PathBuf::from("data/mydb.sqlite");
let sdk_client = Arc::new(ClientContext::new(...));
start(bind_address, db_path, sdk_client).await?;

Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Visual Diagram

flowchart TD
A[Start Function] --> B[open_db]
B --> C{Feature Flag: store_events_only}
C -- false --> D[Build Extended Schema]
C -- true --> E[Build Standard Schema]
D --> F[Attach DataLoaders & SDK Client]
E --> G[Attach Pool Only]
F & G --> H[Create GraphQL POST Filter]
H --> I[Define /graphql_old Playground Endpoint]
H --> J[Define /graphql GraphiQL Endpoint]
I & J --> K[Compose Routes with Error Handling]
K --> L[Run Warp Server Bind to Address]

Summary of Components

Component

Description

open_db

Opens and retries connection to SQLite database.

start

Configures GraphQL schema, HTTP routes, and runs the Warp server.

graphql_playground

Warp filter serving legacy GraphQL Playground UI at /graphql_old.

graphiql

Warp filter serving modern GraphiQL UI at /graphql.

graphql_post

Warp filter handling GraphQL POST requests.

DataLoader instances

Batch loaders for blocks, messages, and transactions in extended API mode.

Schema

GraphQL schemas built with async_graphql, either graphql_ext::QueryRoot or graphql_std::QueryRoot.

ClientContext

Shared SDK client context passed to schema for extended API.


References to Relevant Topics