Skip to content

Commit

Permalink
refactor: tidy type formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Oct 31, 2024
1 parent 83ebaeb commit 537a170
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import tsEslint from 'typescript-eslint'

export default tsEslint.config({
ignores: [
'playground/**/*',
'graffle/**/*',
'project/**/*',
'examples/35_custom-scalar/custom-scalar.ts',
'eslint.config.js',
Expand Down
34 changes: 19 additions & 15 deletions src/documentBuilder/InferResult/OutputObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,39 @@ import type { Alias } from './Alias.js'
import type { OutputField } from './OutputField.js'
import type { ScalarsWildcard } from './ScalarsWildcard.js'

// dprint-ignore
export type OutputObject<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> =
Select.SelectScalarsWildcard.IsSelectScalarsWildcard<$SelectionSet> extends true
// todo what about when scalars wildcard is combined with other fields like relations?
? ScalarsWildcard<$SelectionSet, $Schema, $Node>
: SimplifyExcept<
$Schema['scalars']['typesDecoded'],
& SelectionNonSelectAlias<$SelectionSet, $Schema, $Node>
& Alias<$Schema, $Node, $SelectionSet>
>
$Schema['scalars']['typesDecoded'],
& NonAlias<$SelectionSet, $Schema, $Node>
& Alias<$Schema, $Node, $SelectionSet>
>

type SelectionNonSelectAlias<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> = {
type NonAlias<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> = {
[$Key in PickSelectsPositiveIndicatorAndNotSelectAlias<$SelectionSet>]: $Key extends keyof $Node['fields']
? OutputField<$SelectionSet[$Key], $Node['fields'][$Key], $Schema>
: Errors.UnknownFieldName<$Key, $Node>
}

export namespace Errors {
export type UnknownFieldName<
$FieldName extends string,
$Object extends Schema.OutputObject,
> = TSErrorDescriptive<'Object', `field "${$FieldName}" does not exist on object "${$Object['name']}"`>
}

// dprint-ignore
export type PickSelectsPositiveIndicatorAndNotSelectAlias<$SelectionSet> = StringKeyof<
{
[
$FieldName in keyof $SelectionSet as $SelectionSet[$FieldName] extends Select.Indicator.Negative ? never
: $SelectionSet[$FieldName] extends any[] ? never
: $FieldName
$FieldName in keyof $SelectionSet as $SelectionSet[$FieldName] extends Select.Indicator.Negative
? never
: $SelectionSet[$FieldName] extends any[]
? never
: $FieldName
]: 0
}
>

export namespace Errors {
export type UnknownFieldName<
$FieldName extends string,
$Object extends Schema.OutputObject,
> = TSErrorDescriptive<'Object', `field "${$FieldName}" does not exist on object "${$Object['name']}"`>
}

0 comments on commit 537a170

Please sign in to comment.