Skip to content

Commit 5f13401

Browse files
committed
fix(ts-client): no select root types if not in schema
1 parent 893a5e0 commit 5f13401

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

src/entrypoints/alpha/client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from '../../client/client.js'
2-
// todo need to export a generated version
3-
export { create as createSelect } from '../../select.js'
2+
export { create as createSelect, select } from '../../select.js'

src/select.test-d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, expectTypeOf, it, test } from 'vitest'
2+
import type { Index } from '../tests/_/schema/schema.js'
3+
import type * as SchemaQueryOnly from '../tests/_/schemaQueryOnly/generated/Index.js'
4+
import type { SelectionSet } from './client/SelectionSet/__.js'
5+
import { create } from './select.js'
6+
7+
describe(`select`, () => {
8+
const select = create<Index>()
9+
it(`returns the input for any method name`, () => {
10+
const s = select as any // eslint-disable-line
11+
expect(s.anything(1)).toEqual(1) // eslint-disable-line
12+
})
13+
14+
it(`has type safe methods`, () => {
15+
// @ts-expect-error ID issue
16+
type $Parameters = Parameters<typeof select.Bar>
17+
expectTypeOf<$Parameters>().toEqualTypeOf<[SelectionSet.Object<Index['objects']['Bar'], Index>]>()
18+
expectTypeOf(select.Bar({ int: true })).toEqualTypeOf<{ int: true }>()
19+
})
20+
})
21+
22+
describe(`create`, () => {
23+
const select = create<SchemaQueryOnly.Index>()
24+
test(`does not have root types if not in schema`, () => {
25+
// fine
26+
select.Query
27+
// @ts-expect-error no mutation in schema
28+
select.Mutation
29+
// @ts-expect-error no mutation in schema
30+
select.Subscription
31+
})
32+
})

src/select.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { expect, it } from 'vitest'
2-
import type { Index } from '../tests/ts/_/schema/generated/Index.js'
1+
import { expect, test } from 'vitest'
2+
import type { Index } from '../tests/_/schema/schema.js'
33
import { create } from './select.js'
44

5-
it(`returns the input for any method name`, () => {
6-
const select = create() as any // eslint-disable-line
7-
expect(select.anything(1)).toEqual(1) // eslint-disable-line
5+
const select = create<Index>()
6+
test(`returns the input for any method name`, () => {
7+
const s = select as any // eslint-disable-line
8+
expect(s.anything(1)).toEqual(1) // eslint-disable-line
89
})
910

10-
it(`has type safe methods`, () => {
11-
const select = create<Index>()
11+
test(`has type safe methods`, () => {
1212
expect(select.Bar({ ___: { $defer: true, int: true } })).toEqual({ ___: { $defer: true, int: true } })
1313
})

src/select.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { SelectionSet } from './client/SelectionSet/__.js'
2+
import type { RootTypeName } from './lib/graphql.js'
23
import type { Exact } from './lib/prelude.js'
34
import type { Schema } from './Schema/__.js'
45

5-
// todo test
66
// dprint-ignore
77
type TypeSelectionSets<$Index extends Schema.Index> =
88
& {
9-
[$RootTypeName in Schema.RootTypeName]:
9+
[$RootTypeName in RootTypeName as $RootTypeName extends keyof $Index['Root'] ? $Index['Root'][$RootTypeName] extends null ? never : $RootTypeName:never ]:
1010
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Root<$Index, $RootTypeName>>) =>
1111
$SelectionSet
1212
}
@@ -33,3 +33,7 @@ export const create = <$Index extends Schema.Index>(): TypeSelectionSets<$Index>
3333
const idProxy = new Proxy({}, {
3434
get: () => (value: unknown) => value,
3535
})
36+
37+
// eslint-disable-next-line
38+
// @ts-ignore generated types
39+
export const select: TypeSelectionSets<NamedSchemas['default']['index']> = idProxy

0 commit comments

Comments
 (0)