Skip to content

Commit 2705620

Browse files
authored
improve(custom-scalars): no expand inferred result type (#1211)
1 parent 245a10a commit 2705620

File tree

53 files changed

+3733
-2714
lines changed

Some content is hidden

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

53 files changed

+3733
-2714
lines changed

examples/__outputs__/10_transport-http/transport-http_extension_headers__dynamicHeaders.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
headers: Headers {
55
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
66
'content-type': 'application/json',
7-
'x-sent-at-time': '1729654429348'
7+
'x-sent-at-time': '1729695636061'
88
},
99
signal: undefined,
1010
method: 'post',

examples/__outputs__/20_output/output_envelope.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
headers: Headers {
1919
'content-type': 'application/graphql-response+json; charset=utf-8',
2020
'content-length': '142',
21-
date: 'Wed, 23 Oct 2024 03:33:49 GMT',
21+
date: 'Wed, 23 Oct 2024 15:00:36 GMT',
2222
connection: 'keep-alive',
2323
'keep-alive': 'timeout=5'
2424
},

examples/__outputs__/35_custom-scalar/custom-scalar.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---------------------------------------- SHOW ----------------------------------------
2-
pokemons[0].birthday instanceof Date = true
2+
true
33
---------------------------------------- SHOW ----------------------------------------
44
[
55
{ name: 'Pikachu', birthday: 1850-01-01T00:00:00.000Z },

examples/__outputs__/60_extension/extension_opentelemetry__opentelemetry.output.txt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
}
1010
},
1111
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
12-
traceId: '4db92619c2c83794eea58ac3b0f81739',
13-
parentId: '0441c81c457852b2',
12+
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
13+
parentId: '996dbc7722cae52f',
1414
traceState: undefined,
1515
name: 'encode',
16-
id: '0fed2ea32096273f',
16+
id: '013ccc636ba5ab5a',
1717
kind: 0,
18-
timestamp: 1729654429675000,
19-
duration: 2681.792,
18+
timestamp: 1729695637401000,
19+
duration: 1347.708,
2020
attributes: {},
2121
status: { code: 0 },
2222
events: [],
@@ -33,14 +33,14 @@
3333
}
3434
},
3535
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
36-
traceId: '4db92619c2c83794eea58ac3b0f81739',
37-
parentId: '0441c81c457852b2',
36+
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
37+
parentId: '996dbc7722cae52f',
3838
traceState: undefined,
3939
name: 'pack',
40-
id: 'ca490dca8e6606c9',
40+
id: '9f77c7eb28b781b8',
4141
kind: 0,
42-
timestamp: 1729654429680000,
43-
duration: 48326.792,
42+
timestamp: 1729695637404000,
43+
duration: 11403.833,
4444
attributes: {},
4545
status: { code: 0 },
4646
events: [],
@@ -57,14 +57,14 @@
5757
}
5858
},
5959
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
60-
traceId: '4db92619c2c83794eea58ac3b0f81739',
61-
parentId: '0441c81c457852b2',
60+
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
61+
parentId: '996dbc7722cae52f',
6262
traceState: undefined,
6363
name: 'exchange',
64-
id: '11066eaa0e07c122',
64+
id: '37e8810021b4127d',
6565
kind: 0,
66-
timestamp: 1729654429729000,
67-
duration: 47351.292,
66+
timestamp: 1729695637416000,
67+
duration: 28133.5,
6868
attributes: {},
6969
status: { code: 0 },
7070
events: [],
@@ -81,14 +81,14 @@
8181
}
8282
},
8383
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
84-
traceId: '4db92619c2c83794eea58ac3b0f81739',
85-
parentId: '0441c81c457852b2',
84+
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
85+
parentId: '996dbc7722cae52f',
8686
traceState: undefined,
8787
name: 'unpack',
88-
id: 'c527bbb4b62958fa',
88+
id: '87bd4d2329beb31f',
8989
kind: 0,
90-
timestamp: 1729654429777000,
91-
duration: 1775.75,
90+
timestamp: 1729695637445000,
91+
duration: 1657.667,
9292
attributes: {},
9393
status: { code: 0 },
9494
events: [],
@@ -105,14 +105,14 @@
105105
}
106106
},
107107
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
108-
traceId: '4db92619c2c83794eea58ac3b0f81739',
109-
parentId: '0441c81c457852b2',
108+
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
109+
parentId: '996dbc7722cae52f',
110110
traceState: undefined,
111111
name: 'decode',
112-
id: '20eb54a71997e91e',
112+
id: 'e8b9d88dc53ff408',
113113
kind: 0,
114-
timestamp: 1729654429779000,
115-
duration: 669.25,
114+
timestamp: 1729695637447000,
115+
duration: 214.875,
116116
attributes: {},
117117
status: { code: 0 },
118118
events: [],
@@ -129,14 +129,14 @@
129129
}
130130
},
131131
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
132-
traceId: '4db92619c2c83794eea58ac3b0f81739',
132+
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
133133
parentId: undefined,
134134
traceState: undefined,
135135
name: 'request',
136-
id: '0441c81c457852b2',
136+
id: '996dbc7722cae52f',
137137
kind: 0,
138-
timestamp: 1729654429631000,
139-
duration: 148602.75,
138+
timestamp: 1729695637401000,
139+
duration: 46241.5,
140140
attributes: {},
141141
status: { code: 0 },
142142
events: [],

