Skip to content

Commit

Permalink
refactor: no circular module graph (#1301)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Dec 31, 2024
1 parent 6417878 commit 55a24bc
Show file tree
Hide file tree
Showing 17 changed files with 580 additions and 1,590 deletions.
1,958 changes: 471 additions & 1,487 deletions bundle-sizes/bare/bundle-stats.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bundle-sizes/bare/bundle-visualization.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bundle-sizes/bare/bundle.js

Large diffs are not rendered by default.

43 changes: 0 additions & 43 deletions src/client/Configuration/ConfigInit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ConfigManager } from '../../lib/config-manager/__.js'
import { Context, defaultName } from '../../types/context.js'
import type { GlobalRegistry } from '../../types/GlobalRegistry/GlobalRegistry.js'
import type { SchemaDrivenDataMap } from '../../types/SchemaDrivenDataMap/__.js'
import type { OutputChannel, OutputChannelConfig } from './Output.js'
Expand Down Expand Up @@ -106,45 +105,3 @@ type NormalizeConfigInitOutput<$Output extends ConfigInitOutput | undefined> = {
schema: ConfigManager.GetAtPathOrDefault<$Output,['errors', 'schema'], false>
}
}

export const updateContext = (
context: Context,
configInit: ConfigInit,
): Context => {
const outputEnvelopeLonghand: ConfigInitOutputEnvelopeLonghand | undefined =
typeof configInit.output?.envelope === `object`
? { enabled: true, ...configInit.output.envelope }
: typeof configInit.output?.envelope === `boolean`
? { enabled: configInit.output.envelope }
: undefined

const newConfig = {
name: configInit.name ?? context?.name ?? defaultName,
schemaMap: configInit.schemaMap ?? context.schemaMap,
output: {
defaults: {
errorChannel: configInit.output?.defaults?.errorChannel ?? context.output.defaults.errorChannel,
},
envelope: {
enabled: outputEnvelopeLonghand?.enabled ?? context.output.envelope.enabled,
errors: {
execution: outputEnvelopeLonghand?.errors?.execution ?? context.output.envelope.errors.execution,
other: outputEnvelopeLonghand?.errors?.other ?? context.output.envelope.errors.other,
// @ts-expect-error
schema: outputEnvelopeLonghand?.errors?.schema ?? context.output.envelope.errors.schema,
},
},
errors: {
execution: configInit.output?.errors?.execution ?? context.output.errors.execution,
other: configInit.output?.errors?.other ?? context.output.errors.other,
// @ts-expect-error conditional type
schema: configInit.output?.errors?.schema ?? context.output.errors.schema,
},
},
}

return Context.withTypeLevel({
...context,
...newConfig,
})
}
6 changes: 3 additions & 3 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Extension } from '../extension/__.js'
import type { Anyware } from '../lib/anyware/__.js'
import type { TypeFunction } from '../lib/type-function/__.js'
import { type ClientTransports, Context } from '../types/context.js'
import { type ConfigInit, type NormalizeConfigInit, updateContext } from './Configuration/ConfigInit.js'
import { type ConfigInit, type NormalizeConfigInit } from './Configuration/ConfigInit.js'
import { anywareProperties } from './properties/anyware.js'
import { type gqlOverload, gqlProperties } from './properties/gql/gql.js'
import { type ScalarMethod, scalarProperties, type TypeErrorMissingSchemaMap } from './properties/scalar.js'
Expand Down Expand Up @@ -83,7 +83,7 @@ export const createConstructorWithContext = <$Context extends Context>(
context: $Context,
): ClientConstructor<$Context> => {
return (configInit) => {
const newContext = updateContext(context, configInit ?? {})
const newContext = Context.updateContextConfigInit(context, configInit ?? {})
const client = createWithContext(newContext)
return client
}
Expand All @@ -103,7 +103,7 @@ export type ClientConstructor<$Context extends Context = Context.States.Empty> =
> // {}

export const create: ClientConstructor = (configInit) => {
const initialContext = updateContext(
const initialContext = Context.updateContextConfigInit(
Context.States.empty,
configInit ?? {},
)
Expand Down
2 changes: 1 addition & 1 deletion src/client/properties/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Context } from '../../types/context.js'
import type { GlobalRegistry } from '../../types/GlobalRegistry/GlobalRegistry.js'
import { Schema } from '../../types/Schema/__.js'
import { type Client } from '../client.js'
import type { Client } from '../client.js'
import { createProperties } from '../helpers.js'

export type TypeErrorMissingSchemaMap =
Expand Down
2 changes: 1 addition & 1 deletion src/client/properties/transport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PartialOrUndefined } from '../../lib/prelude.js'
import type { ClientTransports, ClientTransportsConfiguration } from '../../types/context.js'
import { type Context } from '../../types/context.js'
import { type Client } from '../client.js'
import type { Client } from '../client.js'
import { createProperties } from '../helpers.js'

// dprint-ignore
Expand Down
2 changes: 1 addition & 1 deletion src/client/properties/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Anyware } from '../../lib/anyware/__.js'
import type { UnknownOrAnyToNever } from '../../lib/prelude.js'
import { type Context } from '../../types/context.js'
import type { Transport } from '../../types/Transport.js'
import { type Client } from '../client.js'
import type { Client } from '../client.js'
import { createProperties } from '../helpers.js'

