Skip to content

Commit a5ddf5f

Browse files
jasonkuhrtclaude
andcommitted
fix(generator): comprehensive TypeScript reserved keyword escaping
Fix all instances where generator was using hardcoded type.name instead of renderName() for TypeScript identifiers. This ensures TypeScript reserved keywords (like 'class', 'function', 'interface') are properly escaped with $ prefix when used as GraphQL type names. Fixed generators: - MethodsRoot.ts: Interface names and SelectionSets references - Select.ts: Export type definitions for all GraphQL types - SelectionSets.ts: Inline fragment property names - Schema.ts: Type alias definitions - SchemaDrivenDataMap.ts: Variable names, reference assignments, operations object, and types array This extends the fix from #1353 to cover all generator code that creates TypeScript identifiers from GraphQL type names. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 7c2eb0d commit a5ddf5f

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

src/generator/generators/MethodsRoot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ const renderRootType = createCodeGenerator<{ node: Grafaid.Schema.ObjectType }>(
4444

4545
// dprint-ignore
4646
code`
47-
export interface ${node.name}Methods<$Context extends ${$.$$Utilities}.Context> {
47+
export interface ${renderName(node)}Methods<$Context extends ${$.$$Utilities}.Context> {
4848
$batch:
4949
${$.$$Utilities}.GraffleKit.Context.Configuration.Check.Preflight<
5050
$Context,
51-
<$SelectionSet>(selectionSet: ${$.$$Utilities}.Exact<$SelectionSet, ${$.$$SelectionSets}.${node.name}<$Context['scalars']>>) =>
51+
<$SelectionSet>(selectionSet: ${$.$$Utilities}.Exact<$SelectionSet, ${$.$$SelectionSets}.${renderName(node)}<$Context['scalars']>>) =>
5252
Promise<
5353
& (null | {})
5454
& ${$.$$Utilities}.HandleOutput<

src/generator/generators/Schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const ModuleGeneratorSchema = createModuleGenerator(
5858
.map(([, type]) => type)
5959
.flat()
6060
.map((type) => {
61-
return Code.esmExport(Code.tsType(`$$${type.name}`, renderName(type)))
61+
return Code.esmExport(Code.tsType(`$$${renderName(type)}`, renderName(type)))
6262
}).join(`\n`),
6363
),
6464
)
@@ -159,12 +159,12 @@ const InputObject = createCodeGenerator<{ type: Grafaid.Schema.InputObjectType }
159159
})
160160

161161
const ScalarStandard = createCodeGenerator<{ type: Grafaid.Schema.ScalarType }>(({ code, type }) => {
162-
code(Code.esmExport(Code.tsType(type.name, `$.StandardTypes.${type.name}`)))
162+
code(Code.esmExport(Code.tsType(renderName(type), `$.StandardTypes.${type.name}`)))
163163
code``
164164
})
165165

166166
const ScalarCustom = createCodeGenerator<{ type: Grafaid.Schema.ScalarType }>(({ code, type }) => {
167-
code(Code.esmExport(Code.tsType(type.name, `$$Scalar.${renderName(type.name)}`)))
167+
code(Code.esmExport(Code.tsType(renderName(type), `$$Scalar.${renderName(type.name)}`)))
168168
code``
169169
})
170170

src/generator/generators/SchemaDrivenDataMap.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ export const ModuleGeneratorSchemaDrivenDataMap = createModuleGenerator(
6262
operations: kindMap.Root.map(type => {
6363
const operationType = rootsWithOpType.find(({ objectType }) => objectType.name === type.name)?.operationType
6464
if (!operationType) throw new Error(`Operation type not found for ${type.name}`)
65-
return [operationType, type.name] as const
65+
return [operationType, renderName(type)] as const
6666
}),
6767
directives: {},
6868
types: Code.directiveTermObject({
6969
$literal: [
70-
...kinds.map(([, _]) => _).flat().map((_) => _.name),
70+
...kinds.map(([, _]) => _).flat().map((_) => renderName(_)),
7171
// We also include the custom scalars here to facilitate encoding. Encoding has names of variables and
7272
// that need to be looked up to determine which are/have custom scalars.
7373
// ...config.schema.typeMapByKind.GraphQLScalarTypeCustom.map(_ => {
@@ -156,7 +156,7 @@ const ScalarType = createCodeGenerator<
156156
{ type: Grafaid.Schema.ScalarType }
157157
>(
158158
({ code, type }) => {
159-
code(Code.termConst(type.name, `${$.$$Scalar}.${type.name}`))
159+
code(Code.termConst(renderName(type), `${$.$$Scalar}.${type.name}`))
160160
},
161161
)
162162

@@ -185,7 +185,7 @@ const UnionType = createCodeGenerator<
185185
// So what we do is inline all the custom scalar paths of all union members knowing
186186
// that they could never conflict.
187187
code(Code.termConstTyped(
188-
type.name,
188+
renderName(type),
189189
`${$.$$Utilities}.SchemaDrivenDataMap.OutputObject`,
190190
Code.termObject({
191191
[propertyNames.f]: Code.directiveTermObject({
@@ -204,7 +204,7 @@ const InterfaceType = createCodeGenerator<
204204
({ code, type, config }) => {
205205
const implementorTypes = Grafaid.Schema.KindMap.getInterfaceImplementors(config.schema.kindMap, type)
206206
code(Code.termConstTyped(
207-
type.name,
207+
renderName(type),
208208
`${$.$$Utilities}.SchemaDrivenDataMap.OutputObject`,
209209
Code.termObject({
210210
[propertyNames.f]: Code.directiveTermObject({
@@ -290,7 +290,7 @@ const ObjectType = createCodeGenerator<
290290
Grafaid.Schema.isUnionType(outputFieldNamedType) || Grafaid.Schema.isObjectType(outputFieldNamedType)
291291
|| Grafaid.Schema.isInterfaceType(outputFieldNamedType)
292292
) {
293-
referenceAssignments.push(`${type.name}.f[\`${outputField.name}\`]!.nt = ${outputFieldNamedType.name}`)
293+
referenceAssignments.push(`${renderName(type)}.f[\`${outputField.name}\`]!.nt = ${renderName(outputFieldNamedType)}`)
294294
// dprint-ignore
295295
sddmNodeOutputField.$literal = `// ${Code.termField(propertyNames.nt, outputFieldNamedType.name)} <-- Assigned later to avoid potential circular dependency.`
296296
// // todo make kitchen sink schema have a pattern where this code path will be traversed.
@@ -304,7 +304,7 @@ const ObjectType = createCodeGenerator<
304304
}
305305

