Skip to content

Commit

Permalink
refactor: fluent to chain module (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 30, 2024
1 parent ed696db commit dd08c2e
Show file tree
Hide file tree
Showing 53 changed files with 596 additions and 564 deletions.
2 changes: 2 additions & 0 deletions examples/10_transport-http/transport-http_abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const graffle = Graffle.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
})

graffle

const resultPromise = graffle
.with({ transport: { signal: abortController.signal } })
// ^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions src/documentBuilder/InferResult/Alias.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { mergeObjectArray, ValuesOrEmptyObject } from '../../lib/prelude.js'
import type { mergeArrayOfObjects, ValuesOrEmptyObject } from '../../lib/prelude.js'
import type { Schema } from '../../types/Schema/__.js'
import type { Select } from '../Select/__.js'
import type { OutputField } from './OutputField.js'
Expand Down Expand Up @@ -42,7 +42,7 @@ type InferSelectAliasMultiple<
$FieldName extends string,
$Schema extends Schema,
$Node extends Schema.OutputObject,
> = mergeObjectArray<
> = mergeArrayOfObjects<
{
[_ in keyof $SelectAliasMultiple]: InferSelectAliasOne<$SelectAliasMultiple[_], $FieldName, $Schema, $Node>
}
Expand Down
2 changes: 1 addition & 1 deletion src/entrypoints/utilities-for-generated.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { type Simplify } from 'type-fest'
export * from '../documentBuilder/Select/__.js'
export { type ClientContext } from '../layers/6_client/fluent.js'
export { type Context } from '../layers/6_client/context.js'
export type {
ConfigGetOutputError,
HandleOutput,
Expand Down
13 changes: 7 additions & 6 deletions src/extensions/Introspection/Introspection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getIntrospectionQuery, type IntrospectionQuery } from 'graphql'
import type { Extension, SimplifyNullable } from '../../entrypoints/main.js'
import type { Context } from '../../layers/6_client/context.js'
import { createExtension } from '../../layers/6_client/extension/extension.js'
import type { FnParametersProperty } from '../../layers/6_client/fluent.js'
import type { HandleOutput } from '../../layers/6_client/handleOutput.js'
import type { Fluent } from '../../lib/fluent/__.js'
import type { Chain } from '../../lib/chain/__.js'
import { createConfig, type Input } from './config.js'

const knownPotentiallyUnsupportedFeatures = [`inputValueDeprecation`, `oneOf`] as const
Expand Down Expand Up @@ -65,14 +65,15 @@ export const Introspection = (input?: Input) => {
}

type IntrospectionExtension = Extension<{
property: IntrospectFn
property: Introspect_
}>

