Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ts-client): schema import in generated types #732

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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