Skip to content

Commit

Permalink
refactor: remove schema layer funcs (#1202)
Browse files Browse the repository at this point in the history
* refactor: remove schema layer funcs

* lint
  • Loading branch information
jasonkuhrt authored Oct 19, 2024
1 parent 64dc9d5 commit f44b086
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 131 deletions.
9 changes: 0 additions & 9 deletions src/layers/1_Schema/Hybrid/types/Enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,3 @@ export interface Enum<
name: $Name
members: $Members
}

export const Enum = <$Name extends string, const $Members extends [string, ...string[]]>(
name: $Name,
members: $Members,
): Enum<$Name, $Members> => ({
kind: `Enum`,
name,
members,
})
20 changes: 0 additions & 20 deletions src/layers/1_Schema/Input/Args.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import type { Input } from './__.js'

type InputFields = Record<string, any>

export interface Args<$InputFields extends InputFields, $IsFieldsAllNullable extends boolean = boolean> {
fields: $InputFields
isFieldsAllNullable: $IsFieldsAllNullable
}

export const Args = <$InputFields extends InputFields, const $IsInputFieldsAllNullable extends boolean>(
fields: $InputFields,
isFieldsAllNullable: $IsInputFieldsAllNullable,
): Args<$InputFields, $IsInputFieldsAllNullable> => {
return {
fields,
isFieldsAllNullable,
}
}

export type OmitNullableFields<$Fields extends InputFields> = {
[Key in keyof $Fields as $Fields[Key]['type'] extends Input.Nullable<any> ? never : Key]: $Fields[Key]
}

export type PickNullableFields<$Fields extends InputFields> = {
[Key in keyof $Fields as $Fields[Key]['type'] extends Input.Nullable<any> ? Key : never]: $Fields[Key]
}
17 changes: 0 additions & 17 deletions src/layers/1_Schema/Input/types/InputObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,3 @@ export interface InputObject<
fields: $Fields
isAllFieldsNullable: $IsFieldsAllNullable
}

export const InputObject = <
$Name extends string,
$Fields extends Record<keyof $Fields, any>,
const $IsFieldsAllNullable extends boolean,
>(
name: $Name,
fields: $Fields,
isAllFieldsNullable: $IsFieldsAllNullable,
): InputObject<$Name, $Fields, $IsFieldsAllNullable> => ({
kind: `InputObject`,
name: name,
fields: {
...fields,
},
isAllFieldsNullable,
})
5 changes: 0 additions & 5 deletions src/layers/1_Schema/Input/types/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ import type { Base } from '../../core/helpers.js'
import type { Any } from '../typeGroups.js'

export type List<$InnerType extends Any> = Base.List<$InnerType>

export const List = <$InnerType extends Any>(type: $InnerType): List<$InnerType> => ({
kind: `list`,
type,
})
8 changes: 1 addition & 7 deletions src/layers/1_Schema/Input/types/Nullable.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type { Base, MaybeThunk } from '../../core/helpers.js'
import type { Base } from '../../core/helpers.js'
import type { Any, Named } from '../typeGroups.js'
import type { List } from './List.js'

type InnerType = Named | List<any>

export type Nullable<$InnerType extends InnerType> = Base.Nullable<$InnerType>

export const Nullable = <$InnerType extends InnerType>(type: MaybeThunk<$InnerType>): Nullable<$InnerType> => ({
kind: `nullable`,
// at type level "type" is not a thunk
type: type as any,
})

// dprint-ignore
type UnwrapNullable<$Type> =
$Type extends Nullable<infer $innerType> ? UnwrapNullable<$innerType>
Expand Down
13 changes: 1 addition & 12 deletions src/layers/1_Schema/Output/Output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TSErrorDescriptive } from '../../../lib/ts-error.js'
import { readMaybeThunk } from '../core/helpers.js'
import type { Any, Named } from './typeGroups.js'
import type { Named } from './typeGroups.js'
import type { __typename } from './types/__typename.js'
import type { List } from './types/List.js'
import type { Nullable } from './types/Nullable.js'
Expand All @@ -26,13 +25,3 @@ export type Unwrap<$Type extends any> =
export type UnwrapNullable<$Type> =
$Type extends Nullable<infer $innerType> ? UnwrapNullable<$innerType>
: $Type

export const unwrapNullable = <$Type extends Any>(type: $Type): UnwrapNullable<$Type> => {
if (type.kind === `nullable`) return type.type
return type as UnwrapNullable<$Type>
}

export const unwrapToNamed = <$Type extends Any>(type: $Type): Unwrap<$Type> => {
// @ts-expect-error fixme
return type.kind === `list` || type.kind === `nullable` ? unwrapToNamed(readMaybeThunk(type).type) : type
}
4 changes: 0 additions & 4 deletions src/layers/1_Schema/Output/typeGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import type { Union } from './types/Union.js'