interface IntrospectFn extends Fluent.FnProperty<`introspect`> {
interface Introspect_ extends Chain.Extension {
context: Context
// @ts-expect-error untyped params
return: Introspect<this['params']>
}

interface Introspect<$Args extends FnParametersProperty> {
(): Promise<SimplifyNullable<HandleOutput<$Args['state']['context'], IntrospectionQuery>>>
interface Introspect<$Args extends Chain.Extension.Parameters<Introspect_>> {
introspect: () => Promise<SimplifyNullable<HandleOutput<$Args['context'], IntrospectionQuery>>>
}
20 changes: 10 additions & 10 deletions src/extensions/SchemaErrors/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,38 +71,38 @@ export const SchemaErrors = () => {
}

type SchemaErrorsExtension = Extension<{
onRequestDocumentRootType: OnRequestDocumentRootTypeFn
onRequestResult: OnRequestResultFn
onRequestDocumentRootType: OnRequestDocumentRootType_
onRequestResult: OnRequestResult_
}>

type OnRequestDocumentRootType<$Params extends Extension.Hooks.OnRequestDocumentRootType.Params> =
$Params['selectionRootType']

// dprint-ignore
interface OnRequestResult<$Params extends Extension.Hooks.OnRequestResult.Params<GeneratedExtensions>>
interface OnRequestResult<$Arguments extends Extension.Hooks.OnRequestResult.Params<GeneratedExtensions>>
{
result: {
data?:
| null
| {
[$Key in keyof ExcludeNullAndUndefined<$Params['result']['data']>]:
[$Key in keyof ExcludeNullAndUndefined<$Arguments['result']['data']>]:
Exclude<
ExcludeNullAndUndefined<$Params['result']['data']>[$Key],
{ __typename: $Params['registeredSchema']['schema']['extensions']['SchemaErrors']['objectNames'] }
ExcludeNullAndUndefined<$Arguments['result']['data']>[$Key],
{ __typename: $Arguments['registeredSchema']['schema']['extensions']['SchemaErrors']['objectNames'] }
>
}
} & Omit<$Params['result'], 'data'>
registeredSchema: $Params['registeredSchema']
} & Omit<$Arguments['result'], 'data'>
registeredSchema: $Arguments['registeredSchema']
}

// --------- Boilerplate Types ---------

interface OnRequestDocumentRootTypeFn extends Extension.Hooks.OnRequestDocumentRootType {
interface OnRequestDocumentRootType_ extends Extension.Hooks.OnRequestDocumentRootType {
// @ts-expect-error untyped params
return: OnRequestDocumentRootType<this['params']>
}

interface OnRequestResultFn extends Extension.Hooks.OnRequestResult {
interface OnRequestResult_ extends Extension.Hooks.OnRequestResult {
// @ts-expect-error untyped params
return: OnRequestResult<this['params']>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as $$Utilities from '../../../../../../entrypoints/utilities-for-g
import * as $$Schema from './schema.js'
import * as $$SelectionSets from './selection-sets.js'

export interface Document<$Context extends $$Utilities.ClientContext> {
export interface Document<$Context extends $$Utilities.Context> {
<$Document>(
document: $$Utilities.ExactNonEmpty<$Document, $$SelectionSets.$Document<$Context['scalars']>>,
): $$Utilities.DocumentRunner<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as $$Utilities from '../../../../../../entrypoints/utilities-for-g
import * as $$Schema from './schema.js'
import * as $$SelectionSets from './selection-sets.js'

export interface QueryMethods<$Context extends $$Utilities.ClientContext> {
export interface QueryMethods<$Context extends $$Utilities.Context> {
$batch: <$SelectionSet>(
selectionSet: $$Utilities.Exact<$SelectionSet, $$SelectionSets.Query<$Context['scalars']>>,
) => Promise<
Expand Down Expand Up @@ -695,7 +695,7 @@ export interface QueryMethods<$Context extends $$Utilities.ClientContext> {
>
}

export interface MutationMethods<$Context extends $$Utilities.ClientContext> {
export interface MutationMethods<$Context extends $$Utilities.Context> {
$batch: <$SelectionSet>(
selectionSet: $$Utilities.Exact<$SelectionSet, $$SelectionSets.Mutation<$Context['scalars']>>,
) => Promise<
Expand Down Expand Up @@ -744,7 +744,7 @@ export interface MutationMethods<$Context extends $$Utilities.ClientContext> {
>
}

export interface BuilderMethodsRoot<$Context extends $$Utilities.ClientContext> {
export interface BuilderMethodsRoot<$Context extends $$Utilities.Context> {
query: QueryMethods<$Context>
mutation: MutationMethods<$Context>
}
Expand Down
14 changes: 7 additions & 7 deletions src/extensions/Throws/Throws.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type BuilderConfig, createExtension, type Extension, type WithInput } from '../../entrypoints/main.js'
import type { ConfigManager } from '../../lib/config-manager/__.js'
// todo: no deep imports, rethink these utilities and/or how they are exported from the graffle package.
import type { IncrementWthNewConfig } from '../../layers/6_client/client.js'
import type { FnParametersProperty } from '../../layers/6_client/fluent.js'
import type { Fluent } from '../../lib/fluent/__.js'
import type { Context } from '../../layers/6_client/context.js'
import type { Chain } from '../../lib/chain/__.js'

export const Throws = () => {
return createExtension<ThrowsExtension>({
Expand All @@ -30,20 +29,21 @@ export const Throws = () => {
}

type ThrowsExtension = Extension<{
property: ThrowsFn
property: Throws_
}>

interface ThrowsFn extends Fluent.FnProperty<`throws`> {
interface Throws_ extends Chain.Extension {
context: Context
// @ts-expect-error untyped params
return: Throws<this['params']>
}

interface Throws<$Args extends FnParametersProperty> {
interface Throws<$Args extends Chain.Extension.Parameters<Throws_>> {
/**
* TODO
*/
// @ts-expect-error fixme
(): IncrementWthNewConfig<$Args, ThrowsifyConfig<$Args['state']['context']['config']>>
throws: () => IncrementWthNewConfig<$Args, ThrowsifyConfig<$Args['context']['config']>>
}

type ThrowsifyConfig<$BuilderConfig extends BuilderConfig> = ConfigManager.Set<
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/Upload/Upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { Graffle } from '../../entrypoints/main.js'
import { Upload } from './Upload.js'

import { type SchemaService, serveSchema } from '../../../tests/_/lib/serveSchema.js'
import type { ClientContext } from '../../entrypoints/utilities-for-generated.js'
import type { Context } from '../../entrypoints/utilities-for-generated.js'
import type { Client } from '../../layers/6_client/client.js'

let schemaServer: SchemaService

let graffle: Client<ClientContext>
let graffle: Client<Context>

beforeAll(async () => {
schemaServer = await serveSchema({ schema })
Expand Down
14 changes: 6 additions & 8 deletions src/generator/generator/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ exports[`kitchen-sink generated modules > modules/methods-document.ts 1`] = `
import * as $$Schema from './schema.js'
import * as $$SelectionSets from './selection-sets.js'

export interface Document<$Context extends $$Utilities.ClientContext> {
export interface Document<$Context extends $$Utilities.Context> {
<$Document>(
document: $$Utilities.ExactNonEmpty<$Document, $$SelectionSets.$Document<$Context['scalars']>>,
): $$Utilities.DocumentRunner<
Expand All @@ -159,7 +159,7 @@ import type * as $$Utilities from '../../../../../../src/entrypoints/utilities-f
import * as $$Schema from './schema.js'
import * as $$SelectionSets from './selection-sets.js'

export interface QueryMethods<$Context extends $$Utilities.ClientContext> {
export interface QueryMethods<$Context extends $$Utilities.Context> {
$batch: <$SelectionSet>(
selectionSet: $$Utilities.Exact<$SelectionSet, $$SelectionSets.Query<$Context['scalars']>>,
) => Promise<
Expand Down Expand Up @@ -851,7 +851,7 @@ export interface QueryMethods<$Context extends $$Utilities.ClientContext> {
>
}

export interface MutationMethods<$Context extends $$Utilities.ClientContext> {
export interface MutationMethods<$Context extends $$Utilities.Context> {
$batch: <$SelectionSet>(
selectionSet: $$Utilities.Exact<$SelectionSet, $$SelectionSets.Mutation<$Context['scalars']>>,
) => Promise<
Expand Down Expand Up @@ -900,7 +900,7 @@ export interface MutationMethods<$Context extends $$Utilities.ClientContext> {
>
}

export interface BuilderMethodsRoot<$Context extends $$Utilities.ClientContext> {
export interface BuilderMethodsRoot<$Context extends $$Utilities.Context> {
query: QueryMethods<$Context>
mutation: MutationMethods<$Context>
}
Expand Down Expand Up @@ -7700,7 +7700,7 @@ import * as $$Schema from "./schema.js";
import type * as $$Utilities from "graffle/utilities-for-generated";
import type { InferResult } from "graffle/schema";

export interface QueryRootMethods<$Context extends $$Utilities.ClientContext> {
export interface QueryRootMethods<$Context extends $$Utilities.Context> {
$batch: <$SelectionSet>(
selectionSet: $$Utilities.Exact<
$SelectionSet,
Expand Down Expand Up @@ -7748,9 +7748,7 @@ export interface QueryRootMethods<$Context extends $$Utilities.ClientContext> {
>;
}

export interface BuilderMethodsRoot<
$Context extends $$Utilities.ClientContext,
> {
export interface BuilderMethodsRoot<$Context extends $$Utilities.Context> {
undefined: QueryRootMethods<$Context>;
}

Expand Down
2 changes: 1 addition & 1 deletion src/generator/generators/MethodsDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const ModuleGeneratorMethodsDocument = createModuleGenerator(
code()
code(Code.tsInterface({
name: `Document`,
parameters: [`$Context extends ${identifiers.$$Utilities}.ClientContext`],
parameters: [`$Context extends ${identifiers.$$Utilities}.Context`],
// dprint-ignore
block: `
<$Document>(document: ${identifiers.$$Utilities}.ExactNonEmpty<$Document, ${identifiers.$$SelectionSets}.$Document<$Context['scalars']>>): ${identifiers.$$Utilities}.DocumentRunner<
Expand Down
4 changes: 2 additions & 2 deletions src/generator/generators/MethodsRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ModuleGeneratorMethodsRoot = createModuleGenerator(
code()
})
code(`
export interface BuilderMethodsRoot<$Context extends ${identifiers.$$Utilities}.ClientContext> {
export interface BuilderMethodsRoot<$Context extends ${identifiers.$$Utilities}.Context> {
${
config.schema.kindMap.list.Root.map(node => {
const operationName = Grafaid.Document
Expand All @@ -51,7 +51,7 @@ const renderRootType = createCodeGenerator<{ node: Grafaid.Schema.ObjectType }>(

// dprint-ignore
code(`
export interface ${node.name}Methods<$Context extends ${identifiers.$$Utilities}.ClientContext> {
export interface ${node.name}Methods<$Context extends ${identifiers.$$Utilities}.Context> {
$batch: <$SelectionSet>(selectionSet: ${identifiers.$$Utilities}.Exact<$SelectionSet, ${identifiers.$$SelectionSets}.${node.name}<$Context['scalars']>>) =>
Promise<
${identifiers.$$Utilities}.SimplifyExcept<
Expand Down
71 changes: 34 additions & 37 deletions src/layers/6_client/client.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
import type { ConfigManager } from '../../lib/config-manager/__.js'
import type { Fluent } from '../../lib/fluent/__.js'
import type { Chain } from '../../lib/chain/__.js'
import { proxyGet } from '../../lib/prelude.js'
import { Schema } from '../../types/Schema/__.js'
import { type UseFn, useProperties } from './extension/use.js'
import { type ClientContext, createState, type FnParametersProperty, type StateWithoutConfig } from './fluent.js'
import { type FnGql, gqlProperties } from './gql/gql.js'
import { anywareProperties, type FnAnyware } from './properties/anyware.js'
import type { FnInternal } from './properties/internal.js'
import { type FnRetry, retryProperties } from './properties/retry.js'
import { type ScalarFn, scalarProperties } from './properties/scalar.js'
import { type FnWith, withProperties } from './properties/with.js'
import { type FnRequestMethods, requestMethodsProperties } from './requestMethods/requestMethods.js' // todo
import { type Context, type ContextWithoutConfig, createContext } from './context.js'
import { type Use_, useProperties } from './extension/use.js'
import { type Gql_, gqlProperties } from './gql/gql.js'
import { type Anyware_, AnywareExtension } from './properties/anyware.js'
import type { Internal_ } from './properties/internal.js'
import { type Scalar_, scalarProperties } from './properties/scalar.js'
import { type With_, withProperties } from './properties/with.js'
import { type RequestMethods_, requestMethodsProperties } from './requestMethods/requestMethods.js' // todo
import { type InputStatic } from './Settings/Input.js'
import { type NormalizeInput } from './Settings/InputToConfig.js'

export type Client<$Context extends ClientContext> = Fluent.Materialize<
Fluent.AddMany<
Fluent.Create<$Context>,
export type Client<$Context extends Context> = Chain.Definition.MaterializeWithNewContext<
Chain.Definition.ExtendMany<
Chain.Definition.Empty,
[
FnInternal,
FnRequestMethods,
FnRetry,
FnWith,
UseFn,
FnAnyware,
FnGql,
ScalarFn,
Internal_,
RequestMethods_,
With_,
Use_,
Anyware_,
Gql_,
Scalar_,
]
>
>,
$Context
>

export type IncrementWthNewConfig<
$Parameters extends FnParametersProperty,
$ConfigNew extends ClientContext['config'],
> = Fluent.IncrementWthNewContext<
$Parameters,
ConfigManager.SetProperty<$Parameters['state']['context'], 'config', $ConfigNew>
>
// export type IncrementWthNewConfig<
// $Parameters extends FnParametersProperty,
// $ConfigNew extends Context['config'],
// > = Chain.IncrementWthNewContext<
// $Parameters,
// ConfigManager.SetProperty<$Parameters['state']['context'], 'config', $ConfigNew>
// >

// dprint-ignore
type Create = <$Input extends InputStatic>(input: $Input) =>
Expand All @@ -53,7 +51,7 @@ type Create = <$Input extends InputStatic>(input: $Input) =>
}>

export const create: Create = (input) => {
const initialState = createState({
const initialState = createContext({
name: input.name ?? `default`, // todo import from shared constants
extensions: [],
scalars: Schema.Scalar.Registry.empty,
Expand All @@ -64,24 +62,23 @@ export const create: Create = (input) => {
}

export const createWithState = (
initialState: StateWithoutConfig,
initialState: ContextWithoutConfig,
) => {
const state = createState(initialState)
const state = createContext(initialState)

// @ts-expect-error ignoreme
const clientDirect: Client = {
_: state,
...gqlProperties(state),
...gqlProperties(createWithState, state),
...withProperties(createWithState, state),
...useProperties(createWithState, state),
...anywareProperties(createWithState, state),
...retryProperties(createWithState, state),
...AnywareExtension(createWithState, state),
...scalarProperties(createWithState, state),
}

// todo test that access to this works without generation in a unit like test. We discovered bug and covered this in an e2e test.
Object.assign(clientDirect, {
...requestMethodsProperties(state),
...requestMethodsProperties(createWithState, state),
})

const clientProxy = proxyGet(clientDirect, ({ path, property }) => {
Expand Down
Loading

0 comments on commit dd08c2e

Please sign in to comment.