Skip to content

Commit

Permalink
improve(document-builder): no simplify custom scalars (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 28, 2024
1 parent 0f10bad commit 76c8994
Show file tree
Hide file tree
Showing 22 changed files with 474 additions and 249 deletions.
9 changes: 4 additions & 5 deletions src/documentBuilder/InferResult/OutputObject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ConditionalSimplify } from 'type-fest/source/conditional-simplify.js'
import type { StringKeyof } from '../../lib/prelude.js'
import type { SimplifyExcept, StringKeyof } from '../../lib/prelude.js'
import type { TSErrorDescriptive } from '../../lib/ts-error.js'
import type { Schema } from '../../types/Schema/__.js'
import type { Select } from '../Select/__.js'
Expand All @@ -11,10 +10,10 @@ export type OutputObject<$SelectionSet, $Schema extends Schema, $Node extends Sc
Select.SelectScalarsWildcard.IsSelectScalarsWildcard<$SelectionSet> extends true
// todo what about when scalars wildcard is combined with other fields like relations?
? ScalarsWildcard<$SelectionSet, $Schema, $Node>
: ConditionalSimplify<
: SimplifyExcept<
$Schema['scalars']['typesDecoded'],
& SelectionNonSelectAlias<$SelectionSet, $Schema, $Node>
& Alias<$Schema, $Node, $SelectionSet>,
$Schema['scalars']['typesDecoded']
& Alias<$Schema, $Node, $SelectionSet>
>

type SelectionNonSelectAlias<$SelectionSet, $Schema extends Schema, $Node extends Schema.OutputObject> = {
Expand Down
4 changes: 2 additions & 2 deletions src/documentBuilder/InferResult/__.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Date } from '../../../tests/_/fixtures/scalars.js'
import type { DateScalar } from '../../../tests/_/fixtures/scalars.js'
import type { db } from '../../../tests/_/schemas/db.js'
import type { Schema } from '../../../tests/_/schemas/kitchen-sink/graffle/modules/schema.js'
import type * as SelectionSets from '../../../tests/_/schemas/kitchen-sink/graffle/modules/selection-sets.js'
Expand All @@ -9,7 +9,7 @@ import type { PickSelectsPositiveIndicatorAndNotSelectAlias } from './OutputObje

type $<$SelectionSet extends SelectionSets.Query> = InferResult.OperationQuery<$SelectionSet, Schema>

type $Registry = Registry.AddScalar<Registry.Empty, typeof Date>
type $Registry = Registry.AddScalar<Registry.Empty, typeof DateScalar>

type $WithDate<$SelectionSet extends SelectionSets.Query<$Registry>> = InferResult.OperationQuery<
$SelectionSet,
Expand Down
10 changes: 6 additions & 4 deletions src/documentBuilder/SelectGraphQLMapper/toGraphQL.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { Date } from '../../../tests/_/fixtures/scalars.js'
import { DateScalar } from '../../../tests/_/fixtures/scalars.js'
import { db } from '../../../tests/_/schemas/db.js'
import { schemaDrivenDataMap } from '../../../tests/_/schemas/kitchen-sink/graffle/modules/schema-driven-data-map.js'
import type * as SelectionSets from '../../../tests/_/schemas/kitchen-sink/graffle/modules/selection-sets.js'
Expand All @@ -15,7 +15,9 @@ type CasesDescriptiveQuery = [
]
const testEachQueryWithDescription = test.for.bind(test)<CasesDescriptiveQuery>

type QueryWithDate = SelectionSets.Query<Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof Date>>
type QueryWithDate = SelectionSets.Query<
Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof DateScalar>
>

