Skip to content

Commit 911db1b

Browse files
committed
fix(resolver): claims and fractions nested queries
1 parent 402d9b8 commit 911db1b

File tree

7 files changed

+101
-136
lines changed

7 files changed

+101
-136
lines changed

src/graphql/schemas/resolvers/baseTypes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,19 @@ export function createBaseResolver<T extends ClassType>(
103103
return queries.data.executeTakeFirst();
104104
}
105105

106+
console.log("got hypercerts queries: ", queries);
107+
106108
return this.supabaseCachingService.db
107109
.transaction()
108110
.execute(async (transaction) => {
111+
console.log("got transaction: ", transaction);
112+
console.log("got transaction execute query: ", queries.data);
109113
const dataRes = await transaction.executeQuery(queries.data);
114+
115+
console.log("got hypercerts data: ", dataRes);
110116
const countRes = await transaction.executeQuery(queries.count);
117+
118+
console.log("got hypercerts data and count: ", dataRes, countRes);
111119
return {
112120
data: dataRes.rows,
113121
count: countRes.rows[0].count,

src/graphql/schemas/resolvers/fractionResolver.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,21 @@ class FractionResolver extends FractionBaseResolver {
2424
}
2525

2626
@FieldResolver()
27-
async orders(@Root() fraction: Partial<Fraction>) {
27+
async metadata(@Root() fraction: Fraction) {
28+
if (!fraction.claims_id) {
29+
return;
30+
}
31+
32+
return await this.getMetadata(
33+
{
34+
where: { hypercerts: { id: { eq: fraction.claims_id } } },
35+
},
36+
true,
37+
);
38+
}
39+
40+
@FieldResolver()
41+
async orders(@Root() fraction: Fraction) {
2842
if (!fraction.fraction_id) {
2943
return null;
3044
}
@@ -71,21 +85,7 @@ class FractionResolver extends FractionBaseResolver {
7185
}
7286

7387
@FieldResolver()
74-
async metadata(@Root() fraction: Partial<Fraction>) {
75-
if (!fraction.claims_id) {
76-
return;
77-
}
78-
79-
return await this.getMetadata(
80-
{
81-
where: { hypercerts: { id: { eq: fraction.claims_id } } },
82-
},
83-
true,
84-
);
85-
}
86-
87-
@FieldResolver()
88-
async sales(@Root() fraction: Partial<Fraction>) {
88+
async sales(@Root() fraction: Fraction) {
8989
if (!fraction.fraction_id) {
9090
return null;
9191
}

src/graphql/schemas/resolvers/hypercertResolver.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ class HypercertResolver extends HypercertBaseResolver {
7373
return;
7474
}
7575

76-
return await this.getFractions(
77-
{ where: { hypercert_id: { eq: hypercert.hypercert_id } } },
78-
false,
79-
);
76+
return await this.getFractions({
77+
where: { hypercert_id: { eq: hypercert.hypercert_id } },
78+
});
8079
}
8180

8281
@FieldResolver()

src/graphql/schemas/typeDefs/fractionTypeDefs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Fraction extends BasicTypeDef {
1717
description: "Address of the owner of the fractions",
1818
})
1919
owner_address?: string;
20+
2021
@Field(() => EthBigInt, {
2122
nullable: true,
2223
description: "Units held by the fraction",
@@ -43,6 +44,7 @@ class Fraction extends BasicTypeDef {
4344
description: "Marketplace orders related to this fraction",
4445
})
4546
orders?: GetOrdersResponse;
47+
4648
@Field(() => Metadata, {
4749
nullable: true,
4850
description: "The metadata for the fraction",

src/graphql/schemas/typeDefs/hypercertTypeDefs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ class Hypercert extends HypercertBaseType {
3737
"Transferable fractions representing partial ownership of the hypercert",
3838
})
3939
fractions?: GetFractionsResponse;
40+
4041
@Field(() => GetAttestationsResponse, {
4142
nullable: true,
4243
description: "Attestations for the hypercert or parts of its data",
4344
})
4445
attestations?: GetAttestationsResponse;
46+
4547
@Field(() => GetOrdersForHypercertResponse, {
4648
nullable: true,
4749
description: "Marketplace orders related to this hypercert",
Lines changed: 32 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,49 @@
1-
import {
2-
IdSearchOptions,
3-
NumberArraySearchOptions,
4-
NumberSearchOptions,
5-
StringArraySearchOptions,
6-
StringSearchOptions
7-
} from "../inputs/searchOptions.js";
8-
91
type OperandType = string | number | bigint | string[] | bigint[];
10-
type OperatorType = "eq" | "gt" | "gte" | "lt" | "lte" | "ilike" | "contains" | "startsWith" | "endsWith"
2+
type OperatorType =
3+
| "eq"
4+
| "gt"
5+
| "gte"
6+
| "lt"
7+
| "lte"
8+
| "ilike"
9+
| "contains"
10+
| "startsWith"
11+
| "endsWith";
12+
13+
enum OperatorSymbols {
14+
eq = "=",
15+
gt = ">",
16+
gte = ">=",
17+
lt = "<",
18+
lte = "<=",
19+
ilike = "ilike",
20+
}
1121

12-
export const generateFilterValues = (column: string, operator: OperatorType, operand: OperandType) => {
22+
export const generateFilterValues = (
23+
column: string,
24+
operator: OperatorType,
25+
operand: OperandType,
26+
) => {
1327
console.log("generateFilterValues", column, operator, operand);
1428

1529
switch (operator) {
1630
case "eq":
17-
return [column, "=", operand];
31+
return [column, OperatorSymbols.eq, operand];
1832
case "gt":
19-
return [column, ">", operand];
33+
return [column, OperatorSymbols.gt, operand];
2034
case "gte":
21-
return [column, ">=", operand];
35+
return [column, OperatorSymbols.gte, operand];
2236
case "lt":
23-
return [column, "<", operand];
37+
return [column, OperatorSymbols.lt, operand];
2438
case "lte":
25-
return [column, "<=", operand];
39+
return [column, OperatorSymbols.lte, operand];
2640
case "contains":
27-
return [column, "like", `%${operand}%`];
41+
return [column, OperatorSymbols.ilike, operand];
2842
case "startsWith":
29-
return [column, "like", `${operand}%`];
43+
return [column, OperatorSymbols.ilike, operand];
3044
case "endsWith":
31-
return [column, "like", `%${operand}`];
45+
return [column, OperatorSymbols.ilike, operand];
3246
}
3347

3448
return [];
3549
};
36-
37-
38-
39-
const generateArrayFilters = (value: NumberArraySearchOptions | StringArraySearchOptions, column: string) => {
40-
const filters: [OperatorType, string, OperandType][] = [];
41-
for (const [operator, operand] of Object.entries(value)) {
42-
if (!operand) continue;
43-
44-
// Assert operand is an array of numbers
45-
if (!Array.isArray(operand)) {
46-
throw new Error(`Expected operand to be an array, but got ${typeof operand}`);
47-
}
48-
49-
switch (operator) {
50-
case "contains":
51-
filters.push(["contains", column, operand]);
52-
break;
53-
}
54-
}
55-
return filters;
56-
};
57-
58-
function isStringSearchOptions(value: unknown): value is StringSearchOptions {
59-
if (typeof value !== "object" || value === null) {
60-
return false;
61-
}
62-
63-
const possibleStringSearchOptions = value as Partial<StringSearchOptions>;
64-
65-
// Check for properties unique to StringSearchOptions
66-
const keys = ["eq", "contains", "startsWith", "endsWith"];
67-
return keys.some(key => key in possibleStringSearchOptions);
68-
}
69-
70-
function isNumberSearchOptions(value: unknown): value is NumberSearchOptions {
71-
if (typeof value !== "object" || value === null) {
72-
return false;
73-
}
74-
75-
const possibleNumberSearchOptions = value as Partial<NumberSearchOptions>;
76-
77-
// Check for properties unique to NumberSearchOptions
78-
const keys = ["eq", "gt", "gte", "lt", "lte"];
79-
return keys.some(key => key in possibleNumberSearchOptions);
80-
}
81-
82-
function isIdSearchOptions(value: unknown): value is IdSearchOptions {
83-
if (typeof value !== "object" || value === null) {
84-
return false;
85-
}
86-
87-
const possibleIdSearchOptions = value as Partial<NumberSearchOptions>;
88-
89-
// Check for properties unique to IdSearchOptions
90-
const keys = ["eq", "contains", "startsWith", "endsWith"];
91-
return keys.some(key => key in possibleIdSearchOptions);
92-
93-
}
94-
95-
function isStringArraySearchOptions(value: unknown): value is StringArraySearchOptions {
96-
if (!Array.isArray(value) || value === null) {
97-
return false;
98-
}
99-
100-
const possibleStringArraySearchOptions = value as Partial<StringArraySearchOptions>;
101-
102-
// Check for properties unique to StringArraySearchOptions
103-
const keys = ["contains"];
104-
return keys.some(key => key in possibleStringArraySearchOptions);
105-
}
106-
107-
function isNumberArraySearchOptions(value: unknown): value is NumberArraySearchOptions {
108-
if (!Array.isArray(value) || value === null) {
109-
return false;
110-
}
111-
112-
const possibleNumberArraySearchOptions = value as Partial<NumberArraySearchOptions>;
113-
114-
// Check for properties unique to NumberArraySearchOptions
115-
const keys = ["contains"];
116-
return keys.some(key => key in possibleNumberArraySearchOptions);
117-
}
118-
119-
const buildFilters = (value: unknown, column: string) => {
120-
121-
if (isStringArraySearchOptions(value) || isNumberArraySearchOptions(value)) {
122-
return generateArrayFilters(value, column);
123-
}
124-
125-
return [];
126-
};
127-
128-

src/services/SupabaseCachingService.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,23 @@ export class SupabaseCachingService {
110110
case "claims":
111111
return this.db
112112
.selectFrom("claims")
113-
.selectAll("claims") // Select all columns from the claims table
114113
.$if(args.where?.metadata, (qb) =>
115114
qb.innerJoin("metadata", "metadata.uri", "claims.uri"),
116115
)
117116
.$if(args.where?.attestations, (qb) =>
118117
qb.innerJoin("attestations", "attestations.claims_id", "claims.id"),
119118
)
120119
.$if(args.where?.fractions, (qb) =>
121-
qb.innerJoin("fractions_view", (join) =>
122-
join.on("fractions_view.claims_id", "=", "claims.id"),
120+
qb.innerJoin(
121+
"fractions_view",
122+
"fractions_view.claims_id",
123+
"claims.id",
123124
),
124125
)
125126
.$if(args.where?.contract, (qb) =>
126127
qb.innerJoin("contracts", "contracts.id", "claims.contracts_id"),
127-
);
128+
)
129+
.selectAll(); // Select all columns from the claims table
128130
case "contracts":
129131
return this.db.selectFrom("contracts").selectAll();
130132
case "fractions":
@@ -193,7 +195,11 @@ export class SupabaseCachingService {
193195
qb.innerJoin("attestations", "attestations.claims_id", "claims.id"),
194196
)
195197
.$if(args.where?.fractions, (qb) =>
196-
qb.innerJoin("fractions", "fractions.claims_id", "claims.id"),
198+
qb.innerJoin(
199+
"fractions_view",
200+
"fractions_view.claims_id",
201+
"claims.id",
202+
),
197203
)
198204
.$if(args.where?.contract, (qb) =>
199205
qb.innerJoin("contracts", "contracts.id", "claims.contracts_id"),
@@ -262,6 +268,7 @@ export class SupabaseCachingService {
262268

263269
const res = generateFilterValues(column, _column, _value);
264270
if (res.length > 0) {
271+
console.log("got filter values: ", res);
265272
return eb(
266273
`${tableName.toString()}.${res[0]}`,
267274
res[1],
@@ -281,17 +288,39 @@ export class SupabaseCachingService {
281288
_table = "contracts";
282289
}
283290

291+
if (column === "fractions") {
292+
_table = "fractions_view";
293+
}
294+
295+
console.log(
296+
"generating nested filter values for: ",
297+
_table,
298+
_column,
299+
operator,
300+
operand,
301+
);
302+
284303
const [_col, _symbol, _input] = generateFilterValues(
285304
`${_table}.${_column}`,
286305
operator,
287306
operand,
288307
);
308+
309+
console.log(
310+
"got nested filter values: ",
311+
_col,
312+
_symbol,
313+
_input,
314+
);
315+
289316
filters.push(eb(_col, _symbol, _input));
290317
}
291318

292319
return filters.flat();
293320
});
294321
}
322+
323+
console.log("returning column and value: ", column, value);
295324
return column && value ? eb(column, "=", value) : [];
296325
}),
297326
),
@@ -369,6 +398,10 @@ export class SupabaseCachingService {
369398
_table = "contracts";
370399
}
371400

401+
if (column === "fractions") {
402+
_table = "fractions_view";
403+
}
404+
372405
const [_col, _symbol, _input] = generateFilterValues(
373406
`${_table}.${_column}`,
374407
operator,

0 commit comments

Comments
 (0)