export type ObjectLike = Object$2 | Interface

export const isObjectLike = (type: Any): type is ObjectLike => {
return type.kind === `Object` || type.kind === `Interface` || type.kind === `Union`
}

export type Named = Interface | Enum | Object$2 | Union<string, [Object$2, ...Object$2[]]> | Hybrid.Scalar.$Any

export type Unnamed = List<any> | Nullable<any>
Expand Down
16 changes: 1 addition & 15 deletions src/layers/1_Schema/Output/types/Field.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import type { MaybeThunk } from '../../core/helpers.js'
import type { Hybrid } from '../../Hybrid/__.js'
import type { Args } from '../../Input/Args.js'
import type { Output } from '../__.js'

export type Field<$Name extends string, $Type extends Output.Any, $Args extends Args<any> | null> = {
// todo when generating schema keep track of the unwrapped type too to avoid IDE runtime cost to calcualte it
// todo when generating schema keep track of the unwrapped type too to avoid IDE runtime cost to calculate it
// typeUnwrapped: $NamedType
type: $Type
name: $Name
args: $Args
}

export const field = <$Name extends string, $Type extends Output.Any, $Args extends null | Args<any> = null>(
name: $Name,
type: MaybeThunk<$Type>,
args: $Args = null as $Args,
): Field<$Name, $Type, $Args> => {
return {
// At type level "type" is not a thunk
type: type as any,
name,
args,
}
}

type FieldType =
| Hybrid.Enum
| Hybrid.Scalar.$Any
Expand Down
18 changes: 0 additions & 18 deletions src/layers/1_Schema/Output/types/Interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import type { SomeFields } from '../../Field.js'
import type { Object$2 } from './Object.js'

export type Interface<
Expand All @@ -12,20 +11,3 @@ export type Interface<
fields: $Fields
implementors: $Implementors
}

export const Interface = <
$Name extends string,
$Fields extends any,
// todo more accurate constraint but leads to incorrectly inferred types.
// $Fields extends SomeFields,
$Implementors extends [Object$2, ...Object$2[]],
>(
name: $Name,
fields: $Fields,
implementors: $Implementors,
): Interface<$Name, $Fields, $Implementors> => ({
kind: `Interface`,
name,
fields,
implementors,
})
19 changes: 1 addition & 18 deletions src/layers/1_Schema/Output/types/Object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Grafaid } from '../../../../lib/grafaid/__.js'
import { __typename } from './__typename.js'
import type { __typename } from './__typename.js'
import type { Field, SomeFields } from './Field.js'
import { field } from './Field.js'

export interface ObjectQuery<
$Fields extends SomeFields = SomeFields,
Expand All @@ -26,19 +25,3 @@ export interface Object$2<
__typename: Field<'__typename', __typename<$Name>, null>
} & $Fields
}

// Naming this "Object" breaks Vitest: https://github.com/vitest-dev/vitest/issues/5463
export const Object$ = <$Name extends string, $Fields extends Record<keyof $Fields, Field<any, any, any>>>(
name: $Name,
fields: $Fields,
// eslint-disable-next-line
// @ts-ignore infinite depth issue
): Object$2<$Name, $Fields> => ({
kind: `Object`,
fields: {
__typename: field(`__typename`, __typename(name)),
...fields,
},
})

export { Object$ as Object }
4 changes: 2 additions & 2 deletions src/layers/1_Schema/_.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { readMaybeThunk, type RootTypeName } from './core/helpers.js'
export { type RootTypeName } from './core/helpers.js'
export * from './core/Named/__.js'
export * as Directives from './Directives.js'
export * from './Hybrid/__.js'
Expand All @@ -11,5 +11,5 @@ export * from './Output/__.js'
export * from './Output/types/__typename.js'
export * from './Output/types/Field.js'
export * from './Output/types/Interface.js'
export { Object$, type Object$2 } from './Output/types/Object.js'
export { type Object$2 } from './Output/types/Object.js'
export * from './Output/types/Union.js'
4 changes: 0 additions & 4 deletions src/layers/1_Schema/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ export type MaybeThunk<$Type> = $Type | Thunk<$Type>

export type Thunk<$Type> = () => $Type

export const readMaybeThunk = <T>(maybeThunk: MaybeThunk<T>): T =>
// @ts-expect-error fixme
typeof maybeThunk === `function` ? maybeThunk() : maybeThunk

export namespace Base {
export interface Nullable<$Type> {
kind: 'nullable'
Expand Down

0 comments on commit f44b086

Please sign in to comment.