Skip to content

Commit fdca0a7

Browse files
authored
feat(ts-client): index sans dollar namespace (#767)
1 parent c77de38 commit fdca0a7

11 files changed

Lines changed: 115 additions & 101 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
},
8080
"dependencies": {
8181
"@dprint/formatter": "^0.2.1",
82-
"@dprint/typescript": "^0.90.0",
82+
"@dprint/typescript": "^0.90.1",
8383
"@graphql-typed-document-node/core": "^3.2.0",
8484
"@molt/command": "^0.9.0",
8585
"dprint": "^0.45.1",
@@ -118,6 +118,6 @@
118118
"type-fest": "^4.15.0",
119119
"typescript": "^5.4.5",
120120
"typescript-eslint": "^7.6.0",
121-
"vitest": "^1.4.0"
121+
"vitest": "^1.5.0"
122122
}
123123
}

pnpm-lock.yaml

Lines changed: 34 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResultSet/ResultSet.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable @typescript-eslint/ban-types */
22

33
import { expectTypeOf, test } from 'vitest'
4-
import type { $ } from '../../tests/ts/_/schema/generated/Index.js'
4+
import type { Index } from '../../tests/ts/_/schema/generated/Index.js'
55
import type * as Schema from '../../tests/ts/_/schema/generated/SchemaBuildtime.js'
66
import type { SelectionSet } from '../SelectionSet/__.js'
77
import type { ResultSet } from './__.js'
88

9-
type I = $.Index
9+
type I = Index
1010
type RS<$selectionSet extends SelectionSet.Query<I>> = ResultSet.Query<$selectionSet, I>
1111

1212
// dprint-ignore

src/SelectionSet/SelectionSet.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { assertType, expectTypeOf, test } from 'vitest'
2-
import type { $ } from '../../tests/ts/_/schema/generated/Index.js'
2+
import type { Index } from '../../tests/ts/_/schema/generated/Index.js'
33
import type { SelectionSet } from './__.js'
44

5-
type Q = SelectionSet.Query<$.Index>
5+
type Q = SelectionSet.Query<Index>
66

