Skip to content

Commit b60267c

Browse files
committed
ts:gql-codegen:fix
1 parent 0ec8a16 commit b60267c

2 files changed

Lines changed: 37 additions & 20 deletions

File tree

typescript/gql-codegen/src/actors/ts/graphql/generators/server/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export function generateNonCallableFieldSpec(
88
scalars: string[],
99
spec: z.infer<typeof objectNonCallableFieldSpecSchema> |
1010
z.infer<typeof inputFieldSpecSchema>,
11-
) {
11+
): ts.TypeNode {
1212
switch (spec._type) {
1313
case 'array':
1414
return ts.factory.createArrayTypeNode(
15-
generateTypeReferenceNode(scalars, spec.type.name)
15+
generateNonCallableFieldSpec(scalars, spec.type)
1616
)
1717
case 'literal':
1818
return generateTypeReferenceNode(scalars, spec.type.name)

typescript/gql-codegen/src/schema/utils.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PathOrFileDescriptor, readFileSync } from 'fs';
33
import {
44
objectFieldSchema,
55
objectFieldSpecSchema,
6+
objectLiteralSpecSchema,
67
objectNonCallableFieldSpecSchema,
78
objectSchema,
89
ServerSchema,
@@ -19,7 +20,7 @@ import {
1920
fragmentSpecSchema,
2021
objectConditionalSpreadSelection
2122
} from './client/fragment.js';
22-
import { inputFieldSchema } from './shared.js';
23+
import { inputFieldSchema, inputFieldSpecSchema } from './shared.js';
2324
import { argument } from './client/argument.js';
2425
import { operationSchema } from './client/operation.js';
2526

@@ -124,8 +125,8 @@ export function buildFragmentFromUnion(
124125
};
125126
};
126127

127-
function extractFieldFromNonCallableFieldSpec(
128-
spec: z.infer<typeof objectNonCallableFieldSpecSchema>
128+
function extractFieldFromObjectLiteralFieldSpec(
129+
spec: z.infer<typeof objectLiteralSpecSchema>
129130
): string {
130131
switch (spec.type._type) {
131132
case 'Enum': return ''
@@ -137,16 +138,29 @@ function extractFieldFromNonCallableFieldSpec(
137138
};
138139
};
139140

141+
142+
function extractFieldFromNonCallableFieldSpec(
143+
spec: z.infer<typeof objectNonCallableFieldSpecSchema>
144+
): string {
145+
switch (spec._type) {
146+
case 'literal': return extractFieldFromObjectLiteralFieldSpec(spec)
147+
case 'array': return extractFieldFromNonCallableFieldSpec(spec.type)
148+
}
149+
};
150+
151+
function buildInputFieldSpec(
152+
spec: z.infer<typeof inputFieldSpecSchema>
153+
): string {
154+
switch (spec._type) {
155+
case 'literal': return spec.type._type
156+
case 'array':
157+
return '[' + buildInputFieldSpec(spec.type) +
158+
spec.nullable ? '' : '!' + ']'
159+
}
160+
}
161+
140162
function buildInputField(field: z.infer<typeof inputFieldSchema>): string {
141-
let str = ''
142-
if (field.spec._type === 'array') {
143-
str += '['
144-
};
145-
str += field.spec.type.name
146-
if (field.spec._type === 'array') {
147-
if (!field.spec.nullable) str += '!';
148-
str += ']'
149-
};
163+
let str = buildInputFieldSpec(field.spec)
150164
if (!field.nullable) {
151165
str += '!';
152166
};
@@ -217,23 +231,26 @@ export function buildSelectionFromFieldSpec(
217231
spec: z.infer<typeof objectFieldSpecSchema>
218232
): z.infer<typeof fragmentSpecSchema> | null {
219233
const type = extractTypeFromObjectFieldSpec(spec)
220-
if (type._type === 'Enum' || type._type === 'Scalar') return null
221-
switch (type._type) {
234+
if (type._type === 'array') return buildSelectionFromFieldSpec(type.type)
235+
if (type._type !== 'literal') throw new Error('Unexpected condition')
236+
switch (type.type._type) {
237+
case 'Enum':
238+
case 'Scalar': return null
222239
case 'ObjectType':
223240
case 'InterfaceType':
224241
return {
225242
_type: 'ObjectFragmentSpec',
226-
name: type.name,
243+
name: type.type.name,
227244
selections: [
228-
{ _type: 'SpreadSelection', fragment: type.name }
245+
{ _type: 'SpreadSelection', fragment: type.type.name }
229246
]
230247
}
231248
case 'Union':
232249
return {
233250
_type: 'UnionFragmentSpec',
234-
name: type.name,
251+
name: type.type.name,
235252
selections: [
236-
{ _type: 'SpreadSelection', fragment: type.name }
253+
{ _type: 'SpreadSelection', fragment: type.type.name }
237254
]
238255
}
239256
}

0 commit comments

Comments
 (0)