Skip to content

Commit 8f2e1f0

Browse files
Move dataloader to router handler for tracing
1 parent ef09aaf commit 8f2e1f0

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

charts/processed_data/charts/processed_data/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ type: application
55

66
version: 0.1.0
77

8-
appVersion: 0.1.0-rc8
8+
appVersion: 0.1.0-rc9

processed_data/src/graphql/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ use self::entities::AutoProcProgram;
2626
/// The GraphQL schema exposed by the service
2727
pub type RootSchema = Schema<Query, EmptyMutation, EmptySubscription>;
2828

29-
/// A schema builder for the service
30-
pub fn root_schema_builder(
31-
database: DatabaseConnection,
32-
) -> SchemaBuilder<Query, EmptyMutation, EmptySubscription> {
33-
Schema::build(Query, EmptyMutation, EmptySubscription)
34-
.data(DataLoader::new(
29+
pub trait AddDataLoadersExt {
30+
fn add_data_loaders(self, database: DatabaseConnection) -> Self;
31+
}
32+
33+
impl AddDataLoadersExt for async_graphql::Request {
34+
#[instrument(name = "add_data_loaders", skip(self))]
35+
fn add_data_loaders(self, database: DatabaseConnection) -> Self {
36+
self.data(DataLoader::new(
3537
ProcessedDataLoader::new(database.clone()),
3638
tokio::spawn,
3739
))
@@ -72,7 +74,12 @@ pub fn root_schema_builder(
7274
tokio::spawn,
7375
))
7476
.data(database)
75-
.enable_federation()
77+
}
78+
}
79+
80+
/// A schema builder for the service
81+
pub fn root_schema_builder() -> SchemaBuilder<Query, EmptyMutation, EmptySubscription> {
82+
Schema::build(Query, EmptyMutation, EmptySubscription).enable_federation()
7683
}
7784

7885
/// The root query of the service

processed_data/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async fn setup_database(database_url: Url) -> Result<DatabaseConnection, Transac
143143
}
144144

145145
/// Creates an [`axum::Router`] serving GraphiQL, synchronous GraphQL and GraphQL subscriptions
146-
fn setup_router(schema: RootSchema) -> Router {
146+
fn setup_router(schema: RootSchema, database: DatabaseConnection) -> Router {
147147
#[allow(clippy::missing_docs_in_private_items)]
148148
const GRAPHQL_ENDPOINT: &str = "/";
149149

@@ -153,7 +153,7 @@ fn setup_router(schema: RootSchema) -> Router {
153153
get(Html(
154154
GraphiQLSource::build().endpoint(GRAPHQL_ENDPOINT).finish(),
155155
))
156-
.post(GraphQLHandler::new(schema)),
156+
.post(GraphQLHandler::new(schema, database)),
157157
)
158158
.layer(OtelInResponseLayer)
159159
.layer(OtelAxumLayer::default())
@@ -241,13 +241,12 @@ async fn main() {
241241
Cli::Serve(args) => {
242242
setup_telemetry(args.log_level, args.otel_collector_url).unwrap();
243243
let database = setup_database(args.database_url).await.unwrap();
244-
let schema = root_schema_builder(database).finish();
245-
let router = setup_router(schema);
244+
let schema = root_schema_builder().finish();
245+
let router = setup_router(schema, database);
246246
serve(router, args.port).await.unwrap();
247247
}
248248
Cli::Schema(args) => {
249-
let database = setup_database(args.database_url).await.unwrap();
250-
let schema = root_schema_builder(database).finish();
249+
let schema = root_schema_builder().finish();
251250
let schema_string = schema.sdl_with_options(SDLExportOptions::new().federation());
252251
if let Some(path) = args.path {
253252
let mut file = File::create(path).unwrap();

processed_data/src/route_handlers.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ use axum::{
77
response::{IntoResponse, Response},
88
RequestExt,
99
};
10-
use axum_extra::{
11-
headers::{authorization::Bearer, Authorization},
12-
TypedHeader,
13-
};
10+
use sea_orm::DatabaseConnection;
1411
use std::{future::Future, pin::Pin};
1512

13+
use crate::graphql::AddDataLoadersExt;
14+
1615
/// An [`Handler`] which executes an [`Executor`] including the [`Authorization<Bearer>`] in the [`async_graphql::Context`]
1716
#[derive(Debug, Clone)]
1817
pub struct GraphQLHandler<E: Executor> {
1918
/// The GraphQL executor used to process the request
2019
executor: E,
20+
database: DatabaseConnection,
2121
}
2222

2323
impl<E: Executor> GraphQLHandler<E> {
2424
/// Constructs an instance of the handler with the provided schema.
25-
pub fn new(executor: E) -> Self {
26-
Self { executor }
25+
pub fn new(executor: E, database: DatabaseConnection) -> Self {
26+
Self { executor, database }
2727
}
2828
}
2929

@@ -33,18 +33,13 @@ where
3333
{
3434
type Future = Pin<Box<dyn Future<Output = Response> + Send + 'static>>;
3535

36-
fn call(self, mut req: Request, _state: S) -> Self::Future {
36+
fn call(self, req: Request, _state: S) -> Self::Future {
3737
Box::pin(async move {
38-
let token = req
39-
.extract_parts::<TypedHeader<Authorization<Bearer>>>()
40-
.await
41-
.ok()
42-
.map(|token| token.0);
4338
let request = req.extract::<GraphQLRequest, _>().await;
4439
match request {
4540
Ok(request) => GraphQLResponse::from(
4641
self.executor
47-
.execute(request.into_inner().data(token))
42+
.execute(request.into_inner().add_data_loaders(self.database))
4843
.await,
4944
)
5045
.into_response(),

0 commit comments

Comments
 (0)