Skip to content

Commit b7c26f8

Browse files
authored
fix(ts-client): schema import in generated types (#732)
1 parent 4318cac commit b7c26f8

File tree

9 files changed

+20
-8
lines changed

9 files changed

+20
-8
lines changed

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@
1313
"default": "./build/entrypoints/main.js"
1414
}
1515
},
16-
"./alpha": {
16+
"./alpha/client": {
1717
"import": {
18-
"types": "./build/entrypoints/alpha.d.ts",
19-
"default": "./build/entrypoints/alpha.js"
18+
"types": "./build/entrypoints/alpha/client.d.ts",
19+
"default": "./build/entrypoints/alpha/client.js"
20+
}
21+
},
22+
"./alpha/types": {
23+
"import": {
24+
"types": "./build/entrypoints/alpha/types.d.ts",
25+
"default": "./build/entrypoints/alpha/types.js"
2026
}
2127
}
2228
},
File renamed without changes.

src/Schema/__.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * as Schema from './__Schema.js'
1+
export * as Schema from './_.js'
22
export { Args, As, Field } from './Field/__.js'
33
export { __typename, List, Nullable } from './Field/Type.js'
44
export { Index } from './Index.js'

src/entrypoints/alpha.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/entrypoints/alpha/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../../client.js'

src/entrypoints/alpha/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../../Schema/__.js'

src/generator/generator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ const scalarTypeMap: Record<string, 'string' | 'number' | 'boolean'> = {
319319
// high level
320320

321321
interface Input {
322+
schemaModulePath?: string
322323
schemaSource: string
323324
options?: {
324325
TSDoc?: {
@@ -362,7 +363,9 @@ export const generateCode = (input: Input) => {
362363

363364
let code = ``
364365

365-
code += `import type * as _ from '../../../src/Schema/__.js'\n\n`
366+
const schemaModulePath = input.schemaModulePath ?? `graphql-client/alpha/schema`
367+
368+
code += `import type * as _ from ${Code.quote(schemaModulePath)}\n\n`
366369

367370
code += Code.export$(
368371
Code.namespace(
@@ -443,9 +446,10 @@ export const generateCode = (input: Input) => {
443446
export const generateFile = async (params: {
444447
schemaPath: string
445448
typeScriptPath: string
449+
schemaModulePath?: string
446450
}) => {
447451
// todo use @dprint/formatter
448452
const schemaSource = await fs.readFile(params.schemaPath, `utf8`)
449-
const code = generateCode({ schemaSource })
453+
const code = generateCode({ schemaSource, ...params })
450454
await fs.writeFile(params.typeScriptPath, code, { encoding: `utf8` })
451455
}

tests/builder/__snapshots__/generate.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`generates types from GraphQL SDL file 1`] = `
4-
"import type * as _ from '../../../src/Schema/__.js'
4+
"import type * as _ from "../../../src/Schema/__.js"
55
66
export namespace $ {
77
export interface Index {

tests/builder/generate.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { generateFile } from '../../src/generator/generator.js'
44

55
test(`generates types from GraphQL SDL file`, async () => {
66
await generateFile({
7+
schemaModulePath: `../../../src/Schema/__.js`,
78
schemaPath: `./tests/builder/_/schema.graphql`,
89
typeScriptPath: `./tests/builder/_/schema.ts`,
910
})

0 commit comments

Comments
 (0)