Skip to content

Commit

Permalink
feature: add top-level activeStake_aggregate query
Browse files Browse the repository at this point in the history
Closes #345
  • Loading branch information
rhyslbw committed Nov 4, 2020
1 parent 9045496 commit 8e13a52
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
name: ActiveStake
configuration:
custom_root_fields:
select_aggregate: activeStake_aggregate
select: activeStake
custom_column_names: {}
object_relationships:
Expand Down
6 changes: 6 additions & 0 deletions packages/api-cardano-db-hasura/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type Query {
offset: Int
where: ActiveStake_bool_exp
): [ActiveStake]!
activeStake_aggregate (
limit: Int
order_by: [ActiveStake_order_by!]
offset: Int
where: ActiveStake_bool_exp
): ActiveStake_aggregate!
blocks (
limit: Int
order_by: [Block_order_by!]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query averageActiveStakeForAddress (
$address: StakeAddress!
) {
activeStake_aggregate (where: { address: { _eq: $address }}) {
aggregate {
count
avg {
amount
}
}
}
}
10 changes: 10 additions & 0 deletions packages/api-cardano-db-hasura/src/executableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export async function buildSchema (hasuraClient: HasuraClient, genesis: Genesis)
schema: hasuraSchema
})
},
activeStake_aggregate: (_root, args, context, info) => {
return delegateToSchema({
args,
context,
fieldName: 'activeStake_aggregate',
info,
operation: 'query',
schema: hasuraSchema
})
},
blocks: (_root, args, context, info) => {
return delegateToSchema({
args,
Expand Down
10 changes: 10 additions & 0 deletions packages/api-cardano-db-hasura/test/activeStake.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ describe('activeStake', () => {
expect(activeStake[0].epochNo).toBeDefined()
expect(activeStake[0].registeredWith.hash).toBeDefined()
})

it('can return aggregated active stake information for an address', async () => {
const result = await client.query({
query: await loadQueryNode('averageActiveStakeForAddress'),
variables: { address: 'stake1u8atejkgfn8772722rh03lmnxyshvjakk260gfsefamc6sga68ag2' }
})
const { activeStake_aggregate } = result.data
expect(activeStake_aggregate.aggregate.count).toBeDefined()
expect(activeStake_aggregate.aggregate.avg.amount).toBeDefined()
})
})

0 comments on commit 8e13a52

Please sign in to comment.