@@ -106,7 +106,7 @@ const referenceRenderers = defineReferenceRenderers({
106106 GraphQLInterfaceType : ( _ , node ) => Code . propertyAccess ( namespaceNames . GraphQLInterfaceType , node . name ) ,
107107 GraphQLObjectType : ( _ , node ) => Code . propertyAccess ( namespaceNames . GraphQLObjectType , node . name ) ,
108108 GraphQLUnionType : ( _ , node ) => Code . propertyAccess ( namespaceNames . GraphQLUnionType , node . name ) ,
109- GraphQLScalarType : ( _ , node ) => `_. Scalar.${ node . name } ` ,
109+ GraphQLScalarType : ( _ , node ) => `$ Scalar.${ node . name } ` ,
110110} )
111111
112112const dispatchToConcreteRenderer = (
@@ -312,18 +312,19 @@ const unwrapNonNull = (
312312 return { node : nodeUnwrapped , nullable }
313313}
314314
315- const scalarTypeMap : Record < string , 'string' | 'number' | 'boolean' > = {
316- ID : `string` ,
317- Int : `number` ,
318- String : `string` ,
319- Float : `number` ,
320- Boolean : `boolean` ,
321- }
315+ // const scalarTypeMap: Record<string, 'string' | 'number' | 'boolean'> = {
316+ // ID: `string`,
317+ // Int: `number`,
318+ // String: `string`,
319+ // Float: `number`,
320+ // Boolean: `boolean`,
321+ // }
322322
323323// high level
324324
325325interface Input {
326326 schemaModulePath ?: string
327+ scalarsModulePath ?: string
327328 schemaSource : string
328329 options ?: {
329330 TSDoc ?: {
@@ -365,13 +366,16 @@ export const generateCode = (input: Input) => {
365366 ( _ ) => _ . name === `Subscription` ,
366367 )
367368
368- let code = ``
369+ let schemaCode = ``
369370
370371 const schemaModulePath = input . schemaModulePath ?? `graphql-client/alpha/schema`
372+ const scalarsModulePath = input . scalarsModulePath ?? `graphql-client/alpha/schema/scalars`
371373
372- code += `import type * as _ from ${ Code . quote ( schemaModulePath ) } \n\n`
374+ schemaCode += `import type * as _ from ${ Code . quote ( schemaModulePath ) } \n`
375+ schemaCode += `import type * as $Scalar from './Scalar.ts'\n`
376+ schemaCode += `\n\n`
373377
374- code += Code . export$ (
378+ schemaCode += Code . export$ (
375379 Code . namespace (
376380 `$` ,
377381 Code . group (
@@ -409,30 +413,21 @@ export const generateCode = (input: Input) => {
409413 } ,
410414 ) ,
411415 } ,
412- scalars : `Scalars` ,
413416 } ) ,
414417 ) ,
415418 ) ,
416- Code . export$ (
417- Code . interface$ (
418- `Scalars` ,
419- Code . objectFromEntries ( typeMapByKind . GraphQLScalarType . map ( ( _ ) => {
420- // todo strict mode where instead of falling back to "any" we throw an error
421- const type = scalarTypeMap [ _ . name ] || `string`
422- return [ _ . name , type ]
423- } ) ) ,
424- ) ,
425- ) ,
426419 ) ,
427420 ) ,
428421 )
422+ // console.log(typeMapByKind.GraphQLScalarType)
429423
430424 for ( const [ name , types ] of entries ( typeMapByKind ) ) {
431425 if ( name === `GraphQLScalarType` ) continue
426+ if ( name === `GraphQLCustomScalarType` ) continue
432427
433428 const namespaceName = name === `GraphQLRootTypes` ? `Root` : namespaceNames [ name ]
434- code += Code . commentSectionTitle ( namespaceName )
435- code += Code . export$ (
429+ schemaCode += Code . commentSectionTitle ( namespaceName )
430+ schemaCode += Code . export$ (
436431 Code . namespace (
437432 namespaceName ,
438433 types . length === 0
@@ -444,16 +439,48 @@ export const generateCode = (input: Input) => {
444439 )
445440 }
446441
447- return code
442+ let scalarsCode = ``
443+
444+ scalarsCode += `import type * as Scalar from ${ Code . quote ( scalarsModulePath ) }
445+
446+ declare global {
447+ interface SchemaCustomScalars {
448+ Date: Date
449+ }
450+ }
451+
452+ ${
453+ typeMapByKind . GraphQLCustomScalarType
454+ . map ( ( _ ) => {
455+ return `
456+ export const ${ _ . name } = Scalar.scalar('${ _ . name } ', Scalar.nativeScalarConstructors.String)
457+ export type ${ _ . name } = typeof ${ _ . name }
458+ `
459+ } ) . join ( `\n` )
460+ }
461+
462+
463+
464+
465+ export * from ${ Code . quote ( scalarsModulePath ) }
466+ `
467+
468+ return {
469+ scalars : scalarsCode ,
470+ schema : schemaCode ,
471+ }
448472}
449473
450474export const generateFile = async ( params : {
451475 schemaPath : string
452- typeScriptPath : string
476+ outputDirPath : string
453477 schemaModulePath ?: string
478+ scalarsModulePath ?: string
454479} ) => {
455480 // todo use @dprint /formatter
456481 const schemaSource = await fs . readFile ( params . schemaPath , `utf8` )
457482 const code = generateCode ( { schemaSource, ...params } )
458- await fs . writeFile ( params . typeScriptPath , code , { encoding : `utf8` } )
483+ await fs . mkdir ( params . outputDirPath , { recursive : true } )
484+ await fs . writeFile ( `${ params . outputDirPath } /Schema.ts` , code . schema , { encoding : `utf8` } )
485+ await fs . writeFile ( `${ params . outputDirPath } /Scalar.ts` , code . scalars , { encoding : `utf8` } )
459486}
0 commit comments