-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsalesResolver.ts
More file actions
131 lines (124 loc) · 3.38 KB
/
salesResolver.ts
File metadata and controls
131 lines (124 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { inject, injectable } from "tsyringe";
import { Args, FieldResolver, Query, Resolver, Root } from "type-graphql";
import { HypercertsService } from "../../database/entities/HypercertsEntityService.js";
import { SalesService } from "../../database/entities/SalesEntityService.js";
import { GetSalesArgs } from "../../../graphql/schemas/args/salesArgs.js";
import {
Sale,
GetSalesResponse,
} from "../../../graphql/schemas/typeDefs/salesTypeDefs.js";
/**
* Resolver for handling sales-related GraphQL queries and field resolvers.
* This resolver provides functionality to:
* 1. Query sales with filtering and pagination
* 2. Resolve the associated hypercert for a sale
*/
@injectable()
@Resolver(() => Sale)
class SalesResolver {
constructor(
@inject(SalesService)
private salesService: SalesService,
@inject(HypercertsService)
private hypercertsService: HypercertsService,
) {}
/**
* Query resolver for fetching sales with optional filtering and pagination.
*
* @param args - Query arguments including where conditions, sorting, and pagination
* @returns A promise resolving to:
* - Object containing sales data and count if successful
* - null if an error occurs during retrieval
*
* @example
* ```graphql
* query {
* sales(
* where: { hypercert_id: { eq: "123" } }
* first: 10
* offset: 0
* ) {
* data {
* id
* buyer
* seller
* hypercert {
* id
* }
* }
* count
* }
* }
* ```
*/
@Query(() => GetSalesResponse)
async sales(@Args() args: GetSalesArgs) {
try {
return await this.salesService.getSales(args);
} catch (e) {
console.error(
`[SalesResolver::sales] Error fetching sales: ${(e as Error).message}`,
);
return null;
}
}
/**
* Field resolver for the hypercert associated with a sale.
* This resolver is called automatically when the hypercert field is requested in a query.
*
* @param sale - The sale for which to resolve the associated hypercert
* @returns A promise resolving to:
* - The associated hypercert if found
* - null if:
* - No hypercert_id is available
* - The hypercert is not found
* - An error occurs during retrieval
*
* @example
* ```graphql
* query {
* sales {
* data {
* id
* hypercert {
* id
* hypercert_id
* }
* }
* }
* }
* ```
*/
@FieldResolver({ nullable: true })
async hypercert(@Root() sale: Sale) {
if (!sale.hypercert_id) {
console.warn(`[SalesResolver::hypercert_id] Missing hypercert_id`);
return null;
}
try {
const [hypercert, metadata] = await Promise.all([
this.hypercertsService.getHypercert({
where: {
hypercert_id: { eq: sale.hypercert_id },
},
}),
this.hypercertsService.getHypercertMetadata({
hypercert_id: sale.hypercert_id,
}),
]);
if (!hypercert) {
return null;
}
return {
...hypercert,
metadata: metadata || null,
};
} catch (e) {
console.error(
`[SalesResolver::hypercert] Error fetching hypercert: ${(e as Error).message}`,
);
return null;
}
}
}
export { SalesResolver };