Skip to content

Commit

Permalink
improve: remove interface conecpt from anyware (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 28, 2024
1 parent c7f7dc5 commit 4dc1a78
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"clean": "tsc --build --clean && rm -rf build",
"test:unit": "vitest --exclude tests/examples --exclude tests/e2e",
"test:examples": "vitest --config vitest.examples.config.ts --dir tests/examples",
"test:e2e": "vitest --dir tests/e2e --testTimeout 10000",
"test:e2e": "vitest --dir tests/e2e --testTimeout 20000",
"test": "vitest",
"test:web": "vitest --environment jsdom",
"test:types": "vitest --typecheck --dir src --testNamePattern .*.test-d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ export const Transport = {
memory: `memory`,
http: `http`,
} as const

export type Interface = InterfaceRaw | InterfaceTyped

export type InterfaceRaw = 'raw'

export type InterfaceTyped = 'typed'
24 changes: 3 additions & 21 deletions src/layers/5_request/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectionSetGraphqlMapper } from '../../documentBuilder/SelectGraphQLMapper/__.js'
import type { SelectionSetGraphqlMapper } from '../../documentBuilder/SelectGraphQLMapper/__.js'
import { Anyware } from '../../lib/anyware/__.js'
import type { Grafaid } from '../../lib/grafaid/__.js'
import { getOperationDefinition, OperationTypeToAccessKind, print } from '../../lib/grafaid/document.js'
Expand All @@ -22,7 +22,7 @@ import {
hookNamesOrderedBySequence,
type HookSequence,
} from './hooks.js'
import { Transport } from './types.js'
import { Transport } from './Transport.js'