type CasesDescriptiveQueryWithCustomScalars = [
description: string,
Expand Down Expand Up @@ -142,5 +144,5 @@ const customScalarWithCodecCases = testEachQueryWithDescriptionWithCustomScalars
[`args - custom scalar - input object field` , { dateArgInputObject: { $: { input: { idRequired: ``, dateRequired: db.date0, date: db.date1 } } } }],
[`args - custom scalar - nested input object field` , { InputObjectNested: { $: { input: { InputObject: { idRequired: ``, dateRequired: db.date0, date: db.date1 } } } } }],
])
customScalarWithCodecCases(...tester({ variables: true, scalars: { Date } }))
customScalarWithCodecCases(...tester({ variables: false, scalars: { Date } }))
customScalarWithCodecCases(...tester({ variables: true, scalars: { Date: DateScalar } }))
customScalarWithCodecCases(...tester({ variables: false, scalars: { Date: DateScalar } }))
2 changes: 1 addition & 1 deletion src/entrypoints/utilities-for-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type {
} from '../layers/6_client/handleOutput.js'
export { type DocumentRunner } from '../layers/6_client/requestMethods/document.js'
export type { Config } from '../layers/6_client/Settings/Config.js'
export { type Exact, type ExactNonEmpty, type UnionExpanded } from '../lib/prelude.js'
export { type Exact, type ExactNonEmpty, type SimplifyExcept, type UnionExpanded } from '../lib/prelude.js'
export { TypeFunction } from '../lib/type-function/__.js'
export { type GlobalRegistry } from '../types/GlobalRegistry/GlobalRegistry.js'
export { Schema } from '../types/Schema/__.js'
Expand Down
12 changes: 6 additions & 6 deletions src/extensions/CustomScalars/decode.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect } from 'vitest'
import { Date } from '../../../tests/_/fixtures/scalars.js'
import { DateScalar } from '../../../tests/_/fixtures/scalars.js'
import { createResponse, test } from '../../../tests/_/helpers.js'
import { db } from '../../../tests/_/schemas/db.js'
import type { Graffle } from '../../../tests/_/schemas/kitchen-sink/graffle/__.js'
Expand All @@ -9,7 +9,7 @@ import { Grafaid } from '../../lib/grafaid/__.js'
import type { Schema } from '../../types/Schema/__.js'

type QueryWithDate = Graffle.SelectionSets.Query<
Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof Date>
Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof DateScalar>
>

type TestCase = [
Expand All @@ -26,7 +26,7 @@ const withBatch: TestCaseWith = [
{},
async ([_, query, responseData, expectedData], { fetch, kitchenSinkHttp: kitchenSink }) => {
fetch.mockResolvedValueOnce(createResponse({ data: responseData }))
expect(await kitchenSink.scalar(Date).query.$batch(query)).toEqual(expectedData)
expect(await kitchenSink.scalar(DateScalar).query.$batch(query)).toEqual(expectedData)
},
]

Expand All @@ -38,7 +38,7 @@ const withGqlDocument: TestCaseWith = [
const { document } = SelectionSetGraphqlMapper.toGraphQL(
Select.Document.createDocumentNormalizedFromQuerySelection(query as any),
)
expect(await kitchenSink.scalar(Date).gql(document).send()).toEqual(expectedData)
expect(await kitchenSink.scalar(DateScalar).gql(document).send()).toEqual(expectedData)
},
]

Expand All @@ -50,7 +50,7 @@ const withGqlString: TestCaseWith = [
const { document } = SelectionSetGraphqlMapper.toGraphQL(
Select.Document.normalizeOrThrow({ query: { foo: query as any } }),
)
expect(await kitchenSink.scalar(Date).gql(Grafaid.Document.print(document)).send()).toEqual(expectedData)
expect(await kitchenSink.scalar(DateScalar).gql(Grafaid.Document.print(document)).send()).toEqual(expectedData)
},
]

Expand Down Expand Up @@ -107,7 +107,7 @@ describe(`$batch`, () => {
describe(`object field in union`, () => {
testUnionCases(`%s`, async ([_, query, responseData, expectedData], { fetch, kitchenSinkHttp: kitchenSink }) => {
fetch.mockResolvedValueOnce(createResponse({ data: responseData }))
expect(await kitchenSink.scalar(Date).query.$batch(query)).toEqual(expectedData)
expect(await kitchenSink.scalar(DateScalar).query.$batch(query)).toEqual(expectedData)
})
})
})
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/CustomScalars/encode.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'vitest'
import { Date } from '../../../tests/_/fixtures/scalars.js'
import { DateScalar } from '../../../tests/_/fixtures/scalars.js'
import { test } from '../../../tests/_/helpers.js'
import { db } from '../../../tests/_/schemas/db.js'
import type { Graffle } from '../../../tests/_/schemas/kitchen-sink/graffle/__.js'
Expand All @@ -11,7 +11,7 @@ import { Grafaid } from '../../lib/grafaid/__.js'
import type { Schema } from '../../types/Schema/__.js'

type QueryWithDate = Graffle.SelectionSets.Query<
Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof Date>
Schema.Scalar.Registry.AddScalar<Schema.Scalar.Registry.Empty, typeof DateScalar>
>

type TestCase = [
Expand All @@ -36,7 +36,7 @@ const testCases = test.for<TestCase>([
])

testCases(`%s`, async ([_, query, expectedVariables], { kitchenSink }) => {
const g = kitchenSink.use(Spy()).scalar(Date)
const g = kitchenSink.use(Spy()).scalar(DateScalar)
const { document, operationsVariables } = SelectionSetGraphqlMapper.toGraphQL(
Select.Document.createDocumentNormalizedFromQuerySelection(query as any),
{
Expand Down
Loading

0 comments on commit 76c8994

Please sign in to comment.