Skip to content

Commit

Permalink
improve(document-builder): deeply simplify results (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Nov 19, 2024
1 parent 8bf1621 commit 21674a5
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 247 deletions.
8 changes: 7 additions & 1 deletion src/client/builderExtensions/scalar.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ Graffle.create({ schema: `foo`, schemaMap }).scalar(`Foo`, _)
// @ts-expect-error "Foo" is not a scalar name in the schema.
Graffle.create({ schema: `foo`, schemaMap }).scalar(FooScalar)
Graffle.create({ schema: `foo`, schemaMap }).scalar(`Date`, _)
Graffle.create({ schema: `foo`, schemaMap }).scalar(DateScalar)
Graffle.create({ schema: `foo`, schemaMap }).scalar(`Int`, _)
{
const graffle = Graffle.create({ schema: `foo`, schemaMap }).scalar(DateScalar)
assertEqual<
typeof graffle._.scalars,
{ typesDecoded: Date; typesEncoded: string; map: { Date: typeof DateScalar } }
>()
}
5 changes: 4 additions & 1 deletion src/client/builderExtensions/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ type ScalarMethod<$Args extends Builder.Extension.Parameters<BuilderExtensionSca
$Args['context'],
['scalars'],
Simplify<
Schema.Scalar.Registry.AddScalar<$Args['context']['scalars'], Schema.Scalar<$Name, $Decoded, string>>
Schema.Scalar.Registry.AddScalar<
$Args['context']['scalars'],
Schema.Scalar<$Name, $Decoded, string>
>
>
>
>
Expand Down
5 changes: 2 additions & 3 deletions src/documentBuilder/InferResult/OutputField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { SimplifyNullable } from '../../lib/prelude.js'
import type { TSErrorDescriptive } from '../../lib/ts-error.js'
import type { Schema } from '../../types/Schema/__.js'
import type { InlineType } from '../../types/SchemaDrivenDataMap/InlineType.js'
Expand All @@ -15,12 +14,12 @@ export type OutputField<$SelectionSet, $Field extends Schema.OutputField, $Schem
: (
| FieldDirectiveInclude<$SelectionSet>
| FieldDirectiveSkip<$SelectionSet>
| SimplifyNullable<
| //SimplifyNullable<
InlineType.Infer<
$Field['inlineType'],
FieldType<$Schema, Omit<$SelectionSet, '$'>, $Field['namedType']>
>
>
//>
)

// dprint-ignore
Expand Down
14 changes: 7 additions & 7 deletions src/documentBuilder/InferResult/OutputObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IsNever, Simplify } from 'type-fest'
import type { IsNever } from 'type-fest'
import { assertEqual } from '../../lib/assert-equal.js'
import type { AssertExtendsObject, GetOrNever, SimplifyExcept, StringKeyof } from '../../lib/prelude.js'
import type { AssertExtendsObject, GetOrNever, StringKeyof } from '../../lib/prelude.js'
import type { TSErrorDescriptive } from '../../lib/ts-error.js'
import type { Schema } from '../../types/Schema/__.js'
import type { Select } from '../Select/__.js'
Expand All @@ -15,27 +15,27 @@ export type OutputObject<
$Schema extends Schema,
$Node extends Schema.OutputObject
> =
SimplifyExcept<
$Schema['scalars']['typesDecoded'],
// SimplifyExcept<
// $Schema['scalars']['typesDecoded'],
& OutputObject_<$SelectionSet, $Schema, $Node>
& InlineFragmentKeys<$SelectionSet, $Schema, $Node>
>
// >

// dprint-ignore
type OutputObject_<
$SelectionSet extends object,
$Schema extends Schema,
$Node extends Schema.OutputObject,
> =
Simplify<
// Simplify<
Select.SelectScalarsWildcard.IsSelectScalarsWildcard<$SelectionSet> extends true
// todo this needs to be an extension and/or only available when sddm is present
// todo what about when scalars wildcard is combined with other fields like relations?
? ScalarsWildcard<$SelectionSet, $Schema, $Node>
:
& NonAliasKeys<$SelectionSet, $Schema, $Node>
& Alias<$Schema, $Node, $SelectionSet>
>
// >

// dprint-ignore
type NonAliasKeys<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> = {
Expand Down
4 changes: 2 additions & 2 deletions src/documentBuilder/InferResult/__.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Simplify } from 'type-fest'
import type { DateScalar } from '../../../tests/_/fixtures/scalars.js'
import type { db } from '../../../tests/_/schemas/db.js'
import type { Schema } from '../../../tests/_/schemas/kitchen-sink/graffle/modules/schema.js'
import type * as SelectionSets from '../../../tests/_/schemas/kitchen-sink/graffle/modules/selection-sets.js'
import { assertEqual } from '../../lib/assert-equal.js'
import type { SimplifyDeep } from '../../lib/prelude.js'
import type { Registry } from '../../types/Schema/nodes/Scalar/helpers.js'
import type { InferResult } from './__.js'

type $<$SelectionSet extends SelectionSets.Query> = Simplify<InferResult.OperationQuery<$SelectionSet, Schema>>
type $<$SelectionSet extends SelectionSets.Query> = SimplifyDeep<InferResult.OperationQuery<$SelectionSet, Schema>>

type $Registry = Registry.AddScalar<Registry.Empty, typeof DateScalar>

Expand Down
7 changes: 4 additions & 3 deletions src/documentBuilder/requestMethods/document.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Simplify, UnionToTuple } from 'type-fest'
import type { UnionToTuple } from 'type-fest'
import type { Context } from '../../client/context.js'
import { type HandleOutput } from '../../client/handleOutput.js'
import type { InferResult } from '../../documentBuilder/InferResult/__.js'
import type { Select } from '../../documentBuilder/Select/__.js'
import type { Schema } from '../../entrypoints/schema.js'
import type { IsTupleMultiple } from '../../lib/prelude.js'
import type { IsTupleMultiple, SimplifyDeepExcept } from '../../lib/prelude.js'

// dprint-ignore
export type DocumentRunner<
Expand All @@ -18,7 +18,8 @@ export type DocumentRunner<
const $Name extends string = $Params extends [] ? $$Name : $Params[0],
>(...params: $Params) =>
Promise<
Simplify<
SimplifyDeepExcept<
$$Context['scalars']['typesDecoded'],
HandleOutput<
$$Context,
InferResult.Operation<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Schema from '../../../tests/_/schemas/kitchen-sink/schema.js'
// import { Graffle as Pokemon } from '../../../tests/_/schemas/pokemon/graffle/__.js'

const graffle = Graffle.create({ schema: Schema.schema }).scalar(DateScalar)
graffle._.scalars.typesDecoded

// dprint-ignore
test(`query`, async () => {
Expand All @@ -21,7 +22,6 @@ test(`query`, async () => {
expectTypeOf<Parameters<typeof graffle.query.stringWithRequiredArg>>().toEqualTypeOf<[input: Graffle.SelectionSets.Query.stringWithRequiredArg]>()
// scalar custom
expectTypeOf(await graffle.query.date()).toMatchTypeOf<Date | null>()

// scalar with explicit indicators
// positive indicator
expectTypeOf(await graffle.query.idNonNull(true)).toEqualTypeOf<string>()
Expand Down
2 changes: 2 additions & 0 deletions src/entrypoints/utilities-for-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export {
type AssertExtendsObject,
type Exact,
type ExactNonEmpty,
type SimplifyDeep,
type SimplifyDeepExcept,
type SimplifyExcept,
type UnionExpanded,
} from '../lib/prelude.js'
Expand Down
Loading

0 comments on commit 21674a5

Please sign in to comment.