Skip to content

Commit

Permalink
fix(ts-client): generated code imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Mar 30, 2024
1 parent 34c9e25 commit aeb6087
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
12 changes: 5 additions & 7 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env node

import { Command } from '@molt/command'
import * as fs from 'node:fs/promises'
import { z } from 'zod'
import { generateCode } from '../generator/generator.js'
import { generateFiles } from '../generator/generator.js'

const args = Command.create().description(`Generate a type safe GraphQL client.`)
.parameter(`schema`, z.string().min(1).describe(`File path to where your GraphQL schema is.`))
Expand All @@ -20,8 +19,7 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
})
.parse()

const schemaSource = await fs.readFile(args.schema, `utf8`)
const code = generateCode({ schemaSource })

await fs.writeFile(`${args.output}/schema.ts`, code.schema, { encoding: `utf8` })
await fs.writeFile(`${args.output}/scalars.ts`, code.scalars, { encoding: `utf8` })
await generateFiles({
outputDirPath: args.output,
schemaPath: args.schema,
})
6 changes: 3 additions & 3 deletions src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ export const generateCode = (input: Input) => {

let schemaCode = ``

const schemaModulePath = input.schemaModulePath ?? `graphql-client/alpha/schema`
const scalarsModulePath = input.scalarsModulePath ?? `graphql-client/alpha/schema/scalars`
const schemaModulePath = input.schemaModulePath ?? `graphql-request/alpha/schema`
const scalarsModulePath = input.scalarsModulePath ?? `graphql-request/alpha/schema/scalars`

schemaCode += `import type * as _ from ${Code.quote(schemaModulePath)}\n`
schemaCode += `import type * as $Scalar from './Scalar.ts'\n`
Expand Down Expand Up @@ -471,7 +471,7 @@ export * from ${Code.quote(scalarsModulePath)}
}
}

export const generateFile = async (params: {
export const generateFiles = async (params: {
schemaPath: string
outputDirPath: string
schemaModulePath?: string
Expand Down
4 changes: 2 additions & 2 deletions tests/ts/generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readFile } from 'fs/promises'
import { expect, test } from 'vitest'
import { generateFile } from '../../src/generator/generator.js'
import { generateFiles } from '../../src/generator/generator.js'

test(`generates types from GraphQL SDL file`, async () => {
await generateFile({
await generateFiles({
schemaModulePath: `../../../../src/Schema/__.js`,
scalarsModulePath: `../../../../src/Schema/NamedType/Scalar/Scalar.js`,
schemaPath: `./tests/ts/_/schema.graphql`,
Expand Down

0 comments on commit aeb6087

Please sign in to comment.