Skip to content

Commit

Permalink
feat(ts-client): format generated code with dprint
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Mar 30, 2024
1 parent c0d85b6 commit 0e150e4
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 176 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
},
"dependencies": {
"@dprint/formatter": "^0.2.1",
"@dprint/typescript": "^0.89.3",
"@graphql-typed-document-node/core": "^3.2.0",
"@molt/command": "^0.9.0",
"dprint": "^0.45.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
`Directory path for where to output the generated TypeScript files.`,
),
)
.parameter(`format`, z.boolean().describe(`Format the generated files using dprint.`).default(true))
.settings({
parameters: {
environment: false,
Expand All @@ -22,4 +23,5 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
await generateFiles({
outputDirPath: args.output,
schemaPath: args.schema,
format: args.format,
})
60 changes: 37 additions & 23 deletions src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ interface Input {
scalarsModulePath?: string
schemaSource: string
options?: {
formatter?: Formatter
TSDoc?: {
noDocPolicy?: 'message' | 'ignore'
}
Expand Down Expand Up @@ -393,11 +394,6 @@ export const generateCode = (input: Input) => {
objects: Code.objectFromEntries(
typeMapByKind.GraphQLObjectType.map(_ => [_.name, Code.propertyAccess(`Object`, _.name)]),
),
// unionMemberNames: Code.objectFromEntries(
// typeMapByKind.GraphQLUnionType.map(
// (_) => [_.name, Code.unionItems(_.getTypes().map(_ => Code.quote(_.name)))],
// ),
// ),
unions: {
type: Code.objectFrom(
{
Expand All @@ -419,7 +415,6 @@ export const generateCode = (input: Input) => {
),
),
)
// console.log(typeMapByKind.GraphQLScalarType)

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

let scalarsCode = ``

scalarsCode += `import type * as Scalar from ${Code.quote(scalarsModulePath)}
scalarsCode += `
import * as Scalar from ${Code.quote(scalarsModulePath)}
declare global {
interface SchemaCustomScalars {
Date: Date
}
}
declare global {
interface SchemaCustomScalars {
Date: Date
}
}
${
${
typeMapByKind.GraphQLCustomScalarType
.map((_) => {
return `
export const ${_.name} = Scalar.scalar('${_.name}', Scalar.nativeScalarConstructors.String)
export type ${_.name} = typeof ${_.name}
export const ${_.name} = Scalar.scalar('${_.name}', Scalar.nativeScalarConstructors.String)
export type ${_.name} = typeof ${_.name}
`
}).join(`\n`)
}
export * from ${Code.quote(scalarsModulePath)}
`

export * from ${Code.quote(scalarsModulePath)}
`
const defaultDprintConfig = {
quoteStyle: `preferSingle`,
semiColons: `asi`,
}

return {
scalars: scalarsCode,
schema: schemaCode,
scalars: input.options?.formatter?.formatText(`memory.ts`, scalarsCode, defaultDprintConfig)
?? scalarsCode,
schema: input.options?.formatter?.formatText(`memory.ts`, schemaCode, defaultDprintConfig) ?? schemaCode,
}
}

import type { Formatter } from '@dprint/formatter'
import { createFromBuffer } from '@dprint/formatter'
import { getPath } from '@dprint/typescript'
export const generateFiles = async (params: {
schemaPath: string
outputDirPath: string
schemaModulePath?: string
scalarsModulePath?: string
/**
* @defaultValue `true`
*/
format?: boolean
}) => {
// todo use @dprint/formatter
const schemaSource = await fs.readFile(params.schemaPath, `utf8`)
const code = generateCode({ schemaSource, ...params })
const options = (params.format ?? true)
? {
formatter: createFromBuffer(await fs.readFile(getPath())),
}
: undefined
const code = generateCode({
schemaSource,
...params,
options,
})
await fs.mkdir(params.outputDirPath, { recursive: true })
await fs.writeFile(`${params.outputDirPath}/Schema.ts`, code.schema, { encoding: `utf8` })
await fs.writeFile(`${params.outputDirPath}/Scalar.ts`, code.scalars, { encoding: `utf8` })
Expand Down
2 changes: 1 addition & 1 deletion tests/ts/_/schema/Scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare global {
}
}

export const Date = Scalar.scalar(`Date`, Scalar.nativeScalarConstructors.String)
export const Date = Scalar.scalar('Date', Scalar.nativeScalarConstructors.String)
export type Date = typeof Date

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

0 comments on commit 0e150e4

Please sign in to comment.