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

Commit6f07a0e

Browse files
authored
refactor(ts-client): client modules (#791)
1 parenta554328 commit6f07a0e

File tree

5 files changed

+165
-160
lines changed

5 files changed

+165
-160
lines changed

‎src/client/Config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
importtype{ExecutionResult}from'graphql'
2+
3+
// todo: dataAndErrors | dataAndSchemaErrors
4+
exporttypeReturnModeType='graphql'|'data'
5+
6+
exporttypeOptionsInput={
7+
returnMode:ReturnModeType|undefined
8+
}
9+
10+
exporttypeOptionsInputDefaults={
11+
returnMode:'data'
12+
}
13+
14+
exporttypeConfig={
15+
returnMode:ReturnModeType
16+
}
17+
18+
exporttypeApplyInputDefaults<InputextendsOptionsInput>={
19+
[KeyinkeyofOptionsInputDefaults]:undefinedextendsInput[Key] ?OptionsInputDefaults[Key] :Input[Key]
20+
}
21+
22+
// dprint-ignore
23+
exporttypeReturnMode<$ConfigextendsConfig,$Data>=
24+
$Config['returnMode']extends'graphql' ?ExecutionResult<$Data> :$Data

‎src/client/RootTypeMethods.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
importtype{ExecutionResult}from'graphql'
2+
importtype{Exact}from'../lib/prelude.js'
3+
importtype{TSError}from'../lib/TSError.js'
4+
importtype{InputFieldsAllNullable,Schema}from'../Schema/__.js'
5+
importtype{Config,OptionsInputDefaults,ReturnMode}from'./Config.js'
6+
importtype{ResultSet}from'./ResultSet/__.js'
7+
importtype{SelectionSet}from'./SelectionSet/__.js'
8+
9+
typeOperationName='query'|'mutation'
10+
11+
// dprint-ignore
12+
exporttypeGetRootTypeMethods<$ConfigextendsOptionsInputDefaults,$IndexextendsSchema.Index>={
13+
[$OperationNameinOperationNameas$Index['Root'][Capitalize<$OperationName>]extendsnull ?never :$OperationName]:
14+
RootTypeMethods<$Config,$Index,Capitalize<$OperationName>>
15+
}
16+
17+
// dprint-ignore
18+
exporttypeRootTypeMethods<$ConfigextendsOptionsInputDefaults,$IndexextendsSchema.Index,$RootTypeNameextendsSchema.RootTypeName>=
19+
$Index['Root'][$RootTypeName]extendsSchema.Object$2 ?
20+
(
21+
&{
22+
$batch:RootMethod<$Config,$Index,$RootTypeName>
23+
}
24+
&{
25+
[$RootTypeFieldNameinkeyof$Index['Root'][$RootTypeName]['fields']&string]:
26+
RootTypeFieldMethod<{
27+
Config:$Config,
28+
Index:$Index,
29+
RootTypeName:$RootTypeName,
30+
RootTypeFieldName:$RootTypeFieldName
31+
Field:$Index['Root'][$RootTypeName]['fields'][$RootTypeFieldName]
32+
}>
33+
}
34+
)
35+
:TSError<'RootTypeMethods', `Your schema does not have the root type "${$RootTypeName}".`>
36+
37+
// dprint-ignore
38+
typeRootMethod<$ConfigextendsConfig,$IndexextendsSchema.Index,$RootTypeNameextendsSchema.RootTypeName>=
39+
<$SelectionSetextendsobject>(selectionSet:Exact<$SelectionSet,SelectionSet.Root<$Index,$RootTypeName>>)=>
40+
Promise<ReturnMode<$Config,ResultSet.Root<$SelectionSet,$Index,$RootTypeName>>>
41+
42+
// dprint-ignore
43+
// type RootTypeFieldMethod<$Config extends OptionsInputDefaults, $Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName, $RootTypeFieldName extends string> =
44+
typeRootTypeFieldMethod<$ContextextendsRootTypeFieldContext>=
45+
RootTypeFieldMethod_<$Context,$Context['Field']['type']>
46+
47+
// dprint-ignore
48+
typeRootTypeFieldMethod_<$ContextextendsRootTypeFieldContext,$TypeextendsSchema.Output.Any>=
49+
$TypeextendsSchema.Output.Nullable<infer$InnerType> ?RootTypeFieldMethod_<$Context,$InnerType> :
50+
$TypeextendsSchema.Output.List<infer$InnerType> ?RootTypeFieldMethod_<$Context,$InnerType> :
51+
$TypeextendsSchema.Scalar.Any ?ScalarFieldMethod<$Context> :
52+
// todo test this case
53+
$TypeextendsSchema.__typename ?ScalarFieldMethod<$Context> :
54+
ObjectLikeFieldMethod<$Context>
55+
56+
// dprint-ignore
57+
typeObjectLikeFieldMethod<$ContextextendsRootTypeFieldContext>=
58+
<$SelectionSet>(selectionSet:Exact<$SelectionSet,SelectionSet.Field<$Context['Field'],$Context['Index'],{hideDirectives:true}>>)=>
59+
Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<$SelectionSet,$Context['Field'],$Context['Index']>>>
60+
61+
// dprint-ignore
62+
typeScalarFieldMethod<$ContextextendsRootTypeFieldContext>=
63+
$Context['Field']['args']extendsSchema.Args<infer$Fields> ?InputFieldsAllNullable<$Fields>extendstrue ?<$SelectionSet>(args?:Exact<$SelectionSet,SelectionSet.Args<$Context['Field']['args']>>)=>Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<$SelectionSet,$Context['Field'],$Context['Index']>>> :
64+
<$SelectionSet>(args:Exact<$SelectionSet,SelectionSet.Args<$Context['Field']['args']>>)=>Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<$SelectionSet,$Context['Field'],$Context['Index']>>> :
65+
(()=>Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<true,$Context['Field'],$Context['Index']>>>)
66+
// dprint-ignore
67+
typeReturnModeForFieldMethod<$ContextextendsRootTypeFieldContext,$Data>=
68+
$Context['Config']['returnMode']extends'data'
69+
?$Data
70+
:ExecutionResult<{[kin$Context['RootTypeFieldName']] :$Data}>
71+
72+
typeRootTypeFieldContext={
73+
Config:Config
74+
Index:Schema.Index
75+
RootTypeName:Schema.RootTypeName
76+
RootTypeFieldName:string
77+
Field:Schema.SomeField
78+
}

‎src/client/client.ts

Lines changed: 8 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,27 @@
11
importtype{ExecutionResult}from'graphql'
22
import{typeDocumentNode,execute,graphql,typeGraphQLSchema}from'graphql'
3-
importtype{MergeExclusive,NonEmptyObject}from'type-fest'
43
importtype{ExcludeUndefined}from'type-fest/source/required-deep.js'
54
importrequestfrom'../entrypoints/main.js'
6-
importtype{RootTypeName}from'../lib/graphql.js'
7-
importtype{Exact,IsMultipleKeys}from'../lib/prelude.js'
8-
importtype{TSError}from'../lib/TSError.js'
9-
importtype{InputFieldsAllNullable,Object$2}from'../Schema/__.js'
5+
importtype{RootTypeName,Variables}from'../lib/graphql.js'
6+
importtype{Object$2}from'../Schema/__.js'
107
import{Schema}from'../Schema/__.js'
118
import{readMaybeThunk}from'../Schema/core/helpers.js'
9+
importtype{ApplyInputDefaults,OptionsInputDefaults,ReturnModeType}from'./Config.js'
1210
import*asCustomScalarsfrom'./customScalars.js'
11+
importtype{DocumentFn}from'./document.js'
1312
import{toDocumentExpression}from'./document.js'
14-
importtype{ResultSet}from'./ResultSet/__.js'
13+
importtype{GetRootTypeMethods}from'./RootTypeMethods.js'
1514
import{SelectionSet}from'./SelectionSet/__.js'
1615
importtype{DocumentObject,GraphQLObjectSelection}from'./SelectionSet/toGraphQLDocumentString.js'
1716

18-
typeVariables=Record<string,string|number|boolean|null>// todo or any custom scalars too
19-
20-
typeRootTypeFieldContext={
21-
Config:Config
22-
Index:Schema.Index
23-
RootTypeName:Schema.RootTypeName
24-
RootTypeFieldName:string
25-
Field:Schema.SomeField
26-
}
27-
28-
// dprint-ignore
29-
typeRootTypeMethods<$ConfigextendsOptionsInputDefaults,$IndexextendsSchema.Index,$RootTypeNameextendsSchema.RootTypeName>=
30-
$Index['Root'][$RootTypeName]extendsSchema.Object$2 ?
31-
(
32-
&{
33-
$batch:RootMethod<$Config,$Index,$RootTypeName>
34-
}
35-
&{
36-
[$RootTypeFieldNameinkeyof$Index['Root'][$RootTypeName]['fields']&string]:
37-
RootTypeFieldMethod<{
38-
Config:$Config,
39-
Index:$Index,
40-
RootTypeName:$RootTypeName,
41-
RootTypeFieldName:$RootTypeFieldName
42-
Field:$Index['Root'][$RootTypeName]['fields'][$RootTypeFieldName]
43-
}>
44-
}
45-
)
46-
:TSError<'RootTypeMethods', `Your schema does not have the root type "${$RootTypeName}".`>
47-
48-
// dprint-ignore
49-
typeRootMethod<$ConfigextendsConfig,$IndexextendsSchema.Index,$RootTypeNameextendsSchema.RootTypeName>=
50-
<$SelectionSetextendsobject>(selectionSet:Exact<$SelectionSet,SelectionSet.Root<$Index,$RootTypeName>>)=>
51-
Promise<ReturnMode<$Config,ResultSet.Root<$SelectionSet,$Index,$RootTypeName>>>
52-
53-
// dprint-ignore
54-
// type RootTypeFieldMethod<$Config extends OptionsInputDefaults, $Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName, $RootTypeFieldName extends string> =
55-
typeRootTypeFieldMethod<$ContextextendsRootTypeFieldContext>=
56-
RootTypeFieldMethod_<$Context,$Context['Field']['type']>
57-
58-
// dprint-ignore
59-
typeRootTypeFieldMethod_<$ContextextendsRootTypeFieldContext,$TypeextendsSchema.Output.Any>=
60-
$TypeextendsSchema.Output.Nullable<infer$InnerType> ?RootTypeFieldMethod_<$Context,$InnerType> :
61-
$TypeextendsSchema.Output.List<infer$InnerType> ?RootTypeFieldMethod_<$Context,$InnerType> :
62-
$TypeextendsSchema.Scalar.Any ?ScalarFieldMethod<$Context> :
63-
// todo test this case
64-
$TypeextendsSchema.__typename ?ScalarFieldMethod<$Context> :
65-
ObjectLikeFieldMethod<$Context>
66-
67-
// dprint-ignore
68-
typeObjectLikeFieldMethod<$ContextextendsRootTypeFieldContext>=
69-
<$SelectionSet>(selectionSet:Exact<$SelectionSet,SelectionSet.Field<$Context['Field'],$Context['Index'],{hideDirectives:true}>>)=>
70-
Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<$SelectionSet,$Context['Field'],$Context['Index']>>>
71-
72-
// dprint-ignore
73-
typeScalarFieldMethod<$ContextextendsRootTypeFieldContext>=
74-
$Context['Field']['args']extendsSchema.Args<infer$Fields> ?InputFieldsAllNullable<$Fields>extendstrue ?<$SelectionSet>(args?:Exact<$SelectionSet,SelectionSet.Args<$Context['Field']['args']>>)=>Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<$SelectionSet,$Context['Field'],$Context['Index']>>> :
75-
<$SelectionSet>(args:Exact<$SelectionSet,SelectionSet.Args<$Context['Field']['args']>>)=>Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<$SelectionSet,$Context['Field'],$Context['Index']>>> :
76-
(()=>Promise<ReturnModeForFieldMethod<$Context,ResultSet.Field<true,$Context['Field'],$Context['Index']>>>)
77-
// dprint-ignore
78-
typeReturnModeForFieldMethod<$ContextextendsRootTypeFieldContext,$Data>=
79-
$Context['Config']['returnMode']extends'data'
80-
?$Data
81-
:ExecutionResult<{[kin$Context['RootTypeFieldName']] :$Data}>
82-
83-
// dprint-ignore
84-
typeDocument<$IndexextendsSchema.Index>=
85-
{
86-
[name:string]:
87-
$Index['Root']['Query']extendsnull ?{mutation:SelectionSet.Root<$Index,'Mutation'>} :
88-
$Index['Root']['Mutation']extendsnull ?{query:SelectionSet.Root<$Index,'Query'>} :
89-
MergeExclusive<
90-
{
91-
query:SelectionSet.Root<$Index,'Query'>
92-
},
93-
{
94-
mutation:SelectionSet.Root<$Index,'Mutation'>
95-
}
96-
>
97-
}
98-
99-
// dprint-ignore
100-
typeGetOperation<Textends{query:any}|{mutation:any}>=
101-
Textends{query:inferU} ?U :
102-
Textends{mutation:inferU} ?U :
103-
never
104-
105-
// dprint-ignore
106-
typeValidateDocumentOperationNames<$Document>=
107-
// This initial condition checks that the document is not already in an error state.
108-
// Namely from for example { x: { mutation: { ... }}} where the schema has no mutations.
109-
// Which is statically caught by the `Document` type. In that case the document type variable
110-
// no longer functions per normal with regards to keyof utility, not returning exact keys of the object
111-
// but instead this more general union. Not totally clear _why_, but we have tests covering this...
112-
string|numberextendskeyof$Document
113-
?$Document
114-
:keyof{[Kinkeyof$Document&stringasSchema.Named.NameParse<K>extendsnever ?K :never]:K}extendsnever
115-
?$Document
116-
:TSError<'ValidateDocumentOperationNames', `One or more Invalid operation name in document: ${keyof{[Kinkeyof$Document&stringasSchema.Named.NameParse<K>extendsnever ?K :never]:K}}`>
117-
118-
// todo: dataAndErrors | dataAndSchemaErrors
119-
typeReturnModeType='graphql'|'data'
120-
121-
typeOptionsInput={
122-
returnMode:ReturnModeType|undefined
123-
}
124-
125-
typeOptionsInputDefaults={
126-
returnMode:'data'
127-
}
128-
129-
typeConfig={
130-
returnMode:ReturnModeType
131-
}
132-
133-
typeApplyInputDefaults<InputextendsOptionsInput>={
134-
[KeyinkeyofOptionsInputDefaults]:undefinedextendsInput[Key] ?OptionsInputDefaults[Key] :Input[Key]
135-
}
136-
137-
// dprint-ignore
138-
typeReturnMode<$ConfigextendsConfig,$Data>=
139-
$Config['returnMode']extends'graphql' ?ExecutionResult<$Data> :$Data
140-
14117
// dprint-ignore
14218
exporttypeClient<$IndexextendsSchema.Index,$ConfigextendsOptionsInputDefaults>=
14319
&{
14420
// todo test raw
145-
raw:(document:string|DocumentNode,variables?:Variables,operationName?:string)=>Promise<ExecutionResult>
146-
document:<$DocumentextendsDocument<$Index>>
147-
(document:ValidateDocumentOperationNames<NonEmptyObject<$Document>>)=>
148-
// (document: $Document) =>
149-
{
150-
run:<$Nameextendskeyof$Document&string,$Paramsextends(IsMultipleKeys<$Document>extendstrue ?[name:$Name] :([]|[name:$Name|undefined]))>
151-
(...params:$Params)=>
152-
Promise<
153-
ReturnMode<$Config,ResultSet.Root<GetOperation<$Document[$Name]>,$Index,'Query'>>
154-
>
155-
}
21+
raw:(document:string|DocumentNode,variables?:Variables,operationName?:string)=>Promise<ExecutionResult>
22+
document:DocumentFn<$Config,$Index>
15623
}
157-
&(
158-
$Index['Root']['Query']extendsnull
159-
?unknown
160-
:{
161-
query:RootTypeMethods<$Config,$Index,'Query'>
162-
}
163-
)
164-
&(
165-
$Index['Root']['Mutation']extendsnull
166-
?unknown
167-
:{
168-
mutation:RootTypeMethods<$Config,$Index,'Mutation'>
169-
}
170-
)
171-
// todo
172-
// & ($SchemaIndex['Root']['Subscription'] extends null ? {
173-
// subscription: <$SelectionSet extends SelectionSet.Subscription<$SchemaIndex>>(selectionSet: $SelectionSet) => Promise<ResultSet.Subscription<$SelectionSet,$SchemaIndex>>
174-
// }
175-
// : unknown)
176-
//
24+
&GetRootTypeMethods<$Config,$Index>
17725

17826
interfaceHookInputDocumentEncode{
17927
rootIndex:Object$2

‎src/client/document.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
importtype{MergeExclusive,NonEmptyObject}from'type-fest'
2+
importtype{IsMultipleKeys}from'../lib/prelude.js'
3+
importtype{TSError}from'../lib/TSError.js'
4+
importtype{Schema}from'../Schema/__.js'
5+
importtype{Config,ReturnMode}from'./Config.js'
6+
importtype{ResultSet}from'./ResultSet/__.js'
17
import{SelectionSet}from'./SelectionSet/__.js'
28
importtype{DocumentObject}from'./SelectionSet/toGraphQLDocumentString.js'
39

10+
// dprint-ignore
11+
exporttypeDocumentFn<$ConfigextendsConfig,$IndexextendsSchema.Index>=
12+
<$DocumentextendsDocument<$Index>>(document:ValidateDocumentOperationNames<NonEmptyObject<$Document>>)=>
13+
{
14+
run:<
15+
$Nameextendskeyof$Document&string,
16+
$Paramsextends(IsMultipleKeys<$Document>extendstrue ?[name:$Name] :([]|[name:$Name|undefined])),
17+
>(...params:$Params)=>Promise<
18+
ReturnMode<$Config,ResultSet.Root<GetOperation<$Document[$Name]>,$Index,'Query'>>
19+
>
20+
}
21+
422
exportconsttoDocumentExpression=(
523
document:DocumentObject,
624
)=>{
@@ -11,3 +29,38 @@ export const toDocumentExpression = (
1129
return`${operationType}${operationName}${documentString}`
1230
}).join(`\n\n`)
1331
}
32+
33+
// dprint-ignore
34+
exporttypeDocument<$IndexextendsSchema.Index>=
35+
{
36+
[name:string]:
37+
$Index['Root']['Query']extendsnull ?{mutation:SelectionSet.Root<$Index,'Mutation'>} :
38+
$Index['Root']['Mutation']extendsnull ?{query:SelectionSet.Root<$Index,'Query'>} :
39+
MergeExclusive<
40+
{
41+
query:SelectionSet.Root<$Index,'Query'>
42+
},
43+
{
44+
mutation:SelectionSet.Root<$Index,'Mutation'>
45+
}
46+
>
47+
}
48+
49+
// dprint-ignore
50+
exporttypeValidateDocumentOperationNames<$Document>=
51+
// This initial condition checks that the document is not already in an error state.
52+
// Namely from for example { x: { mutation: { ... }}} where the schema has no mutations.
53+
// Which is statically caught by the `Document` type. In that case the document type variable
54+
// no longer functions per normal with regards to keyof utility, not returning exact keys of the object
55+
// but instead this more general union. Not totally clear _why_, but we have tests covering this...
56+
string|numberextendskeyof$Document
57+
?$Document
58+
:keyof{[Kinkeyof$Document&stringasSchema.Named.NameParse<K>extendsnever ?K :never]:K}extendsnever
59+
?$Document
60+
:TSError<'ValidateDocumentOperationNames', `One or more Invalid operation name in document: ${keyof{[Kinkeyof$Document&stringasSchema.Named.NameParse<K>extendsnever ?K :never]:K}}`>
61+
62+
// dprint-ignore
63+
typeGetOperation<Textends{query:any}|{mutation:any}>=
64+
Textends{query:inferU} ?U :
65+
Textends{mutation:inferU} ?U :
66+
never

‎src/lib/graphql.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,5 @@ export const hasMutation = (typeMapByKind: TypeMapByKind) =>
229229

230230
exportconsthasSubscription=(typeMapByKind:TypeMapByKind)=>
231231
typeMapByKind.GraphQLRootType.find((_)=>_.name===`Subscription`)
232+
233+
exporttypeVariables=Record<string,string|number|boolean|null>// todo or any custom scalars too

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp