Skip to content

Commit

Permalink
Added endHeight to blocksFromHeight query (#2130)
Browse files Browse the repository at this point in the history
* feat: added endHeight and extra validaton on blocks from height query

* chore: changeset file

* fix: custom error message
  • Loading branch information
nil-amrutlal-dept committed May 15, 2024
1 parent 1d401bc commit 4a5045c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tall-numbers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/graph": patch
---

Added endHeight as an argument for blocksFromHeight query. Also added aditional validation on arguments
1 change: 1 addition & 0 deletions packages/apps/graph/generated-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ type Query {

"""Default: all chains"""
chainIds: [String!]
endHeight: Int
first: Int
last: Int
startHeight: Int!
Expand Down
23 changes: 22 additions & 1 deletion packages/apps/graph/src/graph/query/blocks-from-height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { prismaClient } from '@db/prisma-client';
import { getDefaultConnectionComplexity } from '@services/complexity';
import { normalizeError } from '@utils/errors';
import { networkData } from '@utils/network';
import { ZodError } from 'zod';
import { builder } from '../builder';
import Block from '../objects/block';

Expand All @@ -16,6 +17,12 @@ builder.queryField('blocksFromHeight', (t) =>
nonnegative: true,
},
}),
endHeight: t.arg.int({
required: false,
validate: {
positive: true,
},
}),
chainIds: t.arg.stringList({
required: false,
description: 'Default: all chains',
Expand All @@ -27,6 +34,7 @@ builder.queryField('blocksFromHeight', (t) =>
},
}),
},

cursor: 'hash',
type: Block,
complexity: (args) => ({
Expand All @@ -38,14 +46,27 @@ builder.queryField('blocksFromHeight', (t) =>
async resolve(
query,
__parent,
{ startHeight, chainIds = networkData.chainIds },
{ startHeight, chainIds = networkData.chainIds, endHeight },
) {
try {
if (endHeight && startHeight > endHeight) {
throw new ZodError([
{
code: 'custom',
message: 'startHeight must be lower than endHeight',
path: ['blocksFromHeight'],
},
]);
}

return await prismaClient.block.findMany({
...query,
where: {
height: {
gte: startHeight,
...(endHeight && {
lte: endHeight,
}),
},
...(chainIds?.length && {
chainId: {
Expand Down

0 comments on commit 4a5045c

Please sign in to comment.