77
test(`ParseAliasExpression`, () => {
88
expectTypeOf<SelectionSet.ParseAliasExpression<'a_as_b'>>().toEqualTypeOf<SelectionSet.Alias<'a', 'b'>>()

src/SelectionSet/toGraphQLDocumentString.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { parse, print } from 'graphql'
22
import { describe, expect, test } from 'vitest'
3-
import type { $ } from '../../tests/ts/_/schema/generated/Index.js'
3+
import type { Index } from '../../tests/ts/_/schema/generated/Index.js'
44
import type { SelectionSet } from './__.js'
55
import { toGraphQLDocumentString } from './toGraphQLDocumentString.js'
66

7-
type Q = SelectionSet.Query<$.Index>
7+
// eslint-disable-next-line
8+
// @ts-ignore
9+
type Q = SelectionSet.Query<Index>
810
const s = (selectionSet: Q) => selectionSet
911
const prepareResult = (ss: Q) => {
1012
const graphqlDocumentString = toGraphQLDocumentString(ss as any)

src/SelectionSet/toGraphQLDocumentString.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ type Args_ = string | boolean | null | number | Args
1717

1818
type Indicator = 0 | 1 | boolean
1919

20-
export type GraphQLDocumentObject = {
21-
[k: string]: Indicator | SS
22-
}
20+
export type GraphQLDocumentObject = Record<string, Indicator | SS>
2321

2422
type SS = {
2523
[k: string]: Indicator | SS

src/client.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable */
22
import { beforeEach, describe, expect, test } from 'vitest'
33
import { setupMockServer } from '../tests/raw/__helpers.js'
4-
import type { $ } from '../tests/ts/_/schema/generated/Index.js'
4+
import type { Index } from '../tests/ts/_/schema/generated/Index.js'
55
import { $Index as schemaIndex } from '../tests/ts/_/schema/generated/SchemaRuntime.js'
66
import { create } from './client.js'
77

88
const ctx = setupMockServer()
99
const data = { fooBarUnion: { int: 1 } }
1010

1111
// @ts-ignore infinite depth
12-
const client = () => create<$.Index>({ url: ctx.url, schemaIndex })
12+
const client = () => create<Index>({ url: ctx.url, schemaIndex })
1313

1414
test.todo(`query`, async () => {
1515
const mockRes = ctx.res({ body: { data } }).spec.body!
@@ -75,7 +75,7 @@ describe(`custom scalar`, () => {
7575
ctx.res({ body: { data: {} } })
7676
})
7777
const clientExpected = (expectedDocument: (document: any) => void) => {
78-
const client = create<$.Index>({
78+
const client = create<Index>({
7979
url: ctx.url,
8080
schemaIndex,
8181
hooks: {

src/generator/code/code.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export interface Input {
1919
}
2020
schemaSource: string
2121
options?: {
22+
/**
23+
* Should custom scalars definitions be imported into the generated output?
24+
*/
25+
customScalars?: boolean
2226
formatter?: Formatter
2327
TSDoc?: {
2428
noDocPolicy?: 'message' | 'ignore'

src/generator/code/index.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,23 @@ export const generateIndex = (config: Config) => {
77
let code = ``
88
code += `import type * as ${namespace} from './SchemaBuildtime.js'\n\n`
99
code += Code.export$(
10-
Code.namespace(
11-
`$`,
12-
Code.group(
13-
Code.export$(
14-
Code.interface$(
15-
`Index`,
16-
Code.objectFrom({
17-
Root: {
18-
type: Code.objectFrom({
19-
Query: hasQuery(config.typeMapByKind) ? `${namespace}.Root.Query` : null,
20-
Mutation: hasMutation(config.typeMapByKind) ? `${namespace}.Root.Mutation` : null,
21-
Subscription: hasSubscription(config.typeMapByKind) ? `${namespace}.Root.Subscription` : null,
22-
}),
23-
},
24-
objects: Code.objectFromEntries(
25-
config.typeMapByKind.GraphQLObjectType.map(_ => [_.name, `${namespace}.Object.${_.name}`]),
26-
),
27-
unions: Code.objectFromEntries(
28-
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${namespace}.Union.${_.name}`]),
29-
),
30-
}),
31-
),
10+
Code.interface$(
11+
`Index`,
12+
Code.objectFrom({
13+
Root: {
14+
type: Code.objectFrom({
15+
Query: hasQuery(config.typeMapByKind) ? `${namespace}.Root.Query` : null,
16+
Mutation: hasMutation(config.typeMapByKind) ? `${namespace}.Root.Mutation` : null,
17+
Subscription: hasSubscription(config.typeMapByKind) ? `${namespace}.Root.Subscription` : null,
18+
}),
19+
},
20+
objects: Code.objectFromEntries(
21+
config.typeMapByKind.GraphQLObjectType.map(_ => [_.name, `${namespace}.Object.${_.name}`]),
3222
),
33-
),
23+
unions: Code.objectFromEntries(
24+
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${namespace}.Union.${_.name}`]),
25+
),
26+
}),
3427
),
3528
)
3629

src/generator/files.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { createFromBuffer } from '@dprint/formatter'
22
import { getPath } from '@dprint/typescript'
3+
import _ from 'json-bigint'
34
import fs from 'node:fs/promises'
45
import * as Path from 'node:path'
6+
import { errorFromMaybeError } from '../lib/prelude.js'
57
import { generateCode, type Input as GenerateInput } from './code/code.js'
68

79
export interface Input {
@@ -16,15 +18,21 @@ export const generateFiles = async (input: Input) => {
1618
const sourceDirPath = input.sourceDirPath ?? process.cwd()
1719
const schemaPath = input.schemaPath ?? Path.join(sourceDirPath, `schema.graphql`)
1820
const schemaSource = await fs.readFile(schemaPath, `utf8`)
19-
const options = (input.format ?? true)
20-
? {
21-
formatter: createFromBuffer(await fs.readFile(getPath())),
22-
}
23-
: undefined
21+
22+
const customScalarCodecsPath = Path.relative(input.outputDirPath, Path.join(sourceDirPath, `customScalarCodecs.js`))
23+
// todo support other extensions: .tsx,.js,.mjs,.cjs
24+
const customScalarCodecsPathExists = await fileExists(customScalarCodecsPath.replace(`.js`, `.ts`))
25+
const formatter = (input.format ?? true) ? createFromBuffer(await fs.readFile(getPath())) : undefined
26+
27+
const options: GenerateInput['options'] = {
28+
formatter,
29+
customScalars: customScalarCodecsPathExists,
30+
}
31+
2432
const code = generateCode({
2533
schemaSource,
2634
importPaths: {
27-
customScalarCodecs: Path.relative(input.outputDirPath, Path.join(sourceDirPath, `customScalarCodecs.js`)),
35+
customScalarCodecs: customScalarCodecsPath,
2836
},
2937
...input.code,
3038
options,
@@ -35,3 +43,14 @@ export const generateFiles = async (input: Input) => {
3543
await fs.writeFile(`${input.outputDirPath}/Scalar.ts`, code.scalars, { encoding: `utf8` })
3644
await fs.writeFile(`${input.outputDirPath}/SchemaRuntime.ts`, code.schemaRuntime, { encoding: `utf8` })
3745
}
46+
47+
const fileExists = async (path: string) => {
48+
return Boolean(
49+
await fs.stat(path).catch((_: unknown) => {
50+
const error = errorFromMaybeError(_)
51+
return `code` in error && typeof error.code === `string` && error.code === `ENOENT`
52+
? null
53+
: Promise.reject(error)
54+
}),
55+
)
56+
}

0 commit comments

Comments
 (0)