Skip to content

Commit c580407

Browse files
authored
refactor: colocate custom scalars logic (#1177)
1 parent d3ffdc2 commit c580407

File tree

44 files changed

+542
-472
lines changed

Some content is hidden

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

44 files changed

+542
-472
lines changed

examples/__outputs__/20_output/output_preset__standard-graphql.output.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@ ContextualError: There was an error in the core implementation of hook "exchange
77
at runPipeline (/some/path/to/runPipeline.ts:XX:XX:18)
88
at async runPipeline (/some/path/to/runPipeline.ts:XX:XX:14)
99
at async runPipeline (/some/path/to/runPipeline.ts:XX:XX:14)
10-
... 2 lines matching cause stack trace ...
10+
at async Object.run (/some/path/to/main.ts:XX:XX:22)
11+
at async Object.send (/some/path/to/gql.ts:XX:XX:26)
1112
at async <anonymous> (/some/path/to/output_preset__standard-graphql.ts:XX:XX:16) {
1213
context: { hookName: 'exchange', source: 'implementation' },
1314
[cause]: TypeError: Failed to parse URL from ...
1415
at new Request (node:internal/deps/undici/undici:XX:XX)
1516
at Object.run (/some/path/to/core.ts:XX:XX:29)
16-
... 6 lines matching cause stack trace ...
17-
at async <anonymous> (/some/path/to/output_preset__standard-graphql.ts:XX:XX:16) {
17+
... 3 lines matching cause stack trace ...
18+
at async applyBody (/some/path/to/main.ts:XX:XX:22) {
1819
[cause]: TypeError: Invalid URL
1920
at new URL (node:internal/url:XX:XX)
2021
at new Request (node:internal/deps/undici/undici:XX:XX)
2122
at Object.run (/some/path/to/core.ts:XX:XX:29)
2223
at runHook (/some/path/to/runHook.ts:XX:XX:37)
23-
at runPipeline (/some/path/to/runPipeline.ts:XX:XX:8)
24-
at runPipeline (/some/path/to/runPipeline.ts:XX:XX:20)
25-
at async runPipeline (/some/path/to/runPipeline.ts:XX:XX:14)
26-
at async Object.run (/some/path/to/main.ts:XX:XX:22)
27-
at async Object.send (/some/path/to/gql.ts:XX:XX:26)
28-
at async <anonymous> (/some/path/to/output_preset__standard-graphql.ts:XX:XX:16) {
24+
at <anonymous> (/some/path/to/runHook.ts:XX:XX:14)
25+
at onRequest (/some/path/to/extension.ts:XX:XX:32)
26+
at async applyBody (/some/path/to/main.ts:XX:XX:22) {
2927
code: 'ERR_INVALID_URL',
3028
input: '...'
3129
}

src/entrypoints/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from '../layers/1_Schema/__.js'
22
export { Select } from '../layers/2_Select/__.js'
3-
export { ResultSet } from '../layers/3_Result/__.js'
3+
export { InferResult } from '../layers/3_InferResult/__.js'

src/layers/3_Result/infer/SelectAlias.ts renamed to src/layers/3_InferResult/Alias.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { mergeObjectArray, ValuesOrEmptyObject } from '../../../lib/prelude.js'
2-
import type { Schema } from '../../1_Schema/__.js'
3-
import type { Select } from '../../2_Select/__.js'
4-
import type { SchemaIndex } from '../../4_generator/generators/SchemaIndex.js'
5-
import type { InferField } from './Field.js'
1+
import type { mergeObjectArray, ValuesOrEmptyObject } from '../../lib/prelude.js'
2+
import type { Schema } from '../1_Schema/__.js'
3+
import type { Select } from '../2_Select/__.js'
4+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
5+
import type { Field } from './Field.js'
66

77
// dprint-ignore
8-
export type InferSelectionSelectAlias<
8+
export type Alias<
99
$SelectionSet,
1010
$Schema extends SchemaIndex,
1111
$Node extends Schema.Output.Object$2
@@ -28,7 +28,7 @@ export type InferSelectionSelectAlias<
2828
>
2929

3030
// dprint-ignore
31-
export type InferSelectAlias<
31+
type InferSelectAlias<
3232
$SelectAlias extends Select.SelectAlias.SelectAlias,
3333
$FieldName extends string,
3434
$Schema extends SchemaIndex,
@@ -55,5 +55,5 @@ type InferSelectAliasOne<
5555
$Schema extends SchemaIndex,
5656
$Node extends Schema.Output.Object$2,
5757
> = {
58-
[_ in $SelectAliasOne[0]]: InferField<$SelectAliasOne[1], $Node['fields'][$FieldName], $Schema>
58+
[_ in $SelectAliasOne[0]]: Field<$SelectAliasOne[1], $Node['fields'][$FieldName], $Schema>
5959
}
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
import type { Simplify } from 'type-fest'
2-
import type { TSError } from '../../../lib/TSError.js'
3-
import type { Schema } from '../../1_Schema/__.js'
4-
import type { Select } from '../../2_Select/__.js'
5-
import type { SchemaIndex } from '../../4_generator/generators/SchemaIndex.js'
6-
import type { InferInterface, InferObject, InferUnion } from './root.js'
2+
import type { TSError } from '../../lib/TSError.js'
3+
import type { Schema } from '../1_Schema/__.js'
4+
import type { Select } from '../2_Select/__.js'
5+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
6+
import type { Interface } from './Interface.js'
7+
import type { Object } from './Object.js'
8+
import type { Union } from './Union.js'
79

810
// dprint-ignore
9-
export type InferField<$SelectionSet, $Field extends Schema.SomeField, $Schema extends SchemaIndex> =
11+
export type Field<$SelectionSet, $Field extends Schema.SomeField, $Schema extends SchemaIndex> =
1012
Simplify<
1113
$SelectionSet extends Select.Directive.Include.FieldStates.Negative | Select.Directive.Skip.FieldStates.Positive ?
1214
null :
1315
(
1416
| FieldDirectiveInclude<$SelectionSet>
1517
| FieldDirectiveSkip<$SelectionSet>
16-
| InferFieldType<Omit<$SelectionSet, '$'>, $Field['type'], $Schema>
18+
| FieldType<Omit<$SelectionSet, '$'>, $Field['type'], $Schema>
1719
)
1820
>
1921

2022
// dprint-ignore
21-
type InferFieldType<
23+
type FieldType<
2224
$SelectionSet,
2325
$Type extends Schema.Output.Any,
2426
$Schema extends SchemaIndex
2527
> =
2628
$Type extends Schema.__typename<infer $Value> ? $Value :
27-
$Type extends Schema.Output.Nullable<infer $InnerType> ? null | InferFieldType<$SelectionSet, $InnerType, $Schema> :
28-
$Type extends Schema.Output.List<infer $InnerType> ? Array<InferFieldType<$SelectionSet, $InnerType, $Schema>> :
29+
$Type extends Schema.Output.Nullable<infer $InnerType> ? null | FieldType<$SelectionSet, $InnerType, $Schema> :
30+
$Type extends Schema.Output.List<infer $InnerType> ? Array<FieldType<$SelectionSet, $InnerType, $Schema>> :
2931
$Type extends Schema.Enum<infer _, infer $Members> ? $Members[number] :
3032
$Type extends Schema.Scalar.$Any ? ReturnType<$Type['codec']['decode']> :
31-
$Type extends Schema.Object$2 ? InferObject<$SelectionSet, $Schema, $Type> :
32-
$Type extends Schema.Interface ? InferInterface<$SelectionSet, $Schema, $Type> :
33-
$Type extends Schema.Union ? InferUnion<$SelectionSet, $Schema, $Type> :
34-
TSError<'InferFieldType', `Unknown type`, { $Type: $Type; $SelectionSet: $SelectionSet; $Schema:$Schema }>
33+
$Type extends Schema.Object$2 ? Object<$SelectionSet, $Schema, $Type> :
34+
$Type extends Schema.Interface ? Interface<$SelectionSet, $Schema, $Type> :
35+
$Type extends Schema.Union ? Union<$SelectionSet, $Schema, $Type> :
36+
TSError<'FieldType', `Unknown type`, { $Type: $Type; $SelectionSet: $SelectionSet; $Schema:$Schema }>
3537

3638
// dprint-ignore
3739
type FieldDirectiveInclude<$SelectionSet> =
3840
$SelectionSet extends Select.Directive.Include.Field ? $SelectionSet extends Select.Directive.Include.FieldStates.Positive ? never
39-
: null
40-
: never
41+
: null
42+
: never
4143

4244
// dprint-ignore
4345
type FieldDirectiveSkip<$SelectionSet> =
4446
$SelectionSet extends Select.Directive.Skip.Field ? $SelectionSet extends Select.Directive.Skip.FieldStates.Negative ? never
45-
: null
46-
: never
47+
: null
48+
: never
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { type GetKeyOr } from '../../lib/prelude.js'
2+
import type { Schema } from '../1_Schema/__.js'
3+
import type { Select } from '../2_Select/__.js'
4+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
5+
import type { Object } from './Object.js'
6+
7+
// dprint-ignore
8+
export type InlineFragmentTypeConditional<$SelectionSet, $Node extends Schema.Output.Object$2, $Index extends SchemaIndex> =
9+
$Node extends any // force distribution
10+
? Object<
11+
& GetKeyOr<
12+
$SelectionSet,
13+
`${Select.InlineFragment.TypeConditionalKeyPrefix}${$Node['fields']['__typename']['type']['type']}`,
14+
{}
15+
>
16+
& Select.InlineFragment.OmitInlineFragmentsWithTypeConditions<$SelectionSet>,
17+
$Index,
18+
$Node
19+
>
20+
: never

src/layers/3_InferResult/Interface.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Schema } from '../1_Schema/__.js'
2+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
3+
import type { InlineFragmentTypeConditional } from './InlineFragment.js'
4+
5+
// dprint-ignore
6+
export type Interface<$SelectionSet, $Index extends SchemaIndex, $Node extends Schema.Output.Interface> =
7+
InlineFragmentTypeConditional<$SelectionSet, $Node['implementors'][number], $Index>

src/layers/3_InferResult/Object.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { Simplify } from 'type-fest'
2+
import { type StringKeyof } from '../../lib/prelude.js'
3+
import type { TSError } from '../../lib/TSError.js'
4+
import type { Schema } from '../1_Schema/__.js'
5+
import type { Select } from '../2_Select/__.js'
6+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
7+
import type { Alias } from './Alias.js'
8+
import type { Field } from './Field.js'
9+
import type { ScalarsWildcard } from './ScalarsWildcard.js'
10+
11+
// dprint-ignore
12+
export type Object<$SelectionSet, $Schema extends SchemaIndex, $Node extends Schema.Output.Object$2> =
13+
Select.SelectScalarsWildcard.IsSelectScalarsWildcard<$SelectionSet> extends true
14+
// todo what about when scalars wildcard is combined with other fields like relations?
15+
? ScalarsWildcard<$SelectionSet, $Schema,$Node>
16+
:
17+
Simplify<(
18+
& SelectionNonSelectAlias<$SelectionSet, $Schema, $Node>
19+
& Alias<$SelectionSet, $Schema, $Node>
20+
)>
21+
22+
// dprint-ignore
23+
type SelectionNonSelectAlias<$SelectionSet , $Schema extends SchemaIndex, $Node extends Schema.Output.Object$2> =
24+
{
25+
[$Select in PickSelectsPositiveIndicatorAndNotSelectAlias<$SelectionSet>]:
26+
$Select extends keyof $Node['fields']
27+
? Field<$SelectionSet[$Select], $Node['fields'][$Select], $Schema>
28+
: Errors.UnknownFieldName<$Select, $Node>
29+
}
30+
31+
// dprint-ignore
32+
export namespace Errors {
33+
export type UnknownFieldName<$FieldName extends string, $Object extends Schema.Object$2 | Schema.Output.RootType> =
34+
TSError<'Object', `field "${$FieldName}" does not exist on object "${$Object['fields']['__typename']['type']['type']}"`>
35+
}
36+
37+
// dprint-ignore
38+
export type PickSelectsPositiveIndicatorAndNotSelectAlias<$SelectionSet> = StringKeyof<{
39+
[
40+
$FieldName in keyof $SelectionSet as $SelectionSet[$FieldName] extends Select.Indicator.Negative
41+
? never
42+
: $SelectionSet[$FieldName] extends any[]
43+
? never
44+
: $FieldName
45+
]: 0
46+
}>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Schema } from '../1_Schema/__.js'
2+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
3+
import type { Field } from './Field.js'
4+
5+
export type ScalarsWildcard<
6+
$SelectionSet,
7+
$Index extends SchemaIndex,
8+
$Node extends Schema.Output.Object$2,
9+
> = {
10+
[$Key in keyof PickScalarFields<$Node>]: Field<$SelectionSet, $Node['fields'][$Key], $Index>
11+
}
12+
13+
// dprint-ignore
14+
type PickScalarFields<$Object extends Schema.Output.Object$2> = {
15+
[
16+
$Key in keyof $Object['fields']
17+
as Schema.Output.UnwrapToNamed<$Object['fields'][$Key]['type']> extends Schema.Hybrid.Scalar.$Any | Schema.Output.__typename
18+
? $Key
19+
: never
20+
]: $Object['fields'][$Key]
21+
}

src/layers/3_InferResult/Union.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Schema } from '../1_Schema/__.js'
2+
import type { SchemaIndex } from '../4_generator/generators/SchemaIndex.js'
3+
import type { InlineFragmentTypeConditional } from './InlineFragment.js'
4+
5+
// dprint-ignore
6+
export type Union<$SelectionSet, $Index extends SchemaIndex, $Node extends Schema.Output.Union> =
7+
InlineFragmentTypeConditional<$SelectionSet, $Node['members'][number], $Index>

src/layers/3_InferResult/_.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from './Alias.js'
2+
export * from './Field.js'
3+
export * from './Interface.js'
4+
export * from './Object.js'
5+
export * from './root.js'
6+
export * from './ScalarsWildcard.js'
7+
export * from './Union.js'

0 commit comments

Comments
 (0)