Skip to content

Commit 7ad9aef

Browse files
committed
♻️ (helpers): remove redundant comments and simplify helper generation logic
1 parent 3306ce1 commit 7ad9aef

File tree

13 files changed

+46
-197
lines changed

13 files changed

+46
-197
lines changed

examples/advanced-graphql/schema-helpers.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export interface ConnectionConfig {
4646
}
4747

4848
export class ConnectionBuilder {
49-
/**
50-
* Build connection configuration without executing queries
51-
*/
5249
static buildConfig(args: {
5350
pagination: PaginationArgs
5451
where?: any
@@ -71,28 +68,23 @@ export class ConnectionBuilder {
7168
} = args
7269
const { first, after, last, before } = pagination
7370

74-
// Calculate pagination parameters
7571
let take = first || last || 10
7672
if (last) take = -take
7773

78-
// For composite key models, we skip cursor-based pagination
7974
const cursor = (hasIdField && (after || before))
8075
? { [cursorField]: (after || before)! }
8176
: undefined
8277
const skip = cursor ? 1 : 0
8378

84-
// Build include from GraphQL selection if info is provided
8579
const finalInclude = info ? buildPrismaInclude(info, relationFields) : include
8680

87-
// Prepare query options
8881
const findManyOptions: any = {
8982
take: Math.abs(take) + 1, // Get one extra to check for next page
9083
where,
9184
orderBy,
9285
include: finalInclude,
9386
}
9487

95-
// Only add cursor and skip for models with ID field
9688
if (hasIdField && cursor) {
9789
findManyOptions.cursor = cursor
9890
findManyOptions.skip = skip
@@ -113,31 +105,24 @@ export class ConnectionBuilder {
113105
}
114106
}
115107

116-
/**
117-
* Process query results into connection format
118-
*/
119108
static processResults<T>(
120109
items: T[],
121110
totalCount: number,
122111
paginationInfo: ConnectionConfig['paginationInfo']
123112
): ConnectionResult<T> {
124113
const { first, last, cursorField, hasIdField } = paginationInfo
125114

126-
// Determine pagination info
127115
const hasNextPage = first ? items.length > first : false
128116
const hasPreviousPage = last ? items.length > Math.abs(last) : false
129117

130-
// Remove extra item if present
131118
const resultItems = hasNextPage || hasPreviousPage ? items.slice(0, -1) : items
132119

133-
// Build edges - use composite key for cursor if no ID field
134120
const edges = resultItems.map((item: any, index: number) => {
135121
let cursor: string
136122
if (hasIdField && item[cursorField]) {
137123
cursor = item[cursorField]
138124
} else {
139-
// For composite key models, create a cursor from available fields or use index
140-
cursor = item.postId && item.categoryId
125+
cursor = item.postId && item.categoryId
141126
? `${item.postId}:${item.categoryId}`
142127
: String(index)
143128
}
@@ -264,10 +249,6 @@ export class ConnectionBuilder {
264249
}
265250

266251
export class FilterBuilder {
267-
/**
268-
* Build Prisma where clause from GraphQL filter input dynamically
269-
* This approach uses runtime reflection to map filter operations
270-
*/
271252
static buildFilter(filter: any): any {
272253
if (!filter || typeof filter !== 'object') return {}
273254

@@ -279,10 +260,8 @@ export class FilterBuilder {
279260
} else if (field === 'OR' && Array.isArray(value)) {
280261
where.OR = value.map((f: any) => this.buildFilter(f))
281262
} else if (value && typeof value === 'object') {
282-
// Map filter operations dynamically
283263
const fieldWhere: any = {}
284264

285-
// Copy all valid operations from the filter value
286265
for (const [operation, operationValue] of Object.entries(value)) {
287266
if (operationValue !== undefined && operationValue !== null) {
288267
fieldWhere[operation] = operationValue
@@ -314,19 +293,14 @@ export class FilterBuilder {
314293
}
315294

316295
export class SortBuilder {
317-
/**
318-
* Build Prisma orderBy clause from GraphQL sort input dynamically
319-
* This approach uses runtime reflection to map sort fields
320-
*/
321296
static buildSort(sort: any, fallbackSort: any = { id: 'asc' }): any {
322297
if (!sort || typeof sort !== 'object') return fallbackSort
323298

324299
const orderBy: any = {}
325300

326301
for (const [field, direction] of Object.entries(sort)) {
327302
if (direction && typeof direction === 'string') {
328-
// Convert enum values to lowercase for Prisma
329-
orderBy[field] = direction.toLowerCase()
303+
orderBy[field] = direction.toLowerCase()
330304
}
331305
}
332306

@@ -335,22 +309,19 @@ export class SortBuilder {
335309

336310

337311
static buildProductSort(sort?: ProductSortInput): any {
338-
// For models without id field (like composite key models), don't use id as fallback
339312
const fallbackSort = true ? { id: 'asc' } : {}
340313
return this.buildSort(sort, fallbackSort)
341314
}
342315

343316
static buildReviewSort(sort?: ReviewSortInput): any {
344-
// For models without id field (like composite key models), don't use id as fallback
345317
const fallbackSort = true ? { id: 'asc' } : {}
346318
return this.buildSort(sort, fallbackSort)
347319
}
348320

349321

350322
}
351323

352-
// Simplified field selection utilities (GraphQL-import-free)
353-
// Uses static includes instead of dynamic GraphQL field parsing
324+
354325

355326
export interface ResolveTree {
356327
name: string
@@ -363,10 +334,7 @@ export interface FieldsByTypeName {
363334
[str: string]: { [str: string]: ResolveTree }
364335
}
365336

366-
// Simplified version that doesn't require GraphQL imports
367337
export function buildPrismaInclude(_resolveInfo: any, relations: string[] = []): any {
368-
// For now, return a simple include object based on available relations
369-
// This avoids GraphQL module conflicts while maintaining basic functionality
370338
const include: any = {}
371339

372340
relations.forEach(relation => {

examples/type-graphql/schema-helpers.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export interface ConnectionConfig {
4646
}
4747

4848
export class ConnectionBuilder {
49-
/**
50-
* Build connection configuration without executing queries
51-
*/
5249
static buildConfig(args: {
5350
pagination: PaginationArgs
5451
where?: any
@@ -71,28 +68,23 @@ export class ConnectionBuilder {
7168
} = args
7269
const { first, after, last, before } = pagination
7370

74-
// Calculate pagination parameters
7571
let take = first || last || 10
7672
if (last) take = -take
7773

78-
// For composite key models, we skip cursor-based pagination
7974
const cursor = (hasIdField && (after || before))
8075
? { [cursorField]: (after || before)! }
8176
: undefined
8277
const skip = cursor ? 1 : 0
8378

84-
// Build include from GraphQL selection if info is provided
8579
const finalInclude = info ? buildPrismaInclude(info, relationFields) : include
8680

87-
// Prepare query options
8881
const findManyOptions: any = {
8982
take: Math.abs(take) + 1, // Get one extra to check for next page
9083
where,
9184
orderBy,
9285
include: finalInclude,
9386
}
9487

95-
// Only add cursor and skip for models with ID field
9688
if (hasIdField && cursor) {
9789
findManyOptions.cursor = cursor
9890
findManyOptions.skip = skip
@@ -113,31 +105,24 @@ export class ConnectionBuilder {
113105
}
114106
}
115107

116-
/**
117-
* Process query results into connection format
118-
*/
119108
static processResults<T>(
120109
items: T[],
121110
totalCount: number,
122111
paginationInfo: ConnectionConfig['paginationInfo']
123112
): ConnectionResult<T> {
124113
const { first, last, cursorField, hasIdField } = paginationInfo
125114

126-
// Determine pagination info
127115
const hasNextPage = first ? items.length > first : false
128116
const hasPreviousPage = last ? items.length > Math.abs(last) : false
129117

130-
// Remove extra item if present
131118
const resultItems = hasNextPage || hasPreviousPage ? items.slice(0, -1) : items
132119

133-
// Build edges - use composite key for cursor if no ID field
134120
const edges = resultItems.map((item: any, index: number) => {
135121
let cursor: string
136122
if (hasIdField && item[cursorField]) {
137123
cursor = item[cursorField]
138124
} else {
139-
// For composite key models, create a cursor from available fields or use index
140-
cursor = item.postId && item.categoryId
125+
cursor = item.postId && item.categoryId
141126
? `${item.postId}:${item.categoryId}`
142127
: String(index)
143128
}
@@ -289,10 +274,6 @@ export class ConnectionBuilder {
289274
}
290275

291276
export class FilterBuilder {
292-
/**
293-
* Build Prisma where clause from GraphQL filter input dynamically
294-
* This approach uses runtime reflection to map filter operations
295-
*/
296277
static buildFilter(filter: any): any {
297278
if (!filter || typeof filter !== 'object') return {}
298279

@@ -304,10 +285,8 @@ export class FilterBuilder {
304285
} else if (field === 'OR' && Array.isArray(value)) {
305286
where.OR = value.map((f: any) => this.buildFilter(f))
306287
} else if (value && typeof value === 'object') {
307-
// Map filter operations dynamically
308288
const fieldWhere: any = {}
309289

310-
// Copy all valid operations from the filter value
311290
for (const [operation, operationValue] of Object.entries(value)) {
312291
if (operationValue !== undefined && operationValue !== null) {
313292
fieldWhere[operation] = operationValue
@@ -346,19 +325,14 @@ export class FilterBuilder {
346325
}
347326

348327
export class SortBuilder {
349-
/**
350-
* Build Prisma orderBy clause from GraphQL sort input dynamically
351-
* This approach uses runtime reflection to map sort fields
352-
*/
353328
static buildSort(sort: any, fallbackSort: any = { id: 'asc' }): any {
354329
if (!sort || typeof sort !== 'object') return fallbackSort
355330

356331
const orderBy: any = {}
357332

358333
for (const [field, direction] of Object.entries(sort)) {
359334
if (direction && typeof direction === 'string') {
360-
// Convert enum values to lowercase for Prisma
361-
orderBy[field] = direction.toLowerCase()
335+
orderBy[field] = direction.toLowerCase()
362336
}
363337
}
364338

@@ -367,28 +341,24 @@ export class SortBuilder {
367341

368342

369343
static buildUserSort(sort?: UserSortInput): any {
370-
// For models without id field (like composite key models), don't use id as fallback
371344
const fallbackSort = true ? { id: 'asc' } : {}
372345
return this.buildSort(sort, fallbackSort)
373346
}
374347

375348
static buildPostSort(sort?: PostSortInput): any {
376-
// For models without id field (like composite key models), don't use id as fallback
377349
const fallbackSort = true ? { id: 'asc' } : {}
378350
return this.buildSort(sort, fallbackSort)
379351
}
380352

381353

382354

383355
static buildCommentSort(sort?: CommentSortInput): any {
384-
// For models without id field (like composite key models), don't use id as fallback
385356
const fallbackSort = true ? { id: 'asc' } : {}
386357
return this.buildSort(sort, fallbackSort)
387358
}
388359
}
389360

390-
// Simplified field selection utilities (GraphQL-import-free)
391-
// Uses static includes instead of dynamic GraphQL field parsing
361+
392362

393363
export interface ResolveTree {
394364
name: string
@@ -401,10 +371,7 @@ export interface FieldsByTypeName {
401371
[str: string]: { [str: string]: ResolveTree }
402372
}
403373

404-
// Simplified version that doesn't require GraphQL imports
405374
export function buildPrismaInclude(_resolveInfo: any, relations: string[] = []): any {
406-
// For now, return a simple include object based on available relations
407-
// This avoids GraphQL module conflicts while maintaining basic functionality
408375
const include: any = {}
409376

410377
relations.forEach(relation => {

src/generators/strategies/graphql-helper-strategy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { TypeScriptHelperStrategy } from './typescript-helper-strategy'
33

44
export class GraphQLHelperStrategy extends TypeScriptHelperStrategy {
55
override generateHelpers(helpers: ModelHelper[], context: HelperGenerationContext): string[] {
6-
// Always generate TypeScript helpers, even for GraphQL output
76
return super.generateHelpers(helpers, context)
87
}
9-
}
8+
}

src/generators/strategies/typescript-helper-strategy.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,6 @@ export interface ConnectionConfig {
247247
}
248248

249249
private getSchemaImportPath(context: HelperGenerationContext): string {
250-
// Determine the correct import path based on where the schema.ts file is generated
251-
// Default to './schema' but can be customized based on output configuration
252250
return './schema'
253251
}
254-
}
252+
}

src/generators/unified/unified-helper-generator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class UnifiedHelperGenerator extends UnifiedGeneratorBase {
5151
return this.models
5252
.filter((model) => !this.shouldSkipModel(model))
5353
.map((model) => {
54-
// Get the GraphQL name from the attribute processor
5554
const graphqlName = this.attributeProcessor.model(model).name()
5655
return {
5756
modelName: graphqlName,

src/orchestrator/helper-file-writer.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ export class HelperFileWriter {
88
async writeHelperFiles(_outputFormat: OutputFormat, outputPath: string, helperCode?: string): Promise<string[]> {
99
const files: string[] = []
1010

11-
// Write helper code if provided (always as TypeScript)
1211
if (helperCode) {
1312
const helperPath = this.resolveHelperPath(outputPath)
14-
// Combine GraphQL field selection utility with helper code in one file
1513
const completeHelperCode = this.combineHelperCode(helperCode)
1614
await this.fileWriter.write(completeHelperCode, helperPath, 'Helper utilities')
1715
files.push(helperPath)
@@ -23,14 +21,11 @@ export class HelperFileWriter {
2321
private resolveHelperPath(basePath: string): string {
2422
const baseDir = path.dirname(basePath)
2523
const baseName = path.basename(basePath, path.extname(basePath))
26-
27-
// Always generate TypeScript helpers
24+
2825
return path.join(baseDir, `${baseName}-helpers.ts`)
2926
}
3027

3128
private combineHelperCode(helperCode: string): string {
32-
// No longer combining with field selection code - it's handled by templates
3329
return helperCode
3430
}
35-
36-
}
31+
}

src/orchestrator/output-writer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class OutputWriter {
1010

1111
async write(result: UnifiedGenerationResult, outputPath: string): Promise<string> {
1212
const finalOutputPath = this.resolveOutputPath(result.outputFormat, outputPath)
13-
13+
1414
if (result.outputFormat === OutputFormat.TYPE_GRAPHQL) {
1515
if (!result.code) {
1616
throw new Error('TypeGraphQL code is required but missing')
@@ -23,7 +23,6 @@ export class OutputWriter {
2323
await this.fileWriter.write(result.sdl, finalOutputPath, 'GraphQL schema')
2424
}
2525

26-
// Write helper files if generated
2726
if (result.helperCode) {
2827
await this.helperFileWriter.writeHelperFiles(result.outputFormat, outputPath, result.helperCode)
2928
}
@@ -33,11 +32,8 @@ export class OutputWriter {
3332

3433
private resolveOutputPath(outputFormat: OutputFormat, basePath: string): string {
3534
if (outputFormat === OutputFormat.TYPE_GRAPHQL) {
36-
return path.join(
37-
path.dirname(basePath),
38-
path.basename(basePath, path.extname(basePath)) + '.ts'
39-
)
35+
return path.join(path.dirname(basePath), path.basename(basePath, path.extname(basePath)) + '.ts')
4036
}
4137
return basePath
4238
}
43-
}
39+
}

0 commit comments

Comments
 (0)