Skip to content

Commit

Permalink
refactor: only track name on ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Oct 31, 2024
1 parent 8bf07bf commit 552f346
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/layers/6_client/Settings/Config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { GraphQLSchema } from 'graphql'
import type { RequireProperties } from '../../../lib/prelude.js'
import type { TransportHttp, TransportMemory } from '../../../requestPipeline/Transport.js'
import type { GlobalRegistry } from '../../../types/GlobalRegistry/GlobalRegistry.js'
import type { SchemaDrivenDataMap } from '../../../types/SchemaDrivenDataMap/__.js'
import type { TransportHttpInput } from '../transportHttp/request.js'

Expand Down Expand Up @@ -106,7 +105,6 @@ export interface TransportConfigMemory {
}

export type Config = {
name: GlobalRegistry.ClientNames // todo remove? it is in context
output: OutputConfig
schemaMap: SchemaDrivenDataMap | null
transport: TransportConfigHttp | TransportConfigMemory
Expand Down
8 changes: 0 additions & 8 deletions src/layers/6_client/Settings/InputToConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { InputOutputEnvelopeLonghand, InputStatic, URLInput } from './Input

// dprint-ignore
export type NormalizeInput<$Input extends InputStatic> = {
name: HandleName<$Input>
schemaMap: ConfigManager.OrDefault<$Input['schemaMap'], null>
transport: HandleTransport<$Input>
output: {
Expand Down Expand Up @@ -49,10 +48,6 @@ export const inputToConfig = <$Input extends InputStatic>(
const transport = handleTransport(input)

return {
// todo fixme: passes before generation
// eslint-disable-next-line
// @ts-ignore
name: input.name ?? defaultSchemaName,
transport,
schemaMap: input.schemaMap ?? null as any,
output: {
Expand Down Expand Up @@ -86,9 +81,6 @@ export const inputToConfig = <$Input extends InputStatic>(
}
}

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

// dprint-ignore
type HandleTransport<$Input extends InputStatic> =
$Input['schema'] extends URLInput ? TransportConfigHttp :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Transport, TransportMemory } from '../../../../requestPipeline/Transport.js'
import type { GlobalRegistry } from '../../../../types/GlobalRegistry/GlobalRegistry.js'
import type { TransportHttpInput } from '../../transportHttp/request.js'
import type { OutputInput } from './output.js'

Expand All @@ -18,7 +17,6 @@ export type WithInput<$Context extends IncrementableInputContext = Incrementable
)

export type IncrementableInputContext = {
name: GlobalRegistry.ClientNames
transport: {
type: Transport
}
Expand Down
9 changes: 7 additions & 2 deletions src/layers/6_client/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defaultName } from '../../generator/config/defaults.js'
import type { Chain } from '../../lib/chain/__.js'
import { proxyGet } from '../../lib/prelude.js'
import type { GlobalRegistry } from '../../types/GlobalRegistry/GlobalRegistry.js'
import { Schema } from '../../types/Schema/__.js'
import { type Anyware_, AnywareExtension } from './chainExtensions/anyware.js'
import type { Internal_ } from './chainExtensions/internal.js'
Expand Down Expand Up @@ -34,7 +36,7 @@ type Create = <$Input extends InputStatic>(input: $Input) =>
// eslint-disable-next-line
// @ts-ignore
Client<{
name: $Input['name']
name: HandleName<$Input>
input: $Input
config: NormalizeInput<$Input>
retry: null
Expand All @@ -45,7 +47,7 @@ type Create = <$Input extends InputStatic>(input: $Input) =>

export const create: Create = (input) => {
const initialContext = createContext({
name: input.name ?? `default`, // todo import from shared constants
name: input.name ?? defaultName,
extensions: [],
scalars: Schema.Scalar.Registry.empty,
// retry: null,
Expand Down Expand Up @@ -93,3 +95,6 @@ export const createWithContext = (

return clientProxy
}

type HandleName<$Input extends InputStatic> = $Input['name'] extends string ? $Input['name']
: GlobalRegistry.DefaultClientName
2 changes: 1 addition & 1 deletion src/layers/6_client/clientPrefilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type CreatePrefilled = <
// @ts-ignore passes after generation
Client<{
name: $Name
input: $Input & { name: $Name; schemaMap: SchemaDrivenDataMap }
input: $Input & { schemaMap: SchemaDrivenDataMap }
retry: null
scalars: $Scalars
extensions: []
Expand Down
5 changes: 5 additions & 0 deletions src/layers/6_client/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface Context {
// retry: Anyware.Extension2<RequestPipeline.Core, { retrying: true }> | null
extensions: Extension[]
scalars: Schema.Scalar.Registry
/**
* Type level augmentations.
*
* @remarks Typically added by extensions. Added here upon use for optimized type-level reads later on.
*/
typeHooks: {
onRequestResult: Extension.Hooks.OnRequestResult[]
onRequestDocumentRootType: Extension.Hooks.OnRequestDocumentRootType[]
Expand Down
4 changes: 3 additions & 1 deletion src/layers/6_client/handleOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ type HandleOutput_Extensions<$Context extends Context, $Envelope extends Graffle
// @ts-ignore fixme
RunTypeHookOnRequestResult<$Context, {
result: $Envelope
registeredSchema: GlobalRegistry.GetOrDefault<$Context['config']['name']>
// eslint-disable-next-line
// @ts-ignore fixme
registeredSchema: GlobalRegistry.GetOrDefault<$Context['name']>
}>['result']
>

Expand Down
6 changes: 3 additions & 3 deletions src/layers/6_client/requestMethods/requestMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export type RequestMethods<$Arguments extends Chain.Extension.Parameters<Request
// GlobalRegistry.Has<$Context['name']> extends false
// eslint-disable-next-line
// @ts-ignore passes after generation
GlobalRegistry.Has<$Arguments['context']['config']['name']> extends false
GlobalRegistry.Has<$Arguments['context']['name']> extends false
? {}
:
(
// eslint-disable-next-line
// @ts-ignore Passes after generation
& TypeFunction.Call<GlobalRegistry.GetOrDefault<$Arguments['context']['config']['name']>['interfaces']['Root'], $Arguments['context']>
& TypeFunction.Call<GlobalRegistry.GetOrDefault<$Arguments['context']['name']>['interfaces']['Root'], $Arguments['context']>
& {
// eslint-disable-next-line
// @ts-ignore Passes after generation
document: TypeFunction.Call<GlobalRegistry.GetOrDefault<$Arguments['context']['config']['name']>['interfaces']['Document'], $Arguments['context']>
document: TypeFunction.Call<GlobalRegistry.GetOrDefault<$Arguments['context']['name']>['interfaces']['Document'], $Arguments['context']>
}
)
)
Expand Down
1 change: 1 addition & 0 deletions src/requestPipeline/CustomScalars/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const withBatch: TestCaseWith = [
{},
async ([_, query, responseData, expectedData], { fetch, kitchenSinkHttp: kitchenSink }) => {
fetch.mockResolvedValueOnce(createResponse({ data: responseData }))
kitchenSink._.name
expect(await kitchenSink.scalar(DateScalar).query.$batch(query)).toEqual(expectedData)
},
]
Expand Down
4 changes: 2 additions & 2 deletions tests/_/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ interface Fixtures {
fetch: Mock<(request: Request) => Promise<Response>>
pokemonService: SchemaService
graffle: Client<Context>
kitchenSink: Client<ConfigManager.SetAtPath<Context, ['config', 'name'], 'default'>>
kitchenSinkHttp: Client<ConfigManager.SetAtPath<Context, ['config', 'name'], 'default'>>
kitchenSink: Client<ConfigManager.SetAtPath<Context, ['name'], 'default'>>
kitchenSinkHttp: Client<ConfigManager.SetAtPath<Context, ['name'], 'default'>>
kitchenSinkData: typeof db
project: Project
}
Expand Down

0 comments on commit 552f346

Please sign in to comment.