Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(torii-gql): get rid of world indexing error #2827

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions crates/torii/graphql/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,26 @@
use async_graphql::http::GraphiQLSource;
use async_graphql::Request;
use async_graphql_warp::graphql_subscription;
use serde_json::json;
use sqlx::{Pool, Sqlite};
use tokio::sync::broadcast::Receiver;
use warp::{Filter, Rejection, Reply};

use super::schema::build_schema;
use crate::constants::MODEL_TABLE;
use crate::query::data::count_rows;

pub async fn new(
mut shutdown_rx: Receiver<()>,
pool: &Pool<Sqlite>,
) -> (SocketAddr, impl Future<Output = ()> + 'static) {
let schema = build_schema(pool).await.unwrap();
let mut conn = pool.acquire().await.unwrap();
let num_models = count_rows(&mut conn, MODEL_TABLE, &None, &None).await.unwrap();

let routes = graphql_filter(schema, num_models == 0);
let routes = graphql_filter(schema);

Check warning on line 19 in crates/torii/graphql/src/server.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/server.rs#L19

Added line #L19 was not covered by tests
warp::serve(routes).bind_with_graceful_shutdown(([127, 0, 0, 1], 0), async move {
shutdown_rx.recv().await.ok();
})
}

fn graphql_filter(
schema: Schema,
is_empty: bool,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
fn graphql_filter(schema: Schema) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {

Check warning on line 25 in crates/torii/graphql/src/server.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/server.rs#L25

Added line #L25 was not covered by tests
let graphql_post = async_graphql_warp::graphql(schema.clone()).and_then(
move |(schema, request): (Schema, Request)| async move {
if is_empty {
return Ok::<_, Rejection>(empty_response());
}

// Execute query
let response = schema.execute(request).await;
// Return result
Expand All @@ -57,12 +44,3 @@

graphql_subscription(schema).or(graphql_post).or(playground_filter)
}

fn empty_response() -> warp::reply::Json {
let empty_response = json!({
"errors": [{
"message": "World does not have any indexed data yet."
}]
});
warp::reply::json(&empty_response)
}
Loading