feat(gql): add field aggregate.sum for query wallets
This commit is contained in:
parent
fd2ecdee4c
commit
3fbf29f71a
2 changed files with 44 additions and 2 deletions
|
@ -33,6 +33,31 @@ pub(crate) struct AmountWithBase {
|
|||
pub(crate) amount: i32,
|
||||
pub(crate) base: i32,
|
||||
}
|
||||
impl AmountWithBase {
|
||||
fn increment_base(self) -> Self {
|
||||
Self {
|
||||
amount: self.amount / 10,
|
||||
base: self.base + 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::ops::Add for AmountWithBase {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
#[allow(clippy::comparison_chain)]
|
||||
if self.base == rhs.base {
|
||||
Self {
|
||||
amount: self.amount + rhs.amount,
|
||||
base: self.base,
|
||||
}
|
||||
} else if self.base > rhs.base {
|
||||
self.add(rhs.increment_base())
|
||||
} else {
|
||||
self.increment_base().add(rhs)
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<SourceAmount> for AmountWithBase {
|
||||
fn from(sa: SourceAmount) -> Self {
|
||||
Self {
|
||||
|
@ -41,6 +66,11 @@ impl From<SourceAmount> for AmountWithBase {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl std::iter::Sum for AmountWithBase {
|
||||
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
|
||||
iter.fold(AmountWithBase::default(), std::ops::Add::add)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(async_graphql::SimpleObject)]
|
||||
pub(crate) struct EdgeTx {
|
||||
|
|
|
@ -32,7 +32,7 @@ impl WalletsQuery {
|
|||
#[graphql(desc = "minimal balance")] min_balance: Option<i64>,
|
||||
#[graphql(desc = "pagination", default)] pagination: Pagination,
|
||||
#[graphql(desc = "Wallet type filter", default)] wallet_type_filter: WalletTypeFilter,
|
||||
) -> async_graphql::Result<Connection<String, Wallet, EmptyFields, EmptyFields>> {
|
||||
) -> async_graphql::Result<Connection<String, Wallet, AggregateSum, EmptyFields>> {
|
||||
let QueryContext { is_whitelisted } = ctx.data::<QueryContext>()?;
|
||||
|
||||
let data = ctx.data::<GvaSchemaData>()?;
|
||||
|
@ -160,7 +160,19 @@ impl WalletsQuery {
|
|||
}
|
||||
};
|
||||
|
||||
let mut conn = Connection::new(has_previous_page, has_next_page);
|
||||
let sum = if ctx.look_ahead().field("aggregate").field("sum").exists() {
|
||||
data.iter().map(|wallet| wallet.balance).sum()
|
||||
} else {
|
||||
AmountWithBase::default()
|
||||
};
|
||||
|
||||
let mut conn = Connection::with_additional_fields(
|
||||
has_previous_page,
|
||||
has_next_page,
|
||||
AggregateSum {
|
||||
aggregate: Sum { sum },
|
||||
},
|
||||
);
|
||||
|
||||
conn.append(
|
||||
data.into_iter()
|
||||
|
|
Loading…
Add table
Reference in a new issue