Skip to content

Commit

Permalink
refactor: global registry use client term (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 19, 2024
1 parent f44b086 commit a32ec8d
Show file tree
Hide file tree
Showing 23 changed files with 51 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/entrypoints/utilities-for-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { type Simplify } from 'type-fest'
export { type SchemaDrivenDataMap } from '../extensions/CustomScalars/schemaDrivenDataMap/__.js'
export * from '../layers/2_Select/__.js'
export { type Schema as SchemaIndexBase } from '../layers/4_generator/generators/Schema.js'
export { type GlobalRegistry } from '../layers/4_generator/globalRegistry.js'
export { type GlobalRegistry } from '../layers/4_generator/GlobalRegistry.js'
export type {
ConfigGetOutputError,
HandleOutput,
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/SchemaErrors/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GlobalRegistry } from '../../layers/4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../layers/4_generator/GlobalRegistry.js'

declare global {
namespace GraffleGlobal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
GraffleSchemaErrors: {
name: Data.Name
schema: Schema
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
@@ -1,4 +1,4 @@
import type { GlobalRegistry } from '../../../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../../../4_generator/GlobalRegistry.js'
import type { Codec, Mapper } from './codec.js'
import { JavaScriptScalarCodecs } from './nativeScalarCodecs.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IsNever } from 'type-fest'
import type { TypeFunction } from '../../entrypoints/utilities-for-generated.js'
import type { ConfigManager } from '../../lib/config-manager/__.js'
import type { Values } from '../../lib/prelude.js'
Expand All @@ -6,14 +7,12 @@ import type { Schema } from './generators/Schema.js'

declare global {
export namespace GraffleGlobal {
interface Schemas {}
// Use this is for manual internal type testing.
interface SchemasAlwaysEmpty {}
interface Clients {}
}
}

interface ZeroClient extends GlobalRegistry.RegisteredSchema {
name: GlobalRegistry.DefaultSchemaName
interface ZeroClient extends GlobalRegistry.Client {
name: GlobalRegistry.DefaultClientName
schema: Schema
interfaces: {
Root: TypeFunction.Fn
Expand All @@ -23,7 +22,7 @@ interface ZeroClient extends GlobalRegistry.RegisteredSchema {
defaultSchemaUrl: null
}

export type GlobalRegistry = Record<string, GlobalRegistry.RegisteredSchema>
export type GlobalRegistry = Record<string, GlobalRegistry.Client>

export namespace GlobalRegistry {
export type TypeExtensions = Record<string, Record<string, unknown>>
Expand All @@ -36,7 +35,7 @@ export namespace GlobalRegistry {
Schema: ConfigManager.OrDefault<$Extensions['Schema'], TypeExtensions>
}

export interface RegisteredSchema<$Extensions extends Extensions = Extensions> {
export interface Client<$Extensions extends Extensions = Extensions> {
name: string
schema: Schema<$Extensions['Schema']>
interfaces: {
Expand All @@ -50,17 +49,17 @@ export namespace GlobalRegistry {
defaultSchemaUrl: string | null
}

export type DefaultSchemaName = 'default'
export type DefaultClientName = 'default'

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

export type IsEmpty = keyof Clients extends never ? true : false
export type IsEmpty = IsNever<keyof Clients> extends true ? true : false

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

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

// dprint-ignore
export type HasDefaultUrlForSchema<$Schema extends ClientUnion> =
Expand All @@ -70,25 +69,25 @@ export namespace GlobalRegistry {

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

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

// dprint-ignore
export type GetOrDefault<$Name extends SchemaNames | undefined> =
$Name extends SchemaNames
export type GetOrDefault<$Name extends ClientNames | undefined> =
$Name extends ClientNames
// eslint-disable-next-line
// @ts-ignore passes after generation
? GraffleGlobal.Schemas[$Name]
? GraffleGlobal.Clients[$Name]
// eslint-disable-next-line
// @ts-ignore passes after generation
: GraffleGlobal.Schemas[DefaultSchemaName]
: GraffleGlobal.Clients[DefaultClientName]

// dprint-ignore
export type GetSchemaOrDefault<$Name extends SchemaNames | undefined> =
$Name extends SchemaNames
export type GetSchemaOrDefault<$Name extends ClientNames | undefined> =
$Name extends ClientNames
? GetSchema<$Name>
: SchemaDefault
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import type { Schema } from './Schema.js'
declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
default: {
name: Data.Name
schema: Schema
Expand Down
4 changes: 2 additions & 2 deletions src/layers/4_generator/generators/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Grafaid } from '../../../lib/grafaid/__.js'
import { entries, isObjectEmpty, values } from '../../../lib/prelude.js'
import type { SchemaKit } from '../../1_Schema/__.js'
import type { Config } from '../config/config.js'
import type { GlobalRegistry } from '../globalRegistry.js'
import type { GlobalRegistry } from '../GlobalRegistry.js'
import { identifiers } from '../helpers/identifiers.js'
import { createModuleGenerator } from '../helpers/moduleGenerator.js'
import { createCodeGenerator } from '../helpers/moduleGeneratorRunner.js'
Expand All @@ -20,7 +20,7 @@ import { ModuleGeneratorScalar } from './Scalar.js'
export interface Schema<
$Extensions extends GlobalRegistry.TypeExtensions = GlobalRegistry.TypeExtensions,
> {
name: GlobalRegistry.SchemaNames
name: GlobalRegistry.ClientNames
RootTypesPresent: ('Query' | 'Mutation' | 'Subscription')[]
RootUnion: SchemaKit.Output.RootType
Root: {
Expand Down
9 changes: 3 additions & 6 deletions src/layers/4_generator/generators/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ModuleGeneratorGlobal = createModuleGenerator(
const customScalarsProperties = config.schema.kindMap.GraphQLScalarTypeCustom
.map((_) => [_.name, `${identifiers.Scalar}.${_.name}`])

const ClientFields = Code.termObjectFields({
const Clients = Code.termObjectFields({
[config.name]: {
name: `Data.Name`,
schema: identifiers.Schema,
Expand All @@ -48,17 +48,14 @@ export const ModuleGeneratorGlobal = createModuleGenerator(
},
})

// todo rename "Schemas" to "Clients"
code(`
declare global {
export namespace GraffleGlobal {
export interface Schemas {
${ClientFields}
export interface Clients {
${Clients}
}
}
}
`)

return code
},
)
4 changes: 2 additions & 2 deletions src/layers/5_select/select.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { identityProxy } from '../../lib/prelude.js'
import type { GlobalRegistry } from '../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../4_generator/GlobalRegistry.js'

// dprint-ignore
type Create = <$Name extends GlobalRegistry.SchemaNames>(name: $Name) =>
type Create = <$Name extends GlobalRegistry.ClientNames>(name: $Name) =>
// eslint-disable-next-line
// @ts-ignore passes after generation
GlobalRegistry.GetOrDefault<$Name>['interfaces']['MethodsSelect']
Expand Down
4 changes: 2 additions & 2 deletions src/layers/6_client/Settings/Config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GraphQLSchema } from 'graphql'
import type { SchemaDrivenDataMap } from '../../../extensions/CustomScalars/schemaDrivenDataMap/__.js'
import type { RequireProperties } from '../../../lib/prelude.js'
import type { GlobalRegistry } from '../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../4_generator/GlobalRegistry.js'
import type { TransportHttp, TransportMemory } from '../../5_request/types.js'
import type { Extension } from '../extension/extension.js'
import type { TransportHttpInput } from '../transportHttp/request.js'
Expand Down Expand Up @@ -115,7 +115,7 @@ export type Config = {
typeHooks: {
onRequestResult: Extension.Hooks.OnRequestResult[]
}
name: GlobalRegistry.SchemaNames
name: GlobalRegistry.ClientNames
output: OutputConfig
schemaMap: SchemaDrivenDataMap | null
transport: TransportConfigHttp | TransportConfigMemory
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/Settings/Input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GraphQLSchema } from 'graphql'
import type { SchemaDrivenDataMap } from '../../../extensions/CustomScalars/schemaDrivenDataMap/__.js'
import type { GlobalRegistry } from '../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../4_generator/GlobalRegistry.js'
import type { WithInput } from './inputIncrementable/inputIncrementable.js'

export type URLInput = URL | string
Expand Down
6 changes: 3 additions & 3 deletions src/layers/6_client/Settings/InputToConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IsUnknown } from 'type-fest'
import type { ConfigManager } from '../../../lib/config-manager/__.js'
import type { GlobalRegistry } from '../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../4_generator/GlobalRegistry.js'
import { Transport } from '../../5_request/types.js'
import { defaultMethodMode } from '../transportHttp/request.js'
import { outputConfigDefault, type TransportConfigHttp, type TransportConfigMemory } from './Config.js'
Expand Down Expand Up @@ -41,7 +41,7 @@ export type NormalizeInput<$Input extends InputStatic> = {
}
}

export const defaultSchemaName: GlobalRegistry.DefaultSchemaName = `default`
export const defaultSchemaName: GlobalRegistry.DefaultClientName = `default`

export const inputToConfig = <$Input extends InputStatic>(
input: $Input,
Expand Down Expand Up @@ -94,7 +94,7 @@ export const inputToConfig = <$Input extends InputStatic>(
}

type HandleName<$Input extends InputStatic> = $Input['name'] extends string ? $Input['name']
: GlobalRegistry.DefaultSchemaName
: GlobalRegistry.DefaultClientName

// dprint-ignore
type HandleTransport<$Input extends InputStatic> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GlobalRegistry } from '../../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../../4_generator/GlobalRegistry.js'
import type { Transport, TransportMemory } from '../../../5_request/types.js'
import type { TransportHttpInput } from '../../transportHttp/request.js'
import type { Config } from '../Config.js'
Expand All @@ -20,7 +20,7 @@ export type WithInput<$Context extends IncrementableInputContext = Incrementable
)

export type IncrementableInputContext = {
name: GlobalRegistry.SchemaNames
name: GlobalRegistry.ClientNames
transport: {
type: Transport
}
Expand Down
4 changes: 2 additions & 2 deletions src/layers/6_client/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AssertConstraint } from '../../../lib/prelude.js'
import type { TypeFunction } from '../../../lib/type-function/__.js'
import type { Fn } from '../../../lib/type-function/TypeFunction.js'
import type { Select } from '../../2_Select/__.js'
import type { GlobalRegistry } from '../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../4_generator/GlobalRegistry.js'
import type { RequestCore } from '../../5_request/__.js'
import type { Client } from '../client.js'
import type { GraffleExecutionResultEnvelope } from '../handleOutput.js'
Expand Down Expand Up @@ -61,7 +61,7 @@ export namespace Extension {
export namespace OnRequestResult {
export interface Params<$Extensions extends GlobalRegistry.Extensions = GlobalRegistry.Extensions> {
result: GraffleExecutionResultEnvelope
registeredSchema: GlobalRegistry.RegisteredSchema<$Extensions>
registeredSchema: GlobalRegistry.Client<$Extensions>
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/handleOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type GetOrNever,
type Values,
} from '../../lib/prelude.js'
import type { GlobalRegistry } from '../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../4_generator/GlobalRegistry.js'
import type { TransportHttp } from '../5_request/types.js'
import type { RunTypeHookOnRequestResult } from './extension/extension.js'
import type { State } from './fluent.js'
Expand Down
4 changes: 2 additions & 2 deletions src/layers/6_client/prefilled.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { HasRequiredKeys } from 'type-fest'
import type { SchemaDrivenDataMap } from '../../extensions/CustomScalars/schemaDrivenDataMap/__.js'
import type { Exact } from '../../lib/prelude.js'
import type { GlobalRegistry } from '../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../4_generator/GlobalRegistry.js'
import { type Client, create } from './client.js'
import type { InputBase } from './Settings/Input.js'
import type { NormalizeInput } from './Settings/InputToConfig.js'
Expand All @@ -20,7 +20,7 @@ export const createPrefilled: CreatePrefilled = (name, schemaMap, schemaUrl) =>

// dprint-ignore
export type CreatePrefilled =
<const $Name extends GlobalRegistry.SchemaNames>(name: $Name, sddm: SchemaDrivenDataMap, schemaUrl?: URL) =>
<const $Name extends GlobalRegistry.ClientNames>(name: $Name, sddm: SchemaDrivenDataMap, schemaUrl?: URL) =>
<
// eslint-disable-next-line
// @ts-ignore passes after generation
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/requestMethods/requestMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Fluent } from '../../../lib/fluent/__.js'
import type { Grafaid } from '../../../lib/grafaid/__.js'
import { isSymbol } from '../../../lib/prelude.js'
import { Select } from '../../2_Select/__.js'
import type { GlobalRegistry } from '../../4_generator/globalRegistry.js'
import type { GlobalRegistry } from '../../4_generator/GlobalRegistry.js'
import { RequestCore } from '../../5_request/__.js'
import { type ClientContext, defineTerminus, type State } from '../fluent.js'
import { handleOutput } from '../handleOutput.js'
Expand Down
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 @@ -7,7 +7,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
default: {
name: Data.Name
schema: Schema
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 @@ -6,7 +6,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
MutationOnly: {
name: Data.Name
schema: Schema
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 @@ -6,7 +6,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
Pokemon: {
name: Data.Name
schema: Schema
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 @@ -6,7 +6,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
QueryOnly: {
name: Data.Name
schema: Schema
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 @@ -6,7 +6,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
default: {
name: Data.Name
schema: Schema
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 @@ -6,7 +6,7 @@ import type { Schema } from './Schema.js'

declare global {
export namespace GraffleGlobal {
export interface Schemas {
export interface Clients {
Pokemon: {
name: Data.Name
schema: Schema
Expand Down

0 comments on commit a32ec8d

Please sign in to comment.