Skip to content

Commit

Permalink
fix(ts-client): schema import in generated types (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Mar 23, 2024
1 parent 4318cac commit b7c26f8
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 8 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
"default": "./build/entrypoints/main.js"
}
},
"./alpha": {
"./alpha/client": {
"import": {
"types": "./build/entrypoints/alpha.d.ts",
"default": "./build/entrypoints/alpha.js"
"types": "./build/entrypoints/alpha/client.d.ts",
"default": "./build/entrypoints/alpha/client.js"
}
},
"./alpha/types": {
"import": {
"types": "./build/entrypoints/alpha/types.d.ts",
"default": "./build/entrypoints/alpha/types.js"
}
}
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Schema/__.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as Schema from './__Schema.js'
export * as Schema from './_.js'
export { Args, As, Field } from './Field/__.js'
export { __typename, List, Nullable } from './Field/Type.js'
export { Index } from './Index.js'
Expand Down
1 change: 0 additions & 1 deletion src/entrypoints/alpha.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/entrypoints/alpha/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../client.js'
1 change: 1 addition & 0 deletions src/entrypoints/alpha/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../Schema/__.js'
8 changes: 6 additions & 2 deletions src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ const scalarTypeMap: Record<string, 'string' | 'number' | 'boolean'> = {
// high level

interface Input {
schemaModulePath?: string
schemaSource: string
options?: {
TSDoc?: {
Expand Down Expand Up @@ -362,7 +363,9 @@ export const generateCode = (input: Input) => {

let code = ``

code += `import type * as _ from '../../../src/Schema/__.js'\n\n`
const schemaModulePath = input.schemaModulePath ?? `graphql-client/alpha/schema`

code += `import type * as _ from ${Code.quote(schemaModulePath)}\n\n`

code += Code.export$(
Code.namespace(
Expand Down Expand Up @@ -443,9 +446,10 @@ export const generateCode = (input: Input) => {
export const generateFile = async (params: {
schemaPath: string
typeScriptPath: string
schemaModulePath?: string
}) => {
// todo use @dprint/formatter
const schemaSource = await fs.readFile(params.schemaPath, `utf8`)
const code = generateCode({ schemaSource })
const code = generateCode({ schemaSource, ...params })
await fs.writeFile(params.typeScriptPath, code, { encoding: `utf8` })
}
2 changes: 1 addition & 1 deletion tests/builder/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generates types from GraphQL SDL file 1`] = `
"import type * as _ from '../../../src/Schema/__.js'
"import type * as _ from "../../../src/Schema/__.js"
export namespace $ {
export interface Index {
Expand Down
1 change: 1 addition & 0 deletions tests/builder/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateFile } from '../../src/generator/generator.js'

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

0 comments on commit b7c26f8

Please sign in to comment.