Skip to content

Commit 7a6f0b1

Browse files
committed
refactor(fraction): restructure fraction resolver and related components
Refactored fraction-related code to improve organization and maintainability: - Moved FractionResolver from graphql to services directory - Updated import paths in composed resolver - Relocated fractionResolver from local to services directory - Enhanced FractionEntityService with comprehensive documentation - Added detailed JSDoc comments for FractionService and FractionsQueryStrategy - Introduced comprehensive test coverage for FractionService, FractionsQueryStrategy, and FractionResolver - Improved type safety and code clarity across related files - updated error handling to not throw in failed resolved fields queries - introduced test utils for recurring methods
1 parent 25d5cc4 commit 7a6f0b1

File tree

9 files changed

+997
-108
lines changed

9 files changed

+997
-108
lines changed

src/graphql/schemas/resolvers/composed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HypercertResolver } from "./hypercertResolver.js";
22
import { MetadataResolver } from "./metadataResolver.js";
33
import { ContractResolver } from "../../../services/graphql/resolvers/contractResolver.js";
4-
import { FractionResolver } from "./fractionResolver.js";
4+
import { FractionResolver } from "../../../services/graphql/resolvers/fractionResolver.js";
55
import { AttestationResolver } from "../../../services/graphql/resolvers/attestationResolver.js";
66
import { AttestationSchemaResolver } from "../../../services/graphql/resolvers/attestationSchemaResolver.js";
77
import { OrderResolver } from "./orderResolver.js";

src/graphql/schemas/resolvers/fractionResolver.ts

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/services/database/entities/FractionEntityService.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@ import {
88
type EntityService,
99
} from "./EntityServiceFactory.js";
1010

11+
/** Type representing a selected fraction record from the database */
1112
export type FractionSelect = Selectable<CachingDatabase["fractions_view"]>;
1213

14+
/**
15+
* Service class for managing fraction entities in the database.
16+
* Handles CRUD operations for hypercert fractions, which represent ownership units of hypercerts.
17+
*
18+
* This service provides methods to:
19+
* - Query multiple fractions with filtering and pagination
20+
* - Retrieve single fraction records by various criteria
21+
*
22+
* @injectable
23+
*/
1324
@injectable()
1425
export class FractionService {
26+
/** The underlying entity service instance for database operations */
1527
private entityService: EntityService<
1628
CachingDatabase["fractions_view"],
1729
GetFractionsArgs
1830
>;
1931

32+
/**
33+
* Initializes a new instance of the FractionService.
34+
* Creates an EntityService instance for the fractions_view table.
35+
*/
2036
constructor() {
2137
this.entityService = createEntityService<
2238
CachingDatabase,
@@ -25,10 +41,42 @@ export class FractionService {
2541
>("fractions_view", "FractionEntityService", kyselyCaching);
2642
}
2743

44+
/**
45+
* Retrieves multiple fractions based on the provided arguments.
46+
*
47+
* @param args - Query arguments for filtering fractions
48+
* @returns Promise resolving to an object containing:
49+
* - data: Array of fraction records
50+
* - count: Total number of matching records
51+
* @throws {Error} If the database query fails
52+
*
53+
* @example
54+
* ```typescript
55+
* // Get all fractions owned by a specific address
56+
* const result = await fractionService.getFractions({
57+
* where: { owner_address: { eq: "0x..." } }
58+
* });
59+
* ```
60+
*/
2861
async getFractions(args: GetFractionsArgs) {
2962
return this.entityService.getMany(args);
3063
}
3164

65+
/**
66+
* Retrieves a single fraction based on the provided arguments.
67+
*
68+
* @param args - Query arguments for filtering the fraction
69+
* @returns Promise resolving to a single fraction record or undefined if not found
70+
* @throws {Error} If the database query fails
71+
*
72+
* @example
73+
* ```typescript
74+
* // Get a specific fraction by ID
75+
* const fraction = await fractionService.getFraction({
76+
* where: { units: { eq: 100n } }
77+
* });
78+
* ```
79+
*/
3280
async getFraction(args: GetFractionsArgs) {
3381
return this.entityService.getSingle(args);
3482
}

src/services/database/strategies/FractionsQueryStrategy.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,49 @@ import { CachingDatabase } from "../../../types/kyselySupabaseCaching.js";
44
import { QueryStrategy } from "./QueryStrategy.js";
55
import { isWhereEmpty } from "../../../lib/strategies/isWhereEmpty.js";
66

7+
/**
8+
* Strategy for building database queries for fractions.
9+
* Implements query logic for fraction retrieval and counting.
10+
*
11+
* This strategy extends the base QueryStrategy to provide fraction-specific query building.
12+
* It handles:
13+
* - Basic data retrieval from the fractions_view
14+
* - Filtering based on metadata relationships
15+
* - Counting operations with appropriate joins
16+
*
17+
* @template CachingDatabase - The database type containing the fractions_view table
18+
*/
719
export class FractionsQueryStrategy extends QueryStrategy<
820
CachingDatabase,
921
"fractions_view",
1022
GetFractionsArgs
1123
> {
1224
protected readonly tableName = "fractions_view" as const;
1325

26+
/**
27+
* Builds a query to retrieve fraction data.
28+
* Handles optional metadata filtering through joins with claims and metadata tables.
29+
*
30+
* @param db - Kysely database instance
31+
* @param args - Optional query arguments for filtering
32+
* @returns A query builder for retrieving fraction data
33+
*
34+
* @example
35+
* ```typescript
36+
* // Basic query to select all fractions
37+
* buildDataQuery(db);
38+
* // SELECT * FROM fractions_view
39+
*
40+
* // Query with metadata filtering
41+
* buildDataQuery(db, { where: { metadata: { ... } } });
42+
* // SELECT * FROM fractions_view
43+
* // WHERE EXISTS (
44+
* // SELECT * FROM claims
45+
* // LEFT JOIN metadata ON metadata.id = fractions_view.claims_id
46+
* // WHERE claims.id = fractions_view.claims_id
47+
* // )
48+
* ```
49+
*/
1450
buildDataQuery(db: Kysely<CachingDatabase>, args?: GetFractionsArgs) {
1551
if (!args) {
1652
return db.selectFrom(this.tableName).selectAll();
@@ -30,6 +66,30 @@ export class FractionsQueryStrategy extends QueryStrategy<
3066
.selectAll(this.tableName);
3167
}
3268

69+
/**
70+
* Builds a query to count fractions.
71+
* Handles optional metadata filtering through joins with claims and metadata tables.
72+
*
73+
* @param db - Kysely database instance
74+
* @param args - Optional query arguments for filtering
75+
* @returns A query builder for counting fractions
76+
*
77+
* @example
78+
* ```typescript
79+
* // Count all fractions
80+
* buildCountQuery(db);
81+
* // SELECT COUNT(*) as count FROM fractions_view
82+
*
83+
* // Count with metadata filtering
84+
* buildCountQuery(db, { where: { metadata: { ... } } });
85+
* // SELECT COUNT(*) as count FROM fractions_view
86+
* // WHERE EXISTS (
87+
* // SELECT * FROM claims
88+
* // LEFT JOIN metadata ON metadata.id = fractions_view.claims_id
89+
* // WHERE claims.id = fractions_view.claims_id
90+
* // )
91+
* ```
92+
*/
3393
buildCountQuery(db: Kysely<CachingDatabase>, args?: GetFractionsArgs) {
3494
if (!args) {
3595
return db.selectFrom(this.tableName).select((eb) => {

0 commit comments

Comments
 (0)