export type UseMethod<
Expand Down
5 changes: 3 additions & 2 deletions src/client/properties/with.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type ConfigInit, updateContext } from '../Configuration/ConfigInit.js'
import { Context } from '../../types/context.js'
import { type ConfigInit } from '../Configuration/ConfigInit.js'
import { createProperties } from '../helpers.js'

export const withProperties = createProperties((builder, state) => {
return {
with: (configInit: ConfigInit) => {
return builder(updateContext(state, configInit))
return builder(Context.updateContextConfigInit(state, configInit))
},
} as any
})
2 changes: 1 addition & 1 deletion src/lib/anyware/PipelineDef/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ConfigManager } from '../../config-manager/__.js'
import { type Tuple } from '../../prelude.js'
import type { Extension } from '../Extension/__.js'
import { Overload } from '../Overload/__.js'
import { Pipeline } from '../Pipeline/Pipeline.js'
import type { StepDefinition } from '../StepDefinition.js'
import type { StepRunner } from '../StepRunner.js'
import { Pipeline } from './_.js'
import type { PipelineDefinition } from './__.js'
import { type Options, resolveOptions } from './Config.js'

Expand Down
3 changes: 1 addition & 2 deletions src/lib/anyware/run/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createRunner } from '../_.js'
import type { Pipeline } from '../Pipeline/Pipeline.js'
import type { Result } from '../Result.js'
import type { Params } from './runner.js'
import { createRunner, type Params } from './runner.js'

type Run = <
$Pipeline extends Pipeline,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/grafaid/schema/KindMap/_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
isScalarType,
isUnionType,
} from 'graphql'
import { isScalarTypeCustom } from '../schema.js'

import { includesUnknown } from '../../../prelude.js'
import type { Grafaid } from '../../__.js'
import { isScalarTypeCustom } from '../typeGuards.js'
import type { KindMap } from './__.js'

