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

add containsTokens to box query #69

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
10 changes: 9 additions & 1 deletion src/context/box-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BoxFindOptions = FindManyParams<BoxEntity> & {
ergoTrees?: string[];
spent?: boolean;
tokenId?: string;
containsTokens?: string[];
registers?: Registers;
minHeight?: number;
maxHeight?: number;
Expand All @@ -34,7 +35,8 @@ export class BoxRepository extends BaseRepository<BoxEntity> {
}

public override find(options: BoxFindOptions): Promise<BoxEntity[]> {
const { spent, tokenId, registers, minHeight, maxHeight, addresses, boxIds } = options;
const { spent, tokenId, registers, minHeight, maxHeight, addresses, boxIds, containsTokens } =
options;

const ergoTrees: string[] = options.ergoTrees ? options.ergoTrees : [];

Expand Down Expand Up @@ -79,6 +81,12 @@ export class BoxRepository extends BaseRepository<BoxEntity> {
.andWhere("asset.tokenId = :tokenId", { tokenId });
}

if (containsTokens && containsTokens.length > 0) {
query = query
.leftJoin("box.assets", "asset", "asset.boxId = box.boxId")
.andWhere("asset.tokenId IN (:...containsTokens)", { containsTokens });
}

if (registers && !isEmpty(registers)) {
query = query.leftJoin("box.registers", "rx", "box.boxId = rx.boxId");
for (const key in registers) {
Expand Down
14 changes: 13 additions & 1 deletion src/graphql/resolvers/box-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,19 @@ class BoxesQueryArgs {
@Field(() => Boolean, { nullable: true })
spent?: boolean;

@ValidateIf((o: BoxesQueryArgs) => {
return o.containsTokens !== undefined && o.containsTokens.length > 0;
})
@IsEmpty({
message: "'tokenId' filter can not be used in combination with 'containsTokens' filter."
})
@Field(() => String, { nullable: true })
tokenId?: string;

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

/** @deprecated */
@Field(() => String, { nullable: true })
address?: string;
Expand Down Expand Up @@ -120,6 +130,7 @@ export class BoxResolver {
ergoTrees,
ergoTreeTemplateHash,
tokenId,
containsTokens,
spent,
registers,
minHeight,
Expand All @@ -129,7 +140,7 @@ export class BoxResolver {
@Ctx() context: GraphQLContext,
@Info() info: GraphQLResolveInfo
) {
const arrayArguments = [addresses, boxIds, ergoTrees];
const arrayArguments = [addresses, boxIds, ergoTrees, containsTokens];
let arrayArgumentsLength = 0;
for (const arg of arrayArguments) {
if (arg) {
Expand Down Expand Up @@ -176,6 +187,7 @@ export class BoxResolver {
addresses,
ergoTrees,
tokenId,
containsTokens,
registers,
minHeight,
maxHeight,
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ type Query {
addresses(addresses: [String!]!): [Address!]!
blockHeaders(headerId: String, height: Int, parentId: String, skip: Int = 0, take: Int = 10): [Header!]!
blocks(headerId: String, height: Int, maxHeight: Int, minHeight: Int, skip: Int = 0, take: Int = 10): [Block!]!
boxes(address: String, addresses: [String!], boxId: String, boxIds: [String!], ergoTree: String, ergoTreeTemplateHash: String, ergoTrees: [String!], headerId: String, maxHeight: Int, minHeight: Int, registers: Registers, skip: Int = 0, spent: Boolean, take: Int = 50, tokenId: String, transactionId: String): [Box!]!
boxes(address: String, addresses: [String!], boxId: String, boxIds: [String!], containsTokens: [String!], ergoTree: String, ergoTreeTemplateHash: String, ergoTrees: [String!], headerId: String, 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!]!
Expand Down