Skip to content

Commit

Permalink
ag/graph/fix bug retrieving transfers (#2408)
Browse files Browse the repository at this point in the history
* fix(graph): bug where transactions weren't filtered correctly. Force that at least one of accountName, fungibleName, blockHash, or requestKey must be provided

* fix(graph): bug where transactions weren't filtered correctly. Force that at least one of accountName, fungibleName, blockHash, or requestKey must be provided
  • Loading branch information
alber70g authored Jul 15, 2024
1 parent 1707e63 commit b089abf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-llamas-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/graph': patch
---

Fix issues with performance of Graph queries on fungibleAccount.transfers
5 changes: 4 additions & 1 deletion packages/apps/graph/generated-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,10 @@ type Query {
"""
transaction(blockHash: String, minimumDepth: Int, requestKey: String!): Transaction

"""Retrieve transactions. Default page size is 20."""
"""
Retrieve transactions. Default page size is 20.
At least one of accountName, fungibleName, blockHash, or requestKey must be provided.
"""
transactions(accountName: String, after: String, before: String, blockHash: String, chainId: String, first: Int, fungibleName: String, last: Int, maxHeight: Int, minHeight: Int, minimumDepth: Int, requestKey: String): QueryTransactionsConnection!

"""Retrieve all transactions by a given public key."""
Expand Down
21 changes: 7 additions & 14 deletions packages/apps/graph/src/graph/objects/fungible-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,12 @@ export default builder.node(
await Promise.all([
await prismaClient.transfer.count({
where: {
senderAccount: parent.accountName,
// NOT: {
// receiverAccount: parent.accountName,
// },
moduleName: parent.fungibleName,
},
}),
await prismaClient.transfer.count({
where: {
receiverAccount: parent.accountName,
// NOT: {
// senderAccount: parent.accountName,
// },
OR: [
{ senderAccount: parent.accountName },
{
receiverAccount: parent.accountName,
},
],
moduleName: parent.fungibleName,
},
}),
Expand All @@ -197,7 +190,7 @@ export default builder.node(
await prismaClient.transfer.findMany({
...condition,
where: {
receiverAccount: parent.accountName,
senderAccount: parent.accountName,
NOT: {
receiverAccount: parent.accountName,
},
Expand Down
43 changes: 42 additions & 1 deletion packages/apps/graph/src/graph/query/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const generateTransactionFilter = async (args: {

builder.queryField('transactions', (t) =>
t.prismaConnection({
description: 'Retrieve transactions. Default page size is 20.',
description:
'Retrieve transactions. Default page size is 20.\n At least one of accountName, fungibleName, blockHash, or requestKey must be provided.',
type: Prisma.ModelName.Transaction,
cursor: 'blockHash_requestKey',
edgesNullable: false,
Expand Down Expand Up @@ -119,6 +120,25 @@ builder.queryField('transactions', (t) =>
}),
}),
async totalCount(__parent, args) {
// at least one of these should be in the args
// accountName && fungibleName
// blockHash
// requestKey
if (
stringNullOrEmpty(args.accountName) &&
stringNullOrEmpty(args.fungibleName) &&
stringNullOrEmpty(args.blockHash) &&
stringNullOrEmpty(args.requestKey)
) {
throw new ZodError([
{
code: 'custom',
message:
'At least one of accountName, fungibleName, blockHash, or requestKey must be provided',
path: ['transactions'],
},
]);
}
try {
return prismaClient.transaction.count({
where: await generateTransactionFilter(args),
Expand All @@ -143,6 +163,23 @@ builder.queryField('transactions', (t) =>
},
]);
}

if (
stringNullOrEmpty(args.accountName) &&
stringNullOrEmpty(args.fungibleName) &&
stringNullOrEmpty(args.blockHash) &&
stringNullOrEmpty(args.requestKey)
) {
throw new ZodError([
{
code: 'custom',
message:
'At least one of accountName, fungibleName, blockHash, or requestKey must be provided',
path: ['transactions'],
},
]);
}

let transactions: Transaction[] = [];
let skip = 0;
const take = query.take;
Expand Down Expand Up @@ -190,3 +227,7 @@ builder.queryField('transactions', (t) =>
},
}),
);

function stringNullOrEmpty(s: string | null | undefined): boolean {
return s === null || s === undefined || s === '';
}

0 comments on commit b089abf

Please sign in to comment.