export const Name = {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/grafaid/schema/customScalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'graphql'
import type { GraphQLInputObjectType, GraphQLNamedOutputType } from 'graphql'
import { casesExhausted } from '../../prelude.js'
import { isInputFieldLike, isOutputField, isScalarTypeAndCustom } from './schema.js'
import { isInputFieldLike, isOutputField, isScalarTypeAndCustom } from './typeGuards.js'

export const isHasCustomScalars = (
node: GraphQLNamedOutputType | GraphQLField<any, any> | GraphQLInputObjectType | GraphQLInputField | GraphQLArgument,
Expand Down
17 changes: 17 additions & 0 deletions src/lib/grafaid/schema/scalars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const standardScalarTypeNames = {
String: `String`,
ID: `ID`,
Int: `Int`,
Float: `Float`,
Boolean: `Boolean`,
}

export interface StandardScalarRuntimeTypeMap {
String: string
ID: string
Int: number
Float: number
Boolean: boolean
}

export type StandardScalarRuntimeTypes = StandardScalarRuntimeTypeMap[keyof StandardScalarRuntimeTypeMap]
51 changes: 7 additions & 44 deletions src/lib/grafaid/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
isEnumType,
isInputObjectType,
isInterfaceType,
isNonNullType,
isObjectType,
isUnionType,
} from 'graphql'
Expand Down Expand Up @@ -57,9 +56,15 @@ export {
} from 'graphql'

export * as Args from './args.js'

export * as CustomScalars from './customScalars.js'

export * from './KindMap/__.js'

export * from './typeGuards.js'

export * from './scalars.js'

export type DeprecatableNodes = GraphQLEnumValue | InputOrOutputField

export const isDeprecatableNode = (node: object): node is DeprecatableNodes => {
Expand Down Expand Up @@ -94,49 +99,7 @@ export type DescribableTypes =
| GraphQLNamedType
| InputOrOutputField

type InputFieldLikeTypes = GraphQLArgument | GraphQLInputField

export const isInputFieldLike = (value: object): value is InputFieldLikeTypes => {
return `defaultValue` in value
}

export const isField = (value: object): value is GraphQLField<any, any> | InputFieldLikeTypes => {
return isOutputField(value) || isInputFieldLike(value)
}

export const isOutputField = (value: object): value is GraphQLField<any, any> => {
return `args` in value
}

export const isScalarTypeCustom = (node: GraphQLScalarType): boolean => {
return !(node.name in standardScalarTypeNames)
}

export const standardScalarTypeNames = {
String: `String`,
ID: `ID`,
Int: `Int`,
Float: `Float`,
Boolean: `Boolean`,
}

export interface StandardScalarRuntimeTypeMap {
String: string
ID: string
Int: number
Float: number
Boolean: boolean
}

export type StandardScalarRuntimeTypes = StandardScalarRuntimeTypeMap[keyof StandardScalarRuntimeTypeMap]

export const isScalarTypeAndCustom = (node: unknown): node is GraphQLScalarType => {
return isScalarType(node) && isScalarTypeCustom(node)
}

export const isAllInputObjectFieldsNullable = (node: GraphQLInputObjectType) => {
return Object.values(node.getFields()).some(_ => !isNonNullType(_.type))
}
export type InputFieldLikeTypes = GraphQLArgument | GraphQLInputField

export const NameToClassNamedType = {
GraphQLScalarType: GraphQLScalarType,
Expand Down
28 changes: 28 additions & 0 deletions src/lib/grafaid/schema/typeGuards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { GraphQLInputObjectType } from 'graphql'
import { type GraphQLField, type GraphQLScalarType, isNonNullType, isScalarType } from 'graphql'
import { standardScalarTypeNames } from './scalars.js'
import type { InputFieldLikeTypes } from './schema.js'

export const isScalarTypeCustom = (node: GraphQLScalarType): boolean => {
return !(node.name in standardScalarTypeNames)
}

export const isScalarTypeAndCustom = (node: unknown): node is GraphQLScalarType => {
return isScalarType(node) && isScalarTypeCustom(node)
}

export const isAllInputObjectFieldsNullable = (node: GraphQLInputObjectType) => {
return Object.values(node.getFields()).some(_ => !isNonNullType(_.type))
}

export const isInputFieldLike = (value: object): value is InputFieldLikeTypes => {
return `defaultValue` in value
}

export const isField = (value: object): value is GraphQLField<any, any> | InputFieldLikeTypes => {
return isOutputField(value) || isInputFieldLike(value)
}

export const isOutputField = (value: object): value is GraphQLField<any, any> => {
return `args` in value
}
43 changes: 42 additions & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ConfigInit } from '../client/Configuration/ConfigInit.js'
import { type ConfigInit, type ConfigInitOutputEnvelopeLonghand } from '../client/Configuration/ConfigInit.js'
import { type OutputConfig, type OutputConfigDefault, outputConfigDefault } from '../client/Configuration/Output.js'
import type { Extension } from '../extension/__.js'
import type { Anyware } from '../lib/anyware/__.js'
Expand Down Expand Up @@ -137,6 +137,47 @@ export type DefaultName = GlobalRegistry.DefaultClientName
export const defaultName = GlobalRegistry.defaultClientName

export namespace Context {
export const updateContextConfigInit = (
context: Context,
configInit: ConfigInit,
): Context => {
const outputEnvelopeLonghand: ConfigInitOutputEnvelopeLonghand | undefined =
typeof configInit.output?.envelope === `object`
? { enabled: true, ...configInit.output.envelope }
: typeof configInit.output?.envelope === `boolean`
? { enabled: configInit.output.envelope }
: undefined

const newConfig = {
name: configInit.name ?? context?.name ?? defaultName,
schemaMap: configInit.schemaMap ?? context.schemaMap,
output: {
defaults: {
errorChannel: configInit.output?.defaults?.errorChannel ?? context.output.defaults.errorChannel,
},
envelope: {
enabled: outputEnvelopeLonghand?.enabled ?? context.output.envelope.enabled,
errors: {
execution: outputEnvelopeLonghand?.errors?.execution ?? context.output.envelope.errors.execution,
other: outputEnvelopeLonghand?.errors?.other ?? context.output.envelope.errors.other,
// @ts-expect-error
schema: outputEnvelopeLonghand?.errors?.schema ?? context.output.envelope.errors.schema,
},
},
errors: {
execution: configInit.output?.errors?.execution ?? context.output.errors.execution,
other: configInit.output?.errors?.other ?? context.output.errors.other,
// @ts-expect-error conditional type
schema: configInit.output?.errors?.schema ?? context.output.errors.schema,
},
},
}

return Context.withTypeLevel({
...context,
...newConfig,
})
}
export const withTypeLevel = (contextValueLevel: ContextValueLevel): Context => contextValueLevel as any

export namespace States {
Expand Down

0 comments on commit 55a24bc

Please sign in to comment.