export const graffleMappedResultToRequest = (
{ document, operationsVariables }: SelectionSetGraphqlMapper.Encoded,
Expand Down Expand Up @@ -62,25 +62,7 @@ export const anyware = Anyware.create<HookSequence, HookMap, Grafaid.FormattedEx
hookNamesOrderedBySequence,
hooks: {
encode: ({ input }) => {
let request: Grafaid.RequestAnalyzedInput

if (input.interfaceType === `raw`) {
request = input.request
} else {
request = graffleMappedResultToRequest(
SelectionSetGraphqlMapper.toGraphQL(input.request.document, {
sddm: input.state.config.schemaMap,
// todo test that when custom scalars are used they are mapped correctly
scalars: input.state.scalars.map,
}),
input.request.operationName,
)
}

return {
...input,
request,
}
return input
},
pack: {
slots: {
Expand Down
21 changes: 2 additions & 19 deletions src/layers/5_request/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import type { FormattedExecutionResult, GraphQLSchema } from 'graphql'
import type { Select } from '../../documentBuilder/Select/__.js'
import type { Grafaid } from '../../lib/grafaid/__.js'
import type { getRequestEncodeSearchParameters, postRequestEncodeBody } from '../../lib/grafaid/http/http.js'
import type { httpMethodGet, httpMethodPost } from '../../lib/http.js'
import type { ClientContext } from '../6_client/fluent.js'
import type { Config } from '../6_client/Settings/Config.js'
import type { MethodModeGetReads, MethodModePost } from '../6_client/transportHttp/request.js'
import type { InterfaceRaw, InterfaceTyped, TransportHttp, TransportMemory } from './types.js'
import type { TransportHttp, TransportMemory } from './Transport.js'

interface HookInputBase {
state: ClientContext
}

type InterfaceInput<TypedProperties = {}, RawProperties = {}> =
| ({
interfaceType: InterfaceTyped
} & TypedProperties)
| ({
interfaceType: InterfaceRaw
} & RawProperties)

// dprint-ignore

type TransportInput<$Config extends Config, $HttpProperties = {}, $MemoryProperties = {}> =
Expand All @@ -44,19 +35,14 @@ type TransportInput<$Config extends Config, $HttpProperties = {}, $MemoryPropert

export type HookDefEncode<$Config extends Config> = {
input:
& { request: Grafaid.RequestAnalyzedInput }
& HookInputBase
& InterfaceInput<
{ request: { document: Select.Document.DocumentNormalized; operationName?: string } },
// { request: Grafaid.RequestInput }
{ request: Grafaid.RequestAnalyzedInput }
>
& TransportInput<$Config>
}

export type HookDefPack<$Config extends Config> = {
input:
& HookInputBase
& InterfaceInput
& TransportInput<
$Config,
// todo why is headers here but not other http request properties?
Expand All @@ -81,7 +67,6 @@ export type HookDefExchange<$Config extends Config> = {
}
input:
& HookInputBase
& InterfaceInput
& TransportInput<
$Config,
{ request: CoreExchangePostRequest | CoreExchangeGetRequest; headers?: HeadersInit },
Expand All @@ -92,7 +77,6 @@ export type HookDefExchange<$Config extends Config> = {
export type HookDefUnpack<$Config extends Config> = {
input:
& HookInputBase
& InterfaceInput
& TransportInput<
$Config,
{ response: Response },
Expand All @@ -103,7 +87,6 @@ export type HookDefUnpack<$Config extends Config> = {
export type HookDefDecode<$Config extends Config> = {
input:
& HookInputBase
& InterfaceInput
& TransportInput<
$Config,
{ response: Response }
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/Settings/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { GraphQLSchema } from 'graphql'
import type { RequireProperties } from '../../../lib/prelude.js'
import type { GlobalRegistry } from '../../../types/GlobalRegistry/GlobalRegistry.js'
import type { SchemaDrivenDataMap } from '../../../types/SchemaDrivenDataMap/__.js'
import type { TransportHttp, TransportMemory } from '../../5_request/types.js'
import type { TransportHttp, TransportMemory } from '../../5_request/Transport.js'
import type { Extension } from '../extension/extension.js'
import type { TransportHttpInput } from '../transportHttp/request.js'
import type { InputStatic } from './Input.js'
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/Settings/InputToConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IsUnknown } from 'type-fest'
import type { ConfigManager } from '../../../lib/config-manager/__.js'
import type { GlobalRegistry } from '../../../types/GlobalRegistry/GlobalRegistry.js'
import { Transport } from '../../5_request/types.js'
import { Transport } from '../../5_request/Transport.js'
import { defaultMethodMode } from '../transportHttp/request.js'
import { outputConfigDefault, type TransportConfigHttp, type TransportConfigMemory } from './Config.js'
import type { InputOutputEnvelopeLonghand, InputStatic, URLInput } from './Input.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GlobalRegistry } from '../../../../types/GlobalRegistry/GlobalRegistry.js'
import type { Transport, TransportMemory } from '../../../5_request/types.js'
import type { Transport, TransportMemory } from '../../../5_request/Transport.js'
import type { TransportHttpInput } from '../../transportHttp/request.js'
import type { Config } from '../Config.js'
import type { NormalizeInput } from '../InputToConfig.js'
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/client.transport-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { serveSchema } from '../../../tests/_/lib/serveSchema.js'
import { Graffle as Pokemon } from '../../../tests/_/schemas/pokemon/graffle/__.js'
import { Graffle } from '../../entrypoints/main.js'
import { ACCEPT_REC, CONTENT_TYPE_REC } from '../../lib/grafaid/http/http.js'
import { Transport } from '../5_request/types.js'
import { Transport } from '../5_request/Transport.js'

const schema = new URL(`https://foo.io/api/graphql`)

Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/client.transport-memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expectTypeOf } from 'vitest'
import { test } from '../../../tests/_/helpers.js'
import { schema } from '../../../tests/_/schemas/kitchen-sink/schema.js'
import { Graffle } from '../../entrypoints/main.js'
import { Transport } from '../5_request/types.js'
import { Transport } from '../5_request/Transport.js'

test(`anyware hooks are typed to memory transport`, () => {
Graffle.create({ schema }).anyware(async ({ encode }) => {
Expand Down
3 changes: 0 additions & 3 deletions src/layers/6_client/gql/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
type TemplateStringsArguments,
} from '../../../lib/template-string.js'
import { RequestCore } from '../../5_request/__.js' // todo
import type { InterfaceRaw } from '../../5_request/types.js'
import { type ClientContext, defineTerminus } from '../fluent.js'
import { handleOutput } from '../handleOutput.js'
import type { Config } from '../Settings/Config.js'
Expand Down Expand Up @@ -37,7 +36,6 @@ export const gqlProperties = defineTerminus((state) => {
return {
gql: (...args: gqlArguments) => {
const { document: query } = resolveGqlArguments(args)
const interfaceType: InterfaceRaw = `raw`
const transportType = state.config.transport.type
const url = state.config.transport.type === `http` ? state.config.transport.url : undefined
const schema = state.config.transport.type === `http` ? undefined : state.config.transport.schema
Expand All @@ -61,7 +59,6 @@ export const gqlProperties = defineTerminus((state) => {
}

const initialInput = {
interfaceType,
transportType,
state,
url,
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 @@ -11,7 +11,7 @@ import {
type Values,
} from '../../lib/prelude.js'
import type { GlobalRegistry } from '../../types/GlobalRegistry/GlobalRegistry.js'
import type { TransportHttp } from '../5_request/types.js'
import type { TransportHttp } from '../5_request/Transport.js'
import type { RunTypeHookOnRequestResult } from './extension/extension.js'
import type { ClientContext } from './fluent.js'
import {
Expand Down
2 changes: 1 addition & 1 deletion src/layers/6_client/requestMethods/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe(`document with two queries`, () => {
const { run } = withTwo
// @ts-expect-error
const error = await run(`boo`).catch((e: unknown) => e) as Errors.ContextualAggregateError
expect(error.cause?.message).toEqual(`Unknown operation named "boo".`)
expect(error.message).toEqual(`Unknown operation named "boo".`)
})
test(`error if no operations provided`, () => {
expect(() => {
Expand Down
25 changes: 14 additions & 11 deletions src/layers/6_client/requestMethods/requestMethods.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { OperationTypeNode } from 'graphql'
import { Select } from '../../../documentBuilder/Select/__.js'
import { SelectionSetGraphqlMapper } from '../../../documentBuilder/SelectGraphQLMapper/__.js'
import type { TypeFunction } from '../../../entrypoints/utilities-for-generated.js'
import type { Fluent } from '../../../lib/fluent/__.js'
import type { Grafaid } from '../../../lib/grafaid/__.js'
import { isSymbol } from '../../../lib/prelude.js'
import type { GlobalRegistry } from '../../../types/GlobalRegistry/GlobalRegistry.js'
import { RequestCore } from '../../5_request/__.js'
import { graffleMappedResultToRequest } from '../../5_request/core.js'
import { type ClientContext, defineTerminus } from '../fluent.js'
import { handleOutput } from '../handleOutput.js'
import type { Config } from '../Settings/Config.js'
Expand Down Expand Up @@ -47,7 +48,7 @@ export const requestMethodsProperties = defineTerminus((state) => {
}
})

export const createMethodDocument = (state: ClientContext) => (document: Select.Document.DocumentObject) => {
const createMethodDocument = (state: ClientContext) => (document: Select.Document.DocumentObject) => {
const documentNormalized = Select.Document.normalizeOrThrow(document)
return {
run: async (maybeOperationName?: string) => {
Expand Down Expand Up @@ -105,28 +106,30 @@ const executeOperation = async (
)
}

export const executeDocument = async (
const executeDocument = async (
state: ClientContext,
document: Select.Document.DocumentNormalized,
operationName?: string,
variables?: Grafaid.Variables,
) => {
const transportType = state.config.transport.type
const interfaceType = `typed`
const url = state.config.transport.type === `http` ? state.config.transport.url : undefined
const schema = state.config.transport.type === `http` ? undefined : state.config.transport.schema

const request = graffleMappedResultToRequest(
SelectionSetGraphqlMapper.toGraphQL(document, {
sddm: state.config.schemaMap,
// todo test that when custom scalars are used they are mapped correctly
scalars: state.scalars.map,
}),
operationName,
)

const initialInput = {
state,
interfaceType,
transportType,
url,
schema,
request: {
document,
operationName,
variables,
},
request,
} as RequestCore.Hooks.HookDefEncode<Config>['input']

const result = await RequestCore.anyware.run({
Expand Down

0 comments on commit 4dc1a78

Please sign in to comment.