src/documentBuilder/InferResult/InlineFragment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { type GetKeyOr } from '../../lib/prelude.js'
22
import type { Schema } from '../../types/Schema/__.js'
33
import type { Select } from '../Select/__.js'
4-
import type { Object } from './OutputObject.js'
4+
import type { OutputObject } from './OutputObject.js'
55

66
// dprint-ignore
77
export type InlineFragmentTypeConditional<$SelectionSet, $Node extends Schema.OutputObject, $Schema extends Schema> =
88
$Node extends any // force distribution
9-
? Object<
9+
? OutputObject<
1010
& GetKeyOr<
1111
$SelectionSet,
1212
`${Select.InlineFragment.TypeConditionalKeyPrefix}${$Node['name']}`,

src/documentBuilder/InferResult/OutputField.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
import type { Simplify } from 'type-fest'
1+
import type { SimplifyNullable } from '../../lib/prelude.js'
22
import type { TSErrorDescriptive } from '../../lib/ts-error.js'
33
import type { Schema } from '../../types/Schema/__.js'
44
import type { InlineType } from '../../types/SchemaDrivenDataMap/InlineType.js'
55
import type { Select } from '../Select/__.js'
66
import type { Interface } from './Interface.js'
7-
import type { Object } from './OutputObject.js'
7+
import type { OutputObject } from './OutputObject.js'
88
import type { Union } from './Union.js'
99

1010
// dprint-ignore
1111
export type OutputField<$SelectionSet, $Field extends Schema.OutputField, $Schema extends Schema> =
12-
Simplify<
13-
$SelectionSet extends Select.Directive.Include.FieldStates.Negative | Select.Directive.Skip.FieldStates.Positive ?
14-
null :
15-
(
16-
| FieldDirectiveInclude<$SelectionSet>
17-
| FieldDirectiveSkip<$SelectionSet>
18-
| InlineType.Infer<
12+
$SelectionSet extends Select.Directive.Include.FieldStates.Negative | Select.Directive.Skip.FieldStates.Positive ?
13+
null :
14+
(
15+
| FieldDirectiveInclude<$SelectionSet>
16+
| FieldDirectiveSkip<$SelectionSet>
17+
| SimplifyNullable<
18+
InlineType.Infer<
1919
$Field['inlineType'],
2020
FieldType<$Schema, Omit<$SelectionSet, '$'>, $Field['namedType']>
2121
>
22-
)
23-
>
22+
>
23+
)
2424

2525
// dprint-ignore
2626
type FieldType<
2727
$Schema extends Schema,
2828
$SelectionSet,
29-
$Type extends Schema.NamedOutputTypes,
29+
$Node extends Schema.NamedOutputTypes,
3030
> =
31-
$Type extends Schema.OutputObject ? Object<$SelectionSet, $Schema, $Type> :
32-
$Type extends Schema.Scalar ? ReturnType<$Type['codec']['decode']> : // TODO use TS compiler API to extract this type at build time.
33-
$Type extends Schema.Scalar.ScalarCodecless ? ReturnType<GetCodecForCodecless<$Type, $Schema>['codec']['decode']> :
34-
$Type extends Schema.__typename ? $Type['value'] :
35-
$Type extends Schema.Enum ? $Type['membersUnion'] :
36-
$Type extends Schema.Interface ? Interface<$SelectionSet, $Schema, $Type> :
37-
$Type extends Schema.Union ? Union<$SelectionSet, $Schema, $Type> :
38-
TSErrorDescriptive<'FieldType', `Unknown type`, { $Type: $Type; $SelectionSet: $SelectionSet; $Schema:$Schema }>
31+
$Node extends Schema.OutputObject ? OutputObject<$SelectionSet, $Schema, $Node> :
32+
$Node extends Schema.Scalar ? Schema.Scalar.GetDecoded<$Node> : // TODO use TS compiler API to extract this type at build time.
33+
$Node extends Schema.Scalar.ScalarCodecless ? Schema.Scalar.GetDecoded<GetCodecForCodecless<$Schema, $Node>> :
34+
$Node extends Schema.__typename ? $Node['value'] :
35+
$Node extends Schema.Enum ? $Node['membersUnion'] :
36+
$Node extends Schema.Interface ? Interface<$SelectionSet, $Schema, $Node> :
37+
$Node extends Schema.Union ? Union<$SelectionSet, $Schema, $Node> :
38+
TSErrorDescriptive<'FieldType', `Unknown type`, { $Type: $Node; $SelectionSet: $SelectionSet; $Schema:$Schema }>
3939

