Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfdca0a7

Browse files
authored
feat(ts-client): index sans dollar namespace (#767)
1 parentc77de38 commitfdca0a7

File tree

11 files changed

+115
-101
lines changed

11 files changed

+115
-101
lines changed

‎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 aboutcustomizing 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-
importtype{$}from'../../tests/ts/_/schema/generated/Index.js'
4+
importtype{Index}from'../../tests/ts/_/schema/generated/Index.js'
55
importtype*asSchemafrom'../../tests/ts/_/schema/generated/SchemaBuildtime.js'
66
importtype{SelectionSet}from'../SelectionSet/__.js'
77
importtype{ResultSet}from'./__.js'
88

9-
typeI=$.Index
9+
typeI=Index
1010
typeRS<$selectionSetextendsSelectionSet.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-
importtype{$}from'../../tests/ts/_/schema/generated/Index.js'
2+
importtype{Index}from'../../tests/ts/_/schema/generated/Index.js'
33
importtype{SelectionSet}from'./__.js'
44

5-
typeQ=SelectionSet.Query<$.Index>
5+
typeQ=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-
importtype{$}from'../../tests/ts/_/schema/generated/Index.js'
3+
importtype{Index}from'../../tests/ts/_/schema/generated/Index.js'
44
importtype{SelectionSet}from'./__.js'
55
import{toGraphQLDocumentString}from'./toGraphQLDocumentString.js'
66

7-
typeQ=SelectionSet.Query<$.Index>
7+
// eslint-disable-next-line
8+
//@ts-ignore
9+
typeQ=SelectionSet.Query<Index>
810
consts=(selectionSet:Q)=>selectionSet
911
constprepareResult=(ss:Q)=>{
1012
constgraphqlDocumentString=toGraphQLDocumentString(ssasany)

‎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
typeIndicator=0|1|boolean
1919

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

2422
typeSS={
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-
importtype{$}from'../tests/ts/_/schema/generated/Index.js'
4+
importtype{Index}from'../tests/ts/_/schema/generated/Index.js'
55
import{$IndexasschemaIndex}from'../tests/ts/_/schema/generated/SchemaRuntime.js'
66
import{create}from'./client.js'
77

88
constctx=setupMockServer()
99
constdata={fooBarUnion:{int:1}}
1010

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

1414
test.todo(`query`,async()=>{
1515
constmockRes=ctx.res({body:{ data}}).spec.body!
@@ -75,7 +75,7 @@ describe(`custom scalar`, () => {
7575
ctx.res({body:{data:{}}})
7676
})
7777
constclientExpected=(expectedDocument:(document:any)=>void)=>{
78-
constclient=create<$.Index>({
78+
constclient=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
letcode=``
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
importfsfrom'node:fs/promises'
45
import*asPathfrom'node:path'
6+
import{errorFromMaybeError}from'../lib/prelude.js'
57
import{generateCode,typeInputasGenerateInput}from'./code/code.js'
68

79
exportinterfaceInput{
@@ -16,15 +18,21 @@ export const generateFiles = async (input: Input) => {
1618
constsourceDirPath=input.sourceDirPath??process.cwd()
1719
constschemaPath=input.schemaPath??Path.join(sourceDirPath,`schema.graphql`)
1820
constschemaSource=awaitfs.readFile(schemaPath,`utf8`)
19-
constoptions=(input.format??true)
20-
?{
21-
formatter:createFromBuffer(awaitfs.readFile(getPath())),
22-
}
23-
:undefined
21+
22+
constcustomScalarCodecsPath=Path.relative(input.outputDirPath,Path.join(sourceDirPath,`customScalarCodecs.js`))
23+
// todo support other extensions: .tsx,.js,.mjs,.cjs
24+
constcustomScalarCodecsPathExists=awaitfileExists(customScalarCodecsPath.replace(`.js`,`.ts`))
25+
constformatter=(input.format??true) ?createFromBuffer(awaitfs.readFile(getPath())) :undefined
26+
27+
constoptions:GenerateInput['options']={
28+
formatter,
29+
customScalars:customScalarCodecsPathExists,
30+
}
31+
2432
constcode=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
awaitfs.writeFile(`${input.outputDirPath}/Scalar.ts`,code.scalars,{encoding:`utf8`})
3644
awaitfs.writeFile(`${input.outputDirPath}/SchemaRuntime.ts`,code.schemaRuntime,{encoding:`utf8`})
3745
}
46+
47+
constfileExists=async(path:string)=>{
48+
returnBoolean(
49+
awaitfs.stat(path).catch((_:unknown)=>{
50+
consterror=errorFromMaybeError(_)
51+
return`code`inerror&&typeoferror.code===`string`&&error.code===`ENOENT`
52+
?null
53+
:Promise.reject(error)
54+
}),
55+
)
56+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp