Skip to content

Commit

Permalink
refactor: rename index to schema (#1201)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 18, 2024
1 parent 1752d75 commit 64dc9d5
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/extensions/SchemaErrors/SchemaErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ interface OnRequestResult<$Params extends Extension.Hooks.OnRequestResult.Params
[$Key in keyof ExcludeNullAndUndefined<$Params['result']['data']>]:
Exclude<
ExcludeNullAndUndefined<$Params['result']['data']>[$Key],
{ __typename: $Params['registeredSchema']['index']['extensions']['SchemaErrors']['objectNames'] }
{ __typename: $Params['registeredSchema']['schema']['extensions']['SchemaErrors']['objectNames'] }
>
}
} & Omit<$Params['result'], 'data'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare global {
export interface Schemas {
GraffleSchemaErrors: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
2 changes: 1 addition & 1 deletion src/layers/1_Schema/Hybrid/types/Scalar/Scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ export type $Any =
| Boolean
| ID
| Float
| Values<GlobalRegistry.Schemas[keyof GlobalRegistry.Schemas]['customScalars']>
| Values<GlobalRegistry.Clients[keyof GlobalRegistry.Clients]['customScalars']>

type Values<T> = T extends any ? keyof T extends never ? never : T[keyof T] : never
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ declare global {
export interface Schemas {
default: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
14 changes: 6 additions & 8 deletions src/layers/4_generator/generators/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Code } from '../../../lib/Code.js'
import { identifiers } from '../helpers/identifiers.js'
import { createModuleGenerator } from '../helpers/moduleGenerator.js'
import { ModuleGeneratorData } from './Data.js'
import { ModuleGeneratorMethodsDocument } from './MethodsDocument.js'
Expand All @@ -10,16 +11,12 @@ import { ModuleGeneratorSchema } from './Schema.js'
export const ModuleGeneratorGlobal = createModuleGenerator(
`Global`,
({ config, code }) => {
const identifiers = {
Scalar: `Scalar`,
}

code(
`import type * as Data from './${ModuleGeneratorData.name}.js'`,
`import type * as MethodsSelect from './${ModuleGeneratorMethodsSelect.name}.js'`,
`import type * as MethodsDocument from './${ModuleGeneratorMethodsDocument.name}.js'`,
`import type * as MethodsRoot from './${ModuleGeneratorMethodsRoot.name}.js'`,
`import type { Schema } from './${ModuleGeneratorSchema.name}.js'`,
`import type { ${identifiers.Schema} } from './${ModuleGeneratorSchema.name}.js'`,
)

if (config.schema.kindMap.GraphQLScalarTypeCustom.length > 0) {
Expand All @@ -34,10 +31,10 @@ export const ModuleGeneratorGlobal = createModuleGenerator(
const customScalarsProperties = config.schema.kindMap.GraphQLScalarTypeCustom
.map((_) => [_.name, `${identifiers.Scalar}.${_.name}`])

const SchemasFields = Code.termObjectFields({
const ClientFields = Code.termObjectFields({
[config.name]: {
name: `Data.Name`,
index: `Schema`,
schema: identifiers.Schema,
interfaces: {
MethodsSelect: `MethodsSelect.$MethodsSelect`,
Document: `MethodsDocument.BuilderMethodsDocumentFn`,
Expand All @@ -51,11 +48,12 @@ export const ModuleGeneratorGlobal = createModuleGenerator(
},
})

// todo rename "Schemas" to "Clients"
code(`
declare global {
export namespace GraffleGlobal {
export interface Schemas {
${SchemasFields}
${ClientFields}
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/layers/4_generator/globalRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ declare global {
}
}

type ZeroSchema = {
interface ZeroClient extends GlobalRegistry.RegisteredSchema {
name: GlobalRegistry.DefaultSchemaName
index: { name: never }
// featureOptions: {}
schema: Schema
interfaces: {
Root: TypeFunction.Fn
Document: TypeFunction.Fn
MethodsSelect: {}
}
defaultSchemaUrl: null
Expand All @@ -37,7 +38,7 @@ export namespace GlobalRegistry {

export interface RegisteredSchema<$Extensions extends Extensions = Extensions> {
name: string
index: Schema<$Extensions['Schema']>
schema: Schema<$Extensions['Schema']>
interfaces: {
Root: TypeFunction.Fn
Document: TypeFunction.Fn
Expand All @@ -51,29 +52,29 @@ export namespace GlobalRegistry {

export type DefaultSchemaName = 'default'

export type Schemas = GraffleGlobal.Schemas
export type Clients = GraffleGlobal.Schemas // todo rename to Clients

export type IsEmpty = keyof Schemas extends never ? true : false
export type IsEmpty = keyof Clients extends never ? true : false

export type SchemaUnion = IsEmpty extends true ? ZeroSchema : Values<Schemas>
export type ClientUnion = IsEmpty extends true ? ZeroClient : Values<Clients>

export type SchemaNames = keyof GraffleGlobal.Schemas extends never
? TSErrorDescriptive<'SchemaNames', 'No schemas have been registered. Did you run graffle generate?'>
: keyof GraffleGlobal.Schemas

// dprint-ignore
export type HasDefaultUrlForSchema<$Schema extends SchemaUnion> =
export type HasDefaultUrlForSchema<$Schema extends ClientUnion> =
$Schema['defaultSchemaUrl'] extends null
? false
: true

// eslint-disable-next-line
// @ts-ignore passes after generation
export type GetSchemaIndex<$Name extends SchemaNames> = GraffleGlobal.Schemas[$Name]['index']
export type GetSchema<$Name extends SchemaNames> = GraffleGlobal.Schemas[$Name]['schema']

// eslint-disable-next-line
// @ts-ignore passes after generation
export type SchemaIndexDefault = GetSchemaIndex<DefaultSchemaName>
export type SchemaDefault = GetSchema<DefaultSchemaName>

// dprint-ignore
export type GetOrDefault<$Name extends SchemaNames | undefined> =
Expand All @@ -86,8 +87,8 @@ export namespace GlobalRegistry {
: GraffleGlobal.Schemas[DefaultSchemaName]

// dprint-ignore
export type GetSchemaIndexOrDefault<$Name extends SchemaNames | undefined> =
export type GetSchemaOrDefault<$Name extends SchemaNames | undefined> =
$Name extends SchemaNames
? GetSchemaIndex<$Name>
: SchemaIndexDefault
? GetSchema<$Name>
: SchemaDefault
}
1 change: 1 addition & 0 deletions src/layers/4_generator/helpers/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const identifiers = {
Schema: `Schema`,
Utilities: `Utilities`,
MethodsRoot: `MethodsRoot`,
Scalar: `Scalar`,
}
2 changes: 1 addition & 1 deletion src/layers/5_select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export const create: Create = (_name) => identityProxy as any
// todo is an any type
// eslint-disable-next-line
// @ts-ignore generated types
export const select: TypeSelectionSets<GlobalRegistry.SchemaIndexDefault> = identityProxy
export const select: TypeSelectionSets<GlobalRegistry.SchemaDefault> = identityProxy
16 changes: 8 additions & 8 deletions src/layers/6_client/Settings/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export type InputOutputEnvelopeLonghand = {
/**
* @remarks This input extends base with properties that can be filled with exports from the generated client.
*/
export type InputStatic<$Schema extends GlobalRegistry.SchemaUnion = GlobalRegistry.SchemaUnion> =
& InputBase<$Schema>
export type InputStatic<$RegisteredClient extends GlobalRegistry.ClientUnion = GlobalRegistry.ClientUnion> =
& InputBase<$RegisteredClient>
& {
/**
* The schema to use.
Expand All @@ -29,7 +29,7 @@ export type InputStatic<$Schema extends GlobalRegistry.SchemaUnion = GlobalRegis
*
* @defaultValue 'default'
*/
name?: $Schema['index']['name']
name?: $RegisteredClient['name']
/**
* todo
*/
Expand All @@ -39,10 +39,10 @@ export type InputStatic<$Schema extends GlobalRegistry.SchemaUnion = GlobalRegis
// TODO use code generation to display
// TODO test that schema is optional when introspection was used to generate client.
// dprint-ignore
export type InputBase<$Schema extends GlobalRegistry.SchemaUnion> =
export type InputBase<$RegisteredClient extends GlobalRegistry.ClientUnion> =
| (
& (
GlobalRegistry.HasDefaultUrlForSchema<$Schema> extends true
GlobalRegistry.HasDefaultUrlForSchema<$RegisteredClient> extends true
? {
/**
* @defaultValue The introspection URL used to generate this Graffle client.
Expand All @@ -55,11 +55,11 @@ export type InputBase<$Schema extends GlobalRegistry.SchemaUnion> =
)
// eslint-disable-next-line
// @ts-ignore passes after generation
& WithInput<{ name: $Schema['name']; transport: { type: 'http'} }>
& WithInput<{ name: $RegisteredClient['name']; transport: { type: 'http'} }>
)
| (
& (
GlobalRegistry.HasDefaultUrlForSchema<$Schema> extends true
GlobalRegistry.HasDefaultUrlForSchema<$RegisteredClient> extends true
? {
/**
* TODO this TSDoc is never rendered in VSCode...
Expand All @@ -71,5 +71,5 @@ export type InputBase<$Schema extends GlobalRegistry.SchemaUnion> =
)
// eslint-disable-next-line
// @ts-ignore passes after generation
& WithInput<{ name: $Schema['name']; transport: { type: 'memory'} }>
& WithInput<{ name: $RegisteredClient['name']; transport: { type: 'memory'} }>
)
4 changes: 3 additions & 1 deletion src/layers/6_client/Settings/InputToConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export const inputToConfig = <$Input extends InputStatic>(

return {
initialInput: input,
// @ts-expect-error conditional type fixme
// todo fixme: passes before generation
// eslint-disable-next-line
// @ts-ignore
name: input.name ?? defaultSchemaName,
transport,
schemaMap: input.schemaMap ?? null as any,
Expand Down
10 changes: 5 additions & 5 deletions src/layers/6_client/prefilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ export type CreatePrefilled =
<
// eslint-disable-next-line
// @ts-ignore passes after generation
$Input extends InputPrefilled<GlobalRegistry.Schemas[$Name]>,
$Input extends InputPrefilled<GlobalRegistry.Clients[$Name]>,
>(...args:
// eslint-disable-next-line
// @ts-ignore passes after generation
HasRequiredKeys<InputPrefilled<GlobalRegistry.Schemas[$Name]>> extends true
HasRequiredKeys<InputPrefilled<GlobalRegistry.Clients[$Name]>> extends true
// eslint-disable-next-line
// @ts-ignore passes after generation
? [input: Exact<$Input, InputPrefilled<GlobalRegistry.Schemas[$Name]>>]
? [input: Exact<$Input, InputPrefilled<GlobalRegistry.Clients[$Name]>>]
// TODO test that input optional when no required properties
// eslint-disable-next-line
// @ts-ignore passes after generation
: ([input: Exact<$Input, InputPrefilled<GlobalRegistry.Schemas[$Name]>>] | [])
: ([input: Exact<$Input, InputPrefilled<GlobalRegistry.Clients[$Name]>>] | [])
) =>
// eslint-disable-next-line
// @ts-ignore passes after generation
Expand All @@ -45,7 +45,7 @@ export type CreatePrefilled =
}>

// dprint-ignore
export type InputPrefilled<$Schema extends GlobalRegistry.SchemaUnion> =
export type InputPrefilled<$Schema extends GlobalRegistry.ClientUnion> =
$Schema extends any
? InputBase<$Schema>
: never
2 changes: 1 addition & 1 deletion tests/_/schemas/kitchen-sink/graffle/modules/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare global {
export interface Schemas {
default: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
2 changes: 1 addition & 1 deletion tests/_/schemas/mutation-only/graffle/modules/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare global {
export interface Schemas {
MutationOnly: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
2 changes: 1 addition & 1 deletion tests/_/schemas/pokemon/graffle/modules/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare global {
export interface Schemas {
Pokemon: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
2 changes: 1 addition & 1 deletion tests/_/schemas/query-only/graffle/modules/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare global {
export interface Schemas {
QueryOnly: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
2 changes: 1 addition & 1 deletion website/graffle/modules/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare global {
export interface Schemas {
default: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down
2 changes: 1 addition & 1 deletion website/pokemon/modules/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare global {
export interface Schemas {
Pokemon: {
name: Data.Name
index: Schema
schema: Schema
interfaces: {
MethodsSelect: MethodsSelect.$MethodsSelect
Document: MethodsDocument.BuilderMethodsDocumentFn
Expand Down

0 comments on commit 64dc9d5

Please sign in to comment.