306306
code(
307-
Code.termConstTyped(type.name, `${$.$$Utilities}.SchemaDrivenDataMap.OutputObject`, Code.termObject(o)),
307+
Code.termConstTyped(renderName(type), `${$.$$Utilities}.SchemaDrivenDataMap.OutputObject`, Code.termObject(o)),
308308
)
309309
},
310310
)
@@ -368,7 +368,7 @@ const InputObjectType = createCodeGenerator<
368368
&& Grafaid.Schema.CustomScalars.isHasCustomScalarInputs(inputFieldType)
369369
) {
370370
referenceAssignments.push(
371-
`${type.name}.${propertyNames.f}![\`${inputField.name}\`]!.${propertyNames.nt} = ${inputFieldType.name}`,
371+
`${renderName(type)}.${propertyNames.f}![\`${inputField.name}\`]!.${propertyNames.nt} = ${renderName(inputFieldType)}`,
372372
)
373373
f[inputField.name]!.$literal = `// ${
374374
Code.termField(propertyNames.nt, inputFieldType.name)
@@ -377,7 +377,7 @@ const InputObjectType = createCodeGenerator<
377377
}
378378

379379
code(
380-
Code.termConstTyped(type.name, `${$.$$Utilities}.SchemaDrivenDataMap.InputObject`, Code.termObject(o)),
380+
Code.termConstTyped(renderName(type), `${$.$$Utilities}.SchemaDrivenDataMap.InputObject`, Code.termObject(o)),
381381
)
382382
},
383383
)

src/generator/generators/Select.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const ModuleGeneratorSelect = createModuleGenerator(
3838
code(
3939
...entries(config.schema.kindMap.index.Root).map(([operationType, type]) => {
4040
if (!type) return null
41-
return `export type ${type.name}<$SelectionSet extends $$SelectionSets.${
41+
return `export type ${renderName(type)}<$SelectionSet extends $$SelectionSets.${
4242
renderName(type)
4343
}> = ${$.$$Utilities}.DocumentBuilderKit.InferResult.Operation<$SelectionSet, ${iSchema}, OperationTypeNode.${
4444
enumMemberName[operationType]
@@ -47,19 +47,19 @@ export const ModuleGeneratorSelect = createModuleGenerator(
4747
)
4848
code(Tex.title2(`OutputObject`))
4949
code(...config.schema.kindMap.list.OutputObject.map((type) => {
50-
return `export type ${type.name}<$SelectionSet extends $$SelectionSets.${
50+
return `export type ${renderName(type)}<$SelectionSet extends $$SelectionSets.${
5151
renderName(type)
5252
}> = ${$.$$Utilities}.DocumentBuilderKit.InferResult.OutputObjectLike<$SelectionSet, ${iSchema}, ${iSchema}['allTypes']['${type.name}']>`
5353
}))
5454
code(Tex.title2(`Union`))
5555
code(...config.schema.kindMap.list.Union.map((type) => {
56-
return `export type ${type.name}<$SelectionSet extends $$SelectionSets.${
56+
return `export type ${renderName(type)}<$SelectionSet extends $$SelectionSets.${
5757
renderName(type)
5858
}> = ${$.$$Utilities}.DocumentBuilderKit.InferResult.Union<$SelectionSet, ${iSchema}, ${iSchema}['allTypes']['${type.name}']>`
5959
}))
6060
code(Tex.title2(`Interface`))
6161
code(...config.schema.kindMap.list.Interface.map((type) => {
62-
return `export type ${type.name}<$SelectionSet extends $$SelectionSets.${
62+
return `export type ${renderName(type)}<$SelectionSet extends $$SelectionSets.${
6363
renderName(type)
6464
}> = ${$.$$Utilities}.DocumentBuilderKit.InferResult.Interface<$SelectionSet, ${iSchema}, ${iSchema}['allTypes']['${type.name}']>`
6565
}))

src/generator/generators/SelectionSets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const ModuleGeneratorSelectionSets = createModuleGenerator(
9696
const Union = createCodeGenerator<{ type: Grafaid.Schema.UnionType }>(
9797
({ config, type, code }) => {
9898
const fragmentsInlineType = type.getTypes().map((type) =>
99-
`${DocumentBuilderKit.Select.InlineFragment.typeConditionPRefix}${type.name}?: ${
99+
`${DocumentBuilderKit.Select.InlineFragment.typeConditionPRefix}${renderName(type)}?: ${
100100
H.forwardTypeParameter$Scalars(type)
101101
}`
102102
).join(`\n`)
@@ -146,7 +146,7 @@ const Interface = createCodeGenerator<{ type: Grafaid.Schema.InterfaceType }>(
146146
const implementorTypes = Grafaid.Schema.KindMap.getInterfaceImplementors(config.schema.kindMap, type)
147147
const onTypesRendered = implementorTypes.map(type =>
148148
H.outputFieldReference(
149-
`${DocumentBuilderKit.Select.InlineFragment.typeConditionPRefix}${type.name}`,
149+
`${DocumentBuilderKit.Select.InlineFragment.typeConditionPRefix}${renderName(type)}`,
150150
renderName(type),
151151
)
152152
).join(`\n`)

0 commit comments

Comments
 (0)