Skip to content

Commit

Permalink
feat(ts-client): select helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 15, 2024
1 parent 420a389 commit 98cb065
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Schema/core/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export interface Index {
Subscription: null | Output.Object$2
}
objects: Record<string, Output.Object$2>
// todo unused?
unions: Record<string, Output.Union>
interfaces: Record<string, Output.Interface>
}
4 changes: 2 additions & 2 deletions src/client/SelectionSet/SelectionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Arguments<$Field extends SomeField> =
{}

// dprint-ignore
type Interface<$Node extends Schema.Interface, $Index extends Schema.Index> =
export type Interface<$Node extends Schema.Interface, $Index extends Schema.Index> =
& InterfaceDistributed<$Node['implementors'][number], $Index>
& Fields<
& $Node['fields']
Expand All @@ -109,7 +109,7 @@ type InterfaceDistributed<$Node extends Schema.Object$2, $Index extends Schema.I
: never

// dprint-ignore
type Union<$Node extends Schema.Union, $Index extends Schema.Index> =
export type Union<$Node extends Schema.Union, $Index extends Schema.Index> =
& UnionDistributed<$Node['members'][number], $Index>
& { __typename?: NoArgsIndicator }

Expand Down
2 changes: 1 addition & 1 deletion src/client/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import { beforeEach, describe, expect, test } from 'vitest'
import { expect, test } from 'vitest'
import { setupMockServer } from '../../tests/raw/__helpers.js'
import type { Index } from '../../tests/ts/_/schema/generated/Index.js'
import { $Index as schemaIndex } from '../../tests/ts/_/schema/generated/SchemaRuntime.js'
Expand Down
2 changes: 2 additions & 0 deletions src/entrypoints/alpha/client.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from '../../client/client.js'
// todo need to export a generated version
export { create as createSelect } from '../../select.js'
3 changes: 3 additions & 0 deletions src/generator/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const generateIndex = (config: Config) => {
unions: Code.objectFromEntries(
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${namespace}.Union.${_.name}`]),
),
interfaces: Code.objectFromEntries(
config.typeMapByKind.GraphQLInterfaceType.map(_ => [_.name, `${namespace}.Interface.${_.name}`]),
),
}),
),
)
Expand Down
13 changes: 13 additions & 0 deletions src/select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, it } from 'vitest'
import type { Index } from '../tests/ts/_/schema/generated/Index.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
})

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

// todo test
// dprint-ignore
export type Select<$Index extends Schema.Index> =
& {
[$RootTypeName in Schema.RootTypeName]:
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Root<$Index, $RootTypeName>>) =>
$SelectionSet
}
& {
[$Name in keyof $Index['objects']]:
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Object<$Index['objects'][$Name], $Index>>) =>
$SelectionSet
}
& {
[$Name in keyof $Index['unions']]:
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Union<$Index['unions'][$Name], $Index>>) =>
$SelectionSet
}
& {
[$Name in keyof Schema.Index['interfaces']]:
<$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Interface<$Index['interfaces'][$Name], $Index>>) =>
$SelectionSet
}

export const create = <$Index extends Schema.Index>(): Select<$Index> => {
return idProxy as any
}

const idProxy = new Proxy({}, {
get: () => (value: unknown) => value,
})
4 changes: 4 additions & 0 deletions tests/ts/_/schema/generated/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export interface Index {
FooBarUnion: Schema.Union.FooBarUnion
lowerCaseUnion: Schema.Union.lowerCaseUnion
}
interfaces: {
DateInterface1: Schema.Interface.DateInterface1
Interface: Schema.Interface.Interface
}
}

0 comments on commit 98cb065

Please sign in to comment.