Skip to content

Commit

Permalink
refactor(torii-gql): get rid of world indexing error (#2827)
Browse files Browse the repository at this point in the history
* refactor(torii-gql): get rid of world indexing error

* fix: fmt and clippy

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
Larkooo and glihm authored Dec 23, 2024
1 parent 845c5b1 commit 548783b
Showing 1 changed file with 2 additions and 24 deletions.
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::dynamic::Schema;
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);
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 {
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 @@ fn graphql_filter(

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)
}

0 comments on commit 548783b

Please sign in to comment.