Skip to content

Commit 1287eda

Browse files
authored
refactor(ts-client): schema module (#770)
1 parent 33a0278 commit 1287eda

55 files changed

Lines changed: 694 additions & 656 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ResultSet/ResultSet.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import type { Simplify } from 'type-fest'
44
import type { GetKeyOr, SimplifyDeep } from '../lib/prelude.js'
55
import type { TSError } from '../lib/TSError.js'
6-
import type { Schema } from '../Schema/__.js'
6+
import type { Schema, SomeField } from '../Schema/__.js'
7+
import type { PickScalarFields } from '../Schema/Output/Output.js'
78
import type { SelectionSet } from '../SelectionSet/__.js'
89

910
// dprint-ignore
@@ -19,18 +20,15 @@ export type Subscription<$SelectionSetSubscription extends object, $Index extend
1920
SimplifyDeep<Object$<$SelectionSetSubscription, Exclude<$Index['Root']['Subscription'], null>, $Index>>
2021

2122
// dprint-ignore
22-
export type Object$<$SelectionSet, $Node extends Schema.Named.Object$2, $Index extends Schema.Index> =
23+
export type Object$<$SelectionSet, $Node extends Schema.Output.Object$2, $Index extends Schema.Index> =
2324
SelectionSet.IsSelectScalarsWildcard<$SelectionSet> extends true
2425

2526
/**
2627
* Handle Scalars Wildcard
2728
*/
2829
?
2930
{
30-
[$Key in keyof $Node['fields'] as $Node['fields'][$Key] extends Schema.Field.Field<Schema.Field.Type.Output.__typename> | {'typeUnwrapped':{kind:'Scalar'}} ? $Key : never]:
31-
// eslint-disable-next-line
32-
// @ts-ignore infinite depth issue, can this be fixed?
33-
Field<$SelectionSet, Schema.Field.As<$Node['fields'][$Key]>, $Index>
31+
[$Key in keyof PickScalarFields<$Node>]: Field<$SelectionSet, $Node['fields'][$Key], $Index>
3432
}
3533
/**
3634
* Handle fields in regular way.
@@ -44,15 +42,15 @@ export type Object$<$SelectionSet, $Node extends Schema.Named.Object$2, $Index e
4442
}>
4543

4644
// dprint-ignore
47-
type Union<$SelectionSet, $Node extends Schema.Named.Union, $Index extends Schema.Index> =
45+
type Union<$SelectionSet, $Node extends Schema.Output.Union, $Index extends Schema.Index> =
4846
OnTypeFragment<$SelectionSet,$Node['members'][number], $Index>
4947

5048
// dprint-ignore
51-
type Interface<$SelectionSet, $Node extends Schema.Named.Interface, $Index extends Schema.Index> =
49+
type Interface<$SelectionSet, $Node extends Schema.Output.Interface, $Index extends Schema.Index> =
5250
OnTypeFragment<$SelectionSet, $Node['implementors'][number], $Index>
5351

5452
// dprint-ignore
55-
type OnTypeFragment<$SelectionSet, $Node extends Schema.Named.Object$2, $Index extends Schema.Index> =
53+
type OnTypeFragment<$SelectionSet, $Node extends Schema.Output.Object$2, $Index extends Schema.Index> =
5654
$Node extends any // force distribution
5755
? Object$<
5856
GetKeyOr<$SelectionSet, `on${Capitalize<$Node['fields']['__typename']['type']['type']>}`, {}> & SelectionSet.OmitOnTypeFragments<$SelectionSet>,
@@ -62,7 +60,7 @@ type OnTypeFragment<$SelectionSet, $Node extends Schema.Named.Object$2, $Index e
6260
: never
6361

6462
// dprint-ignore
65-
type Field<$SelectionSet, $Field extends Schema.Field.Field, $Index extends Schema.Index> =
63+
type Field<$SelectionSet, $Field extends SomeField, $Index extends Schema.Index> =
6664
$SelectionSet extends SelectionSet.Directive.Include.Negative | SelectionSet.Directive.Skip.Positive ?
6765
null :
6866
(
@@ -74,18 +72,18 @@ type Field<$SelectionSet, $Field extends Schema.Field.Field, $Index extends Sche
7472
// dprint-ignore
7573
type FieldType<
7674
$SelectionSet,
77-
$Type extends Schema.Field.Type.Output.Any,
75+
$Type extends Schema.Output.Any,
7876
$Index extends Schema.Index
7977
> =Simplify<
80-
$Type extends Schema.Field.Type.Output.__typename<infer $Value> ? $Value :
81-
$Type extends Schema.Field.Type.Output.Nullable<infer $InnerType> ? null | FieldType<$SelectionSet, $InnerType, $Index> :
82-
$Type extends Schema.Field.Type.Output.List<infer $InnerType> ? Array<FieldType<$SelectionSet, $InnerType, $Index>> :
83-
$Type extends Schema.Named.Enum<infer _, infer $Members> ? $Members[number] :
84-
$Type extends Schema.Named.Scalar.Any ? ReturnType<$Type['codec']['decode']> :
85-
$Type extends Schema.Named.Object$2 ? Object$<$SelectionSet,$Type,$Index> :
86-
$Type extends Schema.Named.Interface ? Interface<$SelectionSet,$Type,$Index> :
87-
$Type extends Schema.Named.Union ? Union<$SelectionSet,$Type,$Index> :
88-
TSError<'FieldType', `Unknown type`, { $Type: $Type }>
78+
$Type extends Schema.__typename<infer $Value> ? $Value :
79+
$Type extends Schema.Output.Nullable<infer $InnerType> ? null | FieldType<$SelectionSet, $InnerType, $Index> :
80+
$Type extends Schema.Output.List<infer $InnerType> ? Array<FieldType<$SelectionSet, $InnerType, $Index>> :
81+
$Type extends Schema.Enum<infer _, infer $Members> ? $Members[number] :
82+
$Type extends Schema.Scalar.Any ? ReturnType<$Type['codec']['decode']> :
83+
$Type extends Schema.Object$2 ? Object$<$SelectionSet,$Type,$Index> :
84+
$Type extends Schema.Interface ? Interface<$SelectionSet,$Type,$Index> :
85+
$Type extends Schema.Union ? Union<$SelectionSet,$Type,$Index> :
86+
TSError<'FieldType', `Unknown type`, { $Type: $Type }>
8987
>
9088

9189
// dprint-ignore
@@ -104,5 +102,5 @@ type FieldDirectiveSkip<$SelectionSet> =
104102

105103
// dprint-ignore
106104
export namespace Errors {
107-
export type UnknownFieldName<$FieldName extends string, $Object extends Schema.Named.Object$2> = TSError<'Object', `field "${$FieldName}" does not exist on object "${$Object['fields']['__typename']['type']['type']}"`>
105+
export type UnknownFieldName<$FieldName extends string, $Object extends Schema.Object$2> = TSError<'Object', `field "${$FieldName}" does not exist on object "${$Object['fields']['__typename']['type']['type']}"`>
108106
}

src/Schema/Args.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Input } from './_.js'
2+
import type { Nullable } from './Input/types/Nullable.js'
3+
4+
type InputFields = Record<string, any>
5+
6+
// dprint-ignore
7+
export type InputFieldsAllNullable<$Fields extends InputFields> =
8+
Exclude<$Fields[keyof $Fields], Nullable<any>> extends never ? true : false
9+
10+
export interface Args<$Fields extends InputFields> {
11+
fields: $Fields
12+
}
13+
14+
export const Args = <F extends InputFields>(fields: F): Args<F> => {
15+
return {
16+
fields,
17+
}
18+
}
19+
20+
export type OmitNullableFields<$Fields extends InputFields> = {
21+
[Key in keyof $Fields as $Fields[Key] extends Input.Nullable<any> ? never : Key]: $Fields[Key]
22+
}
23+
24+
export type PickNullableFields<$Fields extends InputFields> = {
25+
[Key in keyof $Fields as $Fields[Key] extends Input.Nullable<any> ? Key : never]: $Fields[Key]
26+
}

src/Schema/Field.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Args } from './Args.js'
2+
import type { MaybeThunk } from './core/helpers.js'
3+
import type { Hybrid } from './Hybrid/__.js'
4+
import type { Output } from './Output/__.js'
5+
6+
export type Field<$Type extends Output.Any, $Args extends Args<any> | null> = {
7+
type: $Type
8+
args: $Args
9+
}
10+
11+
export const field = <$Type extends Output.Any, $Args extends null | Args<any> = null>(
12+
type: MaybeThunk<$Type>,
13+
args: $Args = null as $Args,
14+
): Field<$Type, $Args> => {
15+
return {
16+
// At type level "type" is not a thunk
17+
type: type as any, // eslint-disable-line
18+
args,
19+
}
20+
}
21+
22+
// todo test non null union and non null interface fields
23+
export type SomeField = Field<
24+
Hybrid.Enum | Hybrid.Scalar.Any | Output.List<any> | Output.Nullable<any> | Output.Object$2<any, any>,
25+
Args<any> | null
26+
>
27+
28+
export type SomeFields<$Keys extends string | number | symbol = string | number | symbol> = Record<
29+
$Keys,
30+
SomeField
31+
>

src/Schema/Field/Field.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Schema/Field/Type.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

src/Schema/Field/_.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Schema/Field/__.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/Schema/Hybrid/_.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './types/Enum.js'
2+
export * from './types/Scalar/__.js'

src/Schema/Hybrid/__.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as Hybrid from './_.js'

0 commit comments

Comments
 (0)