From 5f1340118c83ae42be767c7e2824b07b59dd99bd Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Sat, 20 Apr 2024 07:44:01 -0400 Subject: [PATCH] fix(ts-client): no select root types if not in schema --- src/entrypoints/alpha/client.ts | 3 +-- src/select.test-d.ts | 32 ++++++++++++++++++++++++++++++++ src/select.test.ts | 14 +++++++------- src/select.ts | 8 ++++++-- 4 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/select.test-d.ts diff --git a/src/entrypoints/alpha/client.ts b/src/entrypoints/alpha/client.ts index a36e224ab..9cf19885e 100644 --- a/src/entrypoints/alpha/client.ts +++ b/src/entrypoints/alpha/client.ts @@ -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' diff --git a/src/select.test-d.ts b/src/select.test-d.ts new file mode 100644 index 000000000..8231b5a55 --- /dev/null +++ b/src/select.test-d.ts @@ -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() + 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 + expectTypeOf<$Parameters>().toEqualTypeOf<[SelectionSet.Object]>() + expectTypeOf(select.Bar({ int: true })).toEqualTypeOf<{ int: true }>() + }) +}) + +describe(`create`, () => { + const select = create() + 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 + }) +}) diff --git a/src/select.test.ts b/src/select.test.ts index 3801c71e2..9bfeaaf4b 100644 --- a/src/select.test.ts +++ b/src/select.test.ts @@ -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() +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() +test(`has type safe methods`, () => { expect(select.Bar({ ___: { $defer: true, int: true } })).toEqual({ ___: { $defer: true, int: true } }) }) diff --git a/src/select.ts b/src/select.ts index 8ab8f5f00..6d5122069 100644 --- a/src/select.ts +++ b/src/select.ts @@ -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 } @@ -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 = idProxy