4040
// dprint-ignore
4141
type GetCodecForCodecless<
42-
$Type extends Schema.Scalar.ScalarCodecless,
43-
$Schema extends Schema
42+
$Schema extends Schema,
43+
$Node extends Schema.Scalar.ScalarCodecless
4444
> =
45-
$Type['name'] extends keyof $Schema['scalars']
46-
? $Schema['scalars'][$Type['name']]
45+
$Node['name'] extends keyof $Schema['scalars']['map']
46+
? $Schema['scalars']['map'][$Node['name']]
4747
: Schema.Scalar.String
4848

4949
// dprint-ignore

src/documentBuilder/InferResult/OutputObject.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Simplify } from 'type-fest'
1+
import type { ConditionalSimplify } from 'type-fest/source/conditional-simplify.js'
22
import type { StringKeyof } from '../../lib/prelude.js'
33
import type { TSErrorDescriptive } from '../../lib/ts-error.js'
44
import type { Schema } from '../../types/Schema/__.js'
@@ -7,13 +7,14 @@ import type { Alias } from './Alias.js'
77
import type { OutputField } from './OutputField.js'
88
import type { ScalarsWildcard } from './ScalarsWildcard.js'
99

10-
export type Object<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> =
10+
export type OutputObject<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> =
1111
Select.SelectScalarsWildcard.IsSelectScalarsWildcard<$SelectionSet> extends true
1212
// todo what about when scalars wildcard is combined with other fields like relations?
1313
? ScalarsWildcard<$SelectionSet, $Schema, $Node>
14-
: Simplify<
14+
: ConditionalSimplify<
1515
& SelectionNonSelectAlias<$SelectionSet, $Schema, $Node>
16-
& Alias<$Schema, $Node, $SelectionSet>
16+
& Alias<$Schema, $Node, $SelectionSet>,
17+
$Schema['scalars']['typesDecoded']
1718
>
1819

1920
type SelectionNonSelectAlias<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> = {

src/documentBuilder/InferResult/__.test-d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import type { db } from '../../../tests/_/schemas/db.js'
33
import type { Schema } from '../../../tests/_/schemas/kitchen-sink/graffle/modules/Schema.js'
44
import type * as SelectionSets from '../../../tests/_/schemas/kitchen-sink/graffle/modules/SelectionSets.js'
55
import { assertEqual } from '../../lib/assert-equal.js'
6+
import type { Registry } from '../../types/Schema/nodes/Scalar/helpers.js'
67
import type { InferResult } from './__.js'
78
import type { PickSelectsPositiveIndicatorAndNotSelectAlias } from './OutputObject.js'
89

910
type $<$SelectionSet extends SelectionSets.Query> = InferResult.Query<$SelectionSet, Schema>
1011

11-
type $WithDate<$SelectionSet extends SelectionSets.Query<{ Date: typeof Date }>> = InferResult.Query<
12+
type $Registry = Registry.AddScalar<Registry.Empty, typeof Date>
13+
14+
type $WithDate<$SelectionSet extends SelectionSets.Query<$Registry>> = InferResult.Query<
1215
$SelectionSet,
13-
Schema<{ Date: typeof Date }>
16+
Schema<$Registry>
1417
>
1518

1619
// dprint-ignore

src/documentBuilder/InferResult/root.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Grafaid } from '../../lib/grafaid/__.js'
22
import { type ExcludeNull } from '../../lib/prelude.js'
33
import type { Schema } from '../../types/Schema/__.js'
4-
import type { Object } from './OutputObject.js'
4+
import type { OutputObject } from './OutputObject.js'
55

66
export type RootViaObject<
77
$SelectionSet,
@@ -29,4 +29,4 @@ export type Root<
2929
$SelectionSet,
3030
$Schema extends Schema,
3131
$RootTypeName extends Grafaid.Schema.RootTypeName,
32-
> = Object<$SelectionSet, $Schema, ExcludeNull<$Schema['Root'][$RootTypeName]>>
32+
> = OutputObject<$SelectionSet, $Schema, ExcludeNull<$Schema['Root'][$RootTypeName]>>

src/documentBuilder/SelectGraphQLMapper/toGraphQL.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ type CasesDescriptiveQuery = [
1515
]
1616
const testEachQueryWithDescription = test.for.bind(test)<CasesDescriptiveQuery>
1717

18+
type QueryWithDate = SelectionSets.Query<Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof Date>>
19+
1820
type CasesDescriptiveQueryWithCustomScalars = [
1921
description: string,
20-
selectionSet: SelectionSets.Query<{ Date: typeof Date }>,
22+
selectionSet: QueryWithDate,
2123
options?: { operationName?: string },
2224
]
2325
const testEachQueryWithDescriptionWithCustomScalars = test.for.bind(test)<CasesDescriptiveQueryWithCustomScalars>

0 commit comments

Comments
 (0)