Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: result-set decode aliases #1129

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/$/generated-clients/pokemon/_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import './modules/Global.js'

export { create } from './modules/Client.js'
export { isError } from './modules/Error.js'
export { $Index as schemaModel } from './modules/SchemaRuntime.js'
export { Select } from './modules/Select.js'
export * as SelectionSets from './modules/SelectionSets.js'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1727310087052'
'x-sent-at-time': '1727319638284'
},
signal: undefined,
method: 'post',
Expand Down
2 changes: 1 addition & 1 deletion examples/__outputs__/20_output/output_envelope.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
headers: Headers {
'content-type': 'application/graphql-response+json; charset=utf-8',
'content-length': '104',
date: 'Thu, 26 Sep 2024 00:21:27 GMT',
date: 'Thu, 26 Sep 2024 03:00:38 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=5'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'f384fda0c6aa8cdc0600586696d83aac',
parentId: '78959de15b116846',
traceId: '851f3f771b76e4969a2f8c084f85759d',
parentId: 'ebd5237ad56b820c',
traceState: undefined,
name: 'encode',
id: 'e78df0ee2a54acb6',
id: 'ff492c55cf5462ff',
kind: 0,
timestamp: 1727310087760000,
duration: 554.291,
timestamp: 1727319638868000,
duration: 1170.584,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -33,14 +33,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'f384fda0c6aa8cdc0600586696d83aac',
parentId: '78959de15b116846',
traceId: '851f3f771b76e4969a2f8c084f85759d',
parentId: 'ebd5237ad56b820c',
traceState: undefined,
name: 'pack',
id: '88b5c48ab9be7a36',
id: '9174a3424a228e28',
kind: 0,
timestamp: 1727310087845000,
duration: 17482.625,
timestamp: 1727319638871000,
duration: 36417.291,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -57,14 +57,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'f384fda0c6aa8cdc0600586696d83aac',
parentId: '78959de15b116846',
traceId: '851f3f771b76e4969a2f8c084f85759d',
parentId: 'ebd5237ad56b820c',
traceState: undefined,
name: 'exchange',
id: 'd4a611cb0daf3ac1',
id: '85de30bd471a1d11',
kind: 0,
timestamp: 1727310087867000,
duration: 48095.333,
timestamp: 1727319638909000,
duration: 24460.084,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -81,14 +81,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'f384fda0c6aa8cdc0600586696d83aac',
parentId: '78959de15b116846',
traceId: '851f3f771b76e4969a2f8c084f85759d',
parentId: 'ebd5237ad56b820c',
traceState: undefined,
name: 'unpack',
id: '20d213f3a3f86f1d',
id: '81208c873ce84df5',
kind: 0,
timestamp: 1727310087916000,
duration: 1125.833,
timestamp: 1727319638933000,
duration: 1301.541,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -105,14 +105,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'f384fda0c6aa8cdc0600586696d83aac',
parentId: '78959de15b116846',
traceId: '851f3f771b76e4969a2f8c084f85759d',
parentId: 'ebd5237ad56b820c',
traceState: undefined,
name: 'decode',
id: 'c35ae080bfb451be',
id: '0e084f67d12e45c7',
kind: 0,
timestamp: 1727310087917000,
duration: 181.958,
timestamp: 1727319638935000,
duration: 199.208,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -129,14 +129,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'f384fda0c6aa8cdc0600586696d83aac',
traceId: '851f3f771b76e4969a2f8c084f85759d',
parentId: undefined,
traceState: undefined,
name: 'request',
id: '78959de15b116846',
id: 'ebd5237ad56b820c',
kind: 0,
timestamp: 1727310087759000,
duration: 158234.5,
timestamp: 1727319638867000,
duration: 68430.791,
attributes: {},
status: { code: 0 },
events: [],
Expand Down
10 changes: 7 additions & 3 deletions src/layers/2_SelectionSet/indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const indicator = {
...positiveIndicator,
}

export type Any = ClientIndicator | (Directive.$Fields & { $?: Schema.Args<any> })

/**
* Field selection in general, with directives support too.
* If a field directive is given as an indicator then it implies "select this" e.g. `true`/`1`.
Expand All @@ -46,9 +48,11 @@ export type NoArgsIndicator = ClientIndicator | Directive.$Fields

export type NoArgsIndicator$Expanded = UnionExpanded<ClientIndicator | Simplify<Directive.$Fields>>

export type ArgsIndicator<$Args extends Schema.Args<any>> = $Args['isFieldsAllNullable'] extends true
? ({ $?: Args<$Args> } & Directive.$Fields) | ClientIndicator
: { $: Args<$Args> } & Directive.$Fields
// dprint-ignore
export type ArgsIndicator<$Args extends Schema.Args<any>> =
$Args['isFieldsAllNullable'] extends true
? ({ $?: Args<$Args> } & Directive.$Fields) | ClientIndicator
: { $: Args<$Args> } & Directive.$Fields

// dprint-ignore
export type IndicatorForField<$Field extends SomeField> =
Expand Down
7 changes: 5 additions & 2 deletions src/layers/2_SelectionSet/on.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { OmitKeysWithPrefix } from '../../lib/prelude.js'
import type { Any } from './types.js'

export const prefix = `___on_`

Expand Down Expand Up @@ -28,4 +27,8 @@ export const toGraphQLOn = (on: On) => {
return `...on ${on.typeOrFragmentName}`
}

export type OmitOnTypeFragments<$Object extends Any> = OmitKeysWithPrefix<$Object, KeyPrefix>
export type OmitOnTypeFragments<$Object> = OmitKeysWithPrefix<
// @ts-expect-error fixme
$Object,
KeyPrefix
>
1 change: 1 addition & 0 deletions src/layers/2_SelectionSet/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type DocumentObject = {

export type GraphQLRootSelection = { query: GraphQLObjectSelection } | { mutation: GraphQLObjectSelection }

// todo duplicaets the ObjectLike type in other module
export type GraphQLObjectSelection = Record<string, Indicator | SS>

export type SS = {
Expand Down
18 changes: 13 additions & 5 deletions src/layers/2_SelectionSet/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import type { OmitNullableFields, PickNullableFields, Schema } from '../1_Schema
import type { Indicator } from './_indicator.js'
import type { Directive } from './Directive/__.js'

export type Any = object
export type ObjectLike = {
[fieldName: string]: Any
}

export type Any = AnyExceptAlias | AliasInput

export type AnyExceptAlias = ObjectLike | Indicator.Any

export type IsSelectScalarsWildcard<SS> = SS extends { $scalars: Indicator.ClientIndicatorPositive } ? true : false

Expand Down Expand Up @@ -62,15 +68,17 @@ export namespace Bases {

// dprint-ignore

export type AliasInputOne<$SelectionSet = unknown> = [alias: string, selectionSet: $SelectionSet]
export type AliasInputOne<$SelectionSet = AnyExceptAlias> = [alias: string, selectionSet: $SelectionSet]

export type AliasInputMultiple<$SelectionSet = unknown> = [
export type AliasInputMultiple<$SelectionSet = AnyExceptAlias> = [
...AliasInputOne<$SelectionSet>[],
]

export type AliasInput<$SelectionSet = unknown> = AliasInputOne<$SelectionSet> | AliasInputMultiple<$SelectionSet>
export type AliasInput<$SelectionSet = AnyExceptAlias> =
| AliasInputOne<$SelectionSet>
| AliasInputMultiple<$SelectionSet>

export type AliasNormalized<$SelectionSet = unknown> = [
export type AliasNormalized<$SelectionSet = AnyExceptAlias> = [
alias: string,
selectionSet: $SelectionSet,
][]
Expand Down
2 changes: 1 addition & 1 deletion src/layers/3_ResultSet/__.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as ResultSet from './types.js'
export * as ResultSet from './infer.js'
77 changes: 0 additions & 77 deletions src/layers/3_ResultSet/customScalars.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/layers/3_ResultSet/decode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect, test } from 'vitest'
import { Graffle } from '../../../tests/_/schema/generated/__.js'
import { $Index } from '../../../tests/_/schema/generated/modules/SchemaRuntime.js'
import type * as SelectionSets from '../../../tests/_/schema/generated/modules/SelectionSets.js'
import { decode } from './decode.js'

test(`simple`, () => {
const selectionSet = Graffle.Select.Query({ object: { id: true } })
const data = { object: { id: `x` } }
// @ts-expect-error fixme
expect(decode($Index.Root.Query, selectionSet, data)).toEqual(data)
})

test.each<[selectionSet: SelectionSets.Query, data: object]>([
[{ id: [`x`, true] }, { x: `foo` }],
[{ listInt: [`x`, true] }, { x: [1] }],
[{ objectNested: { object: { id: [`x`, true] } } }, { objectNested: { object: { x: `x` } } }],
])(`alias`, (selectionSet, data) => {
// @ts-expect-error fixme
expect(decode($Index.Root.Query, selectionSet, data)).toEqual(data)
})
Loading