Skip to content

Commit

Permalink
Add page info to connection object
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Nov 16, 2023
1 parent e56d2c5 commit 5377830
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 2 additions & 0 deletions crates/torii/graphql/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub const DEFAULT_LIMIT: u64 = 10;
pub const BOOLEAN_TRUE: i64 = 1;
pub const LIMIT_OFFSET: u64 = 2;

pub const ENTITY_TABLE: &str = "entities";
pub const EVENT_TABLE: &str = "events";
Expand All @@ -21,6 +22,7 @@ pub const EVENT_TYPE_NAME: &str = "World__Event";
pub const SOCIAL_TYPE_NAME: &str = "World__Social";
pub const CONTENT_TYPE_NAME: &str = "World__Content";
pub const METADATA_TYPE_NAME: &str = "World__Metadata";
pub const PAGE_INFO_TYPE_NAME: &str = "World__PageInfo";
pub const TRANSACTION_TYPE_NAME: &str = "World__Transaction";
pub const QUERY_TYPE_NAME: &str = "World__Query";
pub const SUBSCRIPTION_TYPE_NAME: &str = "World__Subscription";
Expand Down
18 changes: 11 additions & 7 deletions crates/torii/graphql/src/object/connection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use async_graphql::connection::PageInfo;
use async_graphql::dynamic::{Field, InputValue, ResolverContext, TypeRef};
use async_graphql::{Error, Name, Value};
use sqlx::sqlite::SqliteRow;
use sqlx::Row;

use self::page_info::PageInfoObject;

use super::ObjectTrait;
use crate::constants::PAGE_INFO_TYPE_NAME;
use crate::query::order::Order;
use crate::query::value_mapping_from_row;
use crate::types::{GraphqlType, TypeData, TypeMapping, ValueMapping};
use crate::utils::extract;
use async_graphql::connection::PageInfo;
use async_graphql::dynamic::indexmap::IndexMap;
use async_graphql::dynamic::{Field, InputValue, ResolverContext, TypeRef};
use async_graphql::{Error, Name, Value};
use sqlx::sqlite::SqliteRow;
use sqlx::Row;

pub mod cursor;
pub mod edge;
Expand Down Expand Up @@ -40,6 +40,10 @@ impl ConnectionObject {
TypeData::Simple(TypeRef::named_list(format!("{}Edge", type_name))),
),
(Name::new("total_count"), TypeData::Simple(TypeRef::named_nn(TypeRef::INT))),
(
Name::new("page_info"),
TypeData::Nested((TypeRef::named(PAGE_INFO_TYPE_NAME), IndexMap::new())),
),
]);

Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/connection/page_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ObjectTrait for PageInfoObject {
impl PageInfoObject {
pub fn value_mapping(page_info: PageInfo) -> ValueMapping {
IndexMap::from([
(Name::new("has_revious_page"), Value::from(page_info.has_previous_page)),
(Name::new("has_previous_page"), Value::from(page_info.has_previous_page)),
(Name::new("has_next_page"), Value::from(page_info.has_next_page)),
(
Name::new("start_cursor"),
Expand Down
8 changes: 3 additions & 5 deletions crates/torii/graphql/src/query/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sqlx::{Result, Sqlite};

use super::filter::{Filter, FilterValue};
use super::order::{CursorDirection, Direction, Order};
use crate::constants::DEFAULT_LIMIT;
use crate::constants::{DEFAULT_LIMIT, LIMIT_OFFSET};
use crate::object::connection::{cursor, ConnectionArguments};

pub async fn count_rows(
Expand Down Expand Up @@ -62,8 +62,8 @@ pub async fn fetch_multiple_rows(
query.push_str(&format!(" WHERE {}", conditions.join(" AND ")));
}

let limit =
connection.first.or(connection.last).or(connection.limit).unwrap_or(DEFAULT_LIMIT) + 2;
let limit = connection.first.or(connection.last).or(connection.limit).unwrap_or(DEFAULT_LIMIT)
+ LIMIT_OFFSET;

// NOTE: Order is determined by the `order` param if provided, otherwise it's inferred from the
// `first` or `last` param. Explicit ordering take precedence
Expand Down Expand Up @@ -103,8 +103,6 @@ pub async fn fetch_multiple_rows(
end_cursor: None,
};

// cases

let order_field = match order {
Some(order) => format!("external_{}", order.field),
None => id_column.to_string(),
Expand Down

0 comments on commit 5377830

Please sign in to comment.