Skip to content

Commit

Permalink
fix(ts-client): no select root types if not in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 20, 2024
1 parent 893a5e0 commit 5f13401
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/entrypoints/alpha/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from '../../client/client.js'
// todo need to export a generated version
export { create as createSelect } from '../../select.js'
export { create as createSelect, select } from '../../select.js'
32 changes: 32 additions & 0 deletions src/select.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, expectTypeOf, it, test } from 'vitest'
import type { Index } from '../tests/_/schema/schema.js'
import type * as SchemaQueryOnly from '../tests/_/schemaQueryOnly/generated/Index.js'
import type { SelectionSet } from './client/SelectionSet/__.js'
import { create } from './select.js'

describe(`select`, () => {
const select = create<Index>()
it(`returns the input for any method name`, () => {
const s = select as any // eslint-disable-line
expect(s.anything(1)).toEqual(1) // eslint-disable-line
})

it(`has type safe methods`, () => {
// @ts-expect-error ID issue
type $Parameters = Parameters<typeof select.Bar>
expectTypeOf<$Parameters>().toEqualTypeOf<[SelectionSet.Object<Index['objects']['Bar'], Index>]>()
expectTypeOf(select.Bar({ int: true })).toEqualTypeOf<{ int: true }>()
})
})

describe(`create`, () => {
const select = create<SchemaQueryOnly.Index>()
test(`does not have root types if not in schema`, () => {
// fine
select.Query
// @ts-expect-error no mutation in schema
select.Mutation
// @ts-expect-error no mutation in schema
select.Subscription
})
})
14 changes: 7 additions & 7 deletions src/select.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect, it } from 'vitest'
import type { Index } from '../tests/ts/_/schema/generated/Index.js'
import { expect, test } from 'vitest'
import type { Index } from '../tests/_/schema/schema.js'
import { create } from './select.js'

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

it(`has type safe methods`, () => {
const select = create<Index>()
test(`has type safe methods`, () => {
expect(select.Bar({ ___: { $defer: true, int: true } })).toEqual({ ___: { $defer: true, int: true } })
})
8 changes: 6 additions & 2 deletions src/select.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { SelectionSet } from './client/SelectionSet/__.js'
import type { RootTypeName } from './lib/graphql.js'
import type { Exact } from './lib/prelude.js'
import type { Schema } from './Schema/__.js'

// todo test
// dprint-ignore
type TypeSelectionSets<$Index extends Schema.Index> =
& {
[$RootTypeName in Schema.RootTypeName]:
[$RootTypeName in RootTypeName as $RootTypeName extends keyof $Index['Root'] ? $Index['Root'][$RootTypeName] extends null ? never : $RootTypeName:never ]:
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Root<$Index, $RootTypeName>>) =>
$SelectionSet
}
Expand All @@ -33,3 +33,7 @@ export const create = <$Index extends Schema.Index>(): TypeSelectionSets<$Index>
const idProxy = new Proxy({}, {
get: () => (value: unknown) => value,
})

// eslint-disable-next-line
// @ts-ignore generated types
export const select: TypeSelectionSets<NamedSchemas['default']['index']> = idProxy

0 comments on commit 5f13401

Please sign in to comment.