-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ts-client): client modules (#791)
- Loading branch information
1 parent
a554328
commit 6f07a0e
Showing
5 changed files
with
165 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ExecutionResult } from 'graphql' | ||
|
||
// todo: dataAndErrors | dataAndSchemaErrors | ||
export type ReturnModeType = 'graphql' | 'data' | ||
|
||
export type OptionsInput = { | ||
returnMode: ReturnModeType | undefined | ||
} | ||
|
||
export type OptionsInputDefaults = { | ||
returnMode: 'data' | ||
} | ||
|
||
export type Config = { | ||
returnMode: ReturnModeType | ||
} | ||
|
||
export type ApplyInputDefaults<Input extends OptionsInput> = { | ||
[Key in keyof OptionsInputDefaults]: undefined extends Input[Key] ? OptionsInputDefaults[Key] : Input[Key] | ||
} | ||
|
||
// dprint-ignore | ||
export type ReturnMode<$Config extends Config, $Data> = | ||
$Config['returnMode'] extends 'graphql' ? ExecutionResult<$Data> : $Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { ExecutionResult } from 'graphql' | ||
import type { Exact } from '../lib/prelude.js' | ||
import type { TSError } from '../lib/TSError.js' | ||
import type { InputFieldsAllNullable, Schema } from '../Schema/__.js' | ||
import type { Config, OptionsInputDefaults, ReturnMode } from './Config.js' | ||
import type { ResultSet } from './ResultSet/__.js' | ||
import type { SelectionSet } from './SelectionSet/__.js' | ||
|
||
type OperationName = 'query' | 'mutation' | ||
|
||
// dprint-ignore | ||
export type GetRootTypeMethods<$Config extends OptionsInputDefaults, $Index extends Schema.Index> = { | ||
[$OperationName in OperationName as $Index['Root'][Capitalize<$OperationName>] extends null ? never : $OperationName]: | ||
RootTypeMethods<$Config, $Index, Capitalize<$OperationName>> | ||
} | ||
|
||
// dprint-ignore | ||
export type RootTypeMethods<$Config extends OptionsInputDefaults, $Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName> = | ||
$Index['Root'][$RootTypeName] extends Schema.Object$2 ? | ||
( | ||
& { | ||
$batch: RootMethod<$Config, $Index, $RootTypeName> | ||
} | ||
& { | ||
[$RootTypeFieldName in keyof $Index['Root'][$RootTypeName]['fields'] & string]: | ||
RootTypeFieldMethod<{ | ||
Config: $Config, | ||
Index: $Index, | ||
RootTypeName: $RootTypeName, | ||
RootTypeFieldName: $RootTypeFieldName | ||
Field: $Index['Root'][$RootTypeName]['fields'][$RootTypeFieldName] | ||
}> | ||
} | ||
) | ||
: TSError<'RootTypeMethods', `Your schema does not have the root type "${$RootTypeName}".`> | ||
|
||
// dprint-ignore | ||
type RootMethod<$Config extends Config, $Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName> = | ||
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Root<$Index, $RootTypeName>>) => | ||
Promise<ReturnMode<$Config, ResultSet.Root<$SelectionSet, $Index, $RootTypeName>>> | ||
|
||
// dprint-ignore | ||
// type RootTypeFieldMethod<$Config extends OptionsInputDefaults, $Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName, $RootTypeFieldName extends string> = | ||
type RootTypeFieldMethod<$Context extends RootTypeFieldContext> = | ||
RootTypeFieldMethod_<$Context, $Context['Field']['type']> | ||
|
||
// dprint-ignore | ||
type RootTypeFieldMethod_<$Context extends RootTypeFieldContext, $Type extends Schema.Output.Any> = | ||
$Type extends Schema.Output.Nullable<infer $InnerType> ? RootTypeFieldMethod_<$Context, $InnerType> : | ||
$Type extends Schema.Output.List<infer $InnerType> ? RootTypeFieldMethod_<$Context, $InnerType> : | ||
$Type extends Schema.Scalar.Any ? ScalarFieldMethod<$Context> : | ||
// todo test this case | ||
$Type extends Schema.__typename ? ScalarFieldMethod<$Context> : | ||
ObjectLikeFieldMethod<$Context> | ||
|
||
// dprint-ignore | ||
type ObjectLikeFieldMethod<$Context extends RootTypeFieldContext> = | ||
<$SelectionSet>(selectionSet: Exact<$SelectionSet, SelectionSet.Field<$Context['Field'], $Context['Index'], { hideDirectives: true }>>) => | ||
Promise<ReturnModeForFieldMethod<$Context, ResultSet.Field<$SelectionSet, $Context['Field'], $Context['Index']>>> | ||
|
||
// dprint-ignore | ||
type ScalarFieldMethod<$Context extends RootTypeFieldContext> = | ||
$Context['Field']['args'] extends Schema.Args<infer $Fields> ? InputFieldsAllNullable<$Fields> extends true ? <$SelectionSet>(args?: Exact<$SelectionSet, SelectionSet.Args<$Context['Field']['args']>>) => Promise<ReturnModeForFieldMethod<$Context, ResultSet.Field<$SelectionSet, $Context['Field'], $Context['Index']>>> : | ||
<$SelectionSet>(args: Exact<$SelectionSet, SelectionSet.Args<$Context['Field']['args']>>) => Promise<ReturnModeForFieldMethod<$Context, ResultSet.Field<$SelectionSet, $Context['Field'], $Context['Index']>>> : | ||
(() => Promise<ReturnModeForFieldMethod<$Context, ResultSet.Field<true, $Context['Field'], $Context['Index']>>>) | ||
// dprint-ignore | ||
type ReturnModeForFieldMethod<$Context extends RootTypeFieldContext, $Data> = | ||
$Context['Config']['returnMode'] extends 'data' | ||
? $Data | ||
: ExecutionResult<{ [k in $Context['RootTypeFieldName']] : $Data }> | ||
|
||
type RootTypeFieldContext = { | ||
Config: Config | ||
Index: Schema.Index | ||
RootTypeName: Schema.RootTypeName | ||
RootTypeFieldName: string | ||
Field: Schema.SomeField | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters