@@ -3,6 +3,7 @@ import { PathOrFileDescriptor, readFileSync } from 'fs';
33import {
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' ;
2324import { argument } from './client/argument.js' ;
2425import { 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+
140162function 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