Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch tx and box id in inputs #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/context/database-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GraphQLDatabaseLoader from "@ergo-graphql/typeorm-graphql-loader";
import { DataSource } from "typeorm";
import { DataInputEntity, InputEntity } from "../entities";
import { DataInputEntity } from "../entities";
import { BaseRepository, RepositoryDataContext } from "./base-repository";
import { BoxRepository } from "./box-repository";
import { HeaderRepository } from "./header-repository";
Expand All @@ -12,13 +12,14 @@ import { UnconfirmedInputRepository } from "./unconfirmed-input-repository";
import { EpochsRepository } from "./epochs-repository";
import { TokenRepository } from "./token-repository";
import { BlocksRepository } from "./blocks-repository";
import { InputsRepository } from "./input-repository";

export class DatabaseContext {
public readonly transactions!: TransactionRepository;
public readonly blockInfo!: BlocksRepository;
public readonly boxes!: BoxRepository;
public readonly dataInputs!: IRepository<DataInputEntity>;
public readonly inputs!: IRepository<InputEntity>;
public readonly inputs!: InputsRepository;
public readonly headers!: HeaderRepository;
public readonly tokens!: TokenRepository;
public readonly epochs!: EpochsRepository;
Expand All @@ -37,6 +38,7 @@ export class DatabaseContext {
this.boxes = new BoxRepository(context);
this.headers = new HeaderRepository(context);
this.epochs = new EpochsRepository(context);
this.inputs = new InputsRepository(context);

this.unconfirmedTransactions = new UnconfirmedTransactionRepository(context);
this.unconfirmedBoxes = new UnconfirmedBoxRepository(context);
Expand All @@ -46,6 +48,5 @@ export class DatabaseContext {

const defaults = { where: { mainChain: true } };
this.dataInputs = new BaseRepository(DataInputEntity, "dti", { context, defaults });
this.inputs = new BaseRepository(InputEntity, "input", { context, defaults });
}
}
44 changes: 44 additions & 0 deletions src/context/input-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { BaseRepository, RepositoryDataContext } from "./base-repository";
import { InputEntity } from "../entities";
import { FindManyParams } from "./repository-interface";

type InputFindOptions = FindManyParams<InputEntity> & {
transactionId?: string;
transactionIds?: string[];
boxId?: string;
boxIds?: string[];
};

export class InputsRepository extends BaseRepository<InputEntity> {
constructor(context: RepositoryDataContext) {
super(InputEntity, "inputs", {
context,
defaults: { where: { mainChain: true } }
});
}

public override async find(options: InputFindOptions): Promise<InputEntity[]> {
const { transactionId, transactionIds, boxId, boxIds } = options;
const txIds = transactionIds ?? [];
if (transactionId) {
txIds.push(transactionId);
}
const bxIds = boxIds ?? [];
if (boxId) {
bxIds.push(boxId);
}
return this.findBase(options, (filterQuery) => {
if (txIds.length > 0) {
filterQuery = filterQuery.andWhere(`${this.alias}.transactionId IN (:...txIds)`, {
txIds
});
}
if (bxIds.length > 0) {
filterQuery = filterQuery.andWhere(`${this.alias}.boxId IN (:...bxIds)`, {
bxIds
});
}
return filterQuery;
});
}
}
23 changes: 17 additions & 6 deletions src/graphql/resolvers/input-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ import { Input } from "../objects";
import { removeUndefined } from "../../utils";
import { GraphQLContext } from "../context-type";
import { PaginationArguments } from "./pagination-arguments";
import { ArrayMaxSize } from "class-validator";

@ArgsType()
class InputsQueryArgs {
/** @deprecated */
@Field(() => String, { nullable: true })
transactionId?: string;

@Field(() => [String], { nullable: true })
@ArrayMaxSize(20)
transactionIds?: string[];

/** @deprecated */
@Field(() => String, { nullable: true })
boxId?: string;

@Field(() => [String], { nullable: true })
@ArrayMaxSize(20)
boxIds?: string[];

@Field(() => String, { nullable: true })
headerId?: string;
}
Expand All @@ -21,18 +32,18 @@ class InputsQueryArgs {
export class InputResolver {
@Query(() => [Input])
async inputs(
@Args() { transactionId, boxId, headerId }: InputsQueryArgs,
@Args() { transactionId, transactionIds, boxId, boxIds, headerId }: InputsQueryArgs,
@Args({ validate: true }) { skip, take }: PaginationArguments,
@Ctx() context: GraphQLContext,
@Info() info: GraphQLResolveInfo
) {
return context.repository.inputs.find({
resolverInfo: info,
where: removeUndefined({
transactionId,
boxId,
headerId
}),
where: removeUndefined({ headerId }),
boxId,
boxIds,
transactionId,
transactionIds,
skip,
take
});
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ type Query {
boxes(address: String, addresses: [String!], boxId: String, boxIds: [String!], ergoTree: String, ergoTreeTemplateHash: String, ergoTrees: [String!], headerId: String, heightType: HeightFilterType = settlement, maxHeight: Int, minHeight: Int, registers: Registers, skip: Int = 0, spent: Boolean, take: Int = 50, tokenId: String, transactionId: String): [Box!]!
dataInputs(boxId: String, headerId: String, skip: Int = 0, take: Int = 50, transactionId: String): [DataInput!]!
info: Info!
inputs(boxId: String, headerId: String, skip: Int = 0, take: Int = 50, transactionId: String): [Input!]!
inputs(boxId: String, boxIds: [String!], headerId: String, skip: Int = 0, take: Int = 50, transactionId: String, transactionIds: [String!]): [Input!]!
mempool: Mempool!
state: State!
tokens(boxId: String, name: String, skip: Int = 0, take: Int = 50, tokenId: String, tokenIds: [String!]): [Token!]!
Expand Down
Loading