Skip to content

Commit 0e150e4

Browse files
committed
feat(ts-client): format generated code with dprint
1 parent c0d85b6 commit 0e150e4

File tree

6 files changed

+222
-176
lines changed

6 files changed

+222
-176
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
},
8080
"dependencies": {
8181
"@dprint/formatter": "^0.2.1",
82+
"@dprint/typescript": "^0.89.3",
8283
"@graphql-typed-document-node/core": "^3.2.0",
8384
"@molt/command": "^0.9.0",
8485
"dprint": "^0.45.0",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/generate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
1212
`Directory path for where to output the generated TypeScript files.`,
1313
),
1414
)
15+
.parameter(`format`, z.boolean().describe(`Format the generated files using dprint.`).default(true))
1516
.settings({
1617
parameters: {
1718
environment: false,
@@ -22,4 +23,5 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
2223
await generateFiles({
2324
outputDirPath: args.output,
2425
schemaPath: args.schema,
26+
format: args.format,
2527
})

src/generator/generator.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ interface Input {
327327
scalarsModulePath?: string
328328
schemaSource: string
329329
options?: {
330+
formatter?: Formatter
330331
TSDoc?: {
331332
noDocPolicy?: 'message' | 'ignore'
332333
}
@@ -393,11 +394,6 @@ export const generateCode = (input: Input) => {
393394
objects: Code.objectFromEntries(
394395
typeMapByKind.GraphQLObjectType.map(_ => [_.name, Code.propertyAccess(`Object`, _.name)]),
395396
),
396-
// unionMemberNames: Code.objectFromEntries(
397-
// typeMapByKind.GraphQLUnionType.map(
398-
// (_) => [_.name, Code.unionItems(_.getTypes().map(_ => Code.quote(_.name)))],
399-
// ),
400-
// ),
401397
unions: {
402398
type: Code.objectFrom(
403399
{
@@ -419,7 +415,6 @@ export const generateCode = (input: Input) => {
419415
),
420416
),
421417
)
422-
// console.log(typeMapByKind.GraphQLScalarType)
423418

424419
for (const [name, types] of entries(typeMapByKind)) {
425420
if (name === `GraphQLScalarType`) continue
@@ -441,45 +436,64 @@ export const generateCode = (input: Input) => {
441436

442437
let scalarsCode = ``
443438

444-
scalarsCode += `import type * as Scalar from ${Code.quote(scalarsModulePath)}
439+
scalarsCode += `
440+
import * as Scalar from ${Code.quote(scalarsModulePath)}
445441
446-
declare global {
447-
interface SchemaCustomScalars {
448-
Date: Date
449-
}
450-
}
442+
declare global {
443+
interface SchemaCustomScalars {
444+
Date: Date
445+
}
446+
}
451447
452-
${
448+
${
453449
typeMapByKind.GraphQLCustomScalarType
454450
.map((_) => {
455451
return `
456-
export const ${_.name} = Scalar.scalar('${_.name}', Scalar.nativeScalarConstructors.String)
457-
export type ${_.name} = typeof ${_.name}
452+
export const ${_.name} = Scalar.scalar('${_.name}', Scalar.nativeScalarConstructors.String)
453+
export type ${_.name} = typeof ${_.name}
458454
`
459455
}).join(`\n`)
460456
}
461457
458+
export * from ${Code.quote(scalarsModulePath)}
459+
`
462460

463-
464-
465-
export * from ${Code.quote(scalarsModulePath)}
466-
`
461+
const defaultDprintConfig = {
462+
quoteStyle: `preferSingle`,
463+
semiColons: `asi`,
464+
}
467465

468466
return {
469-
scalars: scalarsCode,
470-
schema: schemaCode,
467+
scalars: input.options?.formatter?.formatText(`memory.ts`, scalarsCode, defaultDprintConfig)
468+
?? scalarsCode,
469+
schema: input.options?.formatter?.formatText(`memory.ts`, schemaCode, defaultDprintConfig) ?? schemaCode,
471470
}
472471
}
473472

473+
import type { Formatter } from '@dprint/formatter'
474+
import { createFromBuffer } from '@dprint/formatter'
475+
import { getPath } from '@dprint/typescript'
474476
export const generateFiles = async (params: {
475477
schemaPath: string
476478
outputDirPath: string
477479
schemaModulePath?: string
478480
scalarsModulePath?: string
481+
/**
482+
* @defaultValue `true`
483+
*/
484+
format?: boolean
479485
}) => {
480-
// todo use @dprint/formatter
481486
const schemaSource = await fs.readFile(params.schemaPath, `utf8`)
482-
const code = generateCode({ schemaSource, ...params })
487+
const options = (params.format ?? true)
488+
? {
489+
formatter: createFromBuffer(await fs.readFile(getPath())),
490+
}
491+
: undefined
492+
const code = generateCode({
493+
schemaSource,
494+
...params,
495+
options,
496+
})
483497
await fs.mkdir(params.outputDirPath, { recursive: true })
484498
await fs.writeFile(`${params.outputDirPath}/Schema.ts`, code.schema, { encoding: `utf8` })
485499
await fs.writeFile(`${params.outputDirPath}/Scalar.ts`, code.scalars, { encoding: `utf8` })

tests/ts/_/schema/Scalar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare global {
66
}
77
}
88

9-
export const Date = Scalar.scalar(`Date`, Scalar.nativeScalarConstructors.String)
9+
export const Date = Scalar.scalar('Date', Scalar.nativeScalarConstructors.String)
1010
export type Date = typeof Date
1111

1212
export * from '../../../../src/Schema/NamedType/Scalar/Scalar.js'

0 commit comments

Comments
 (0)