Skip to content

Commit a35be0a

Browse files
authored
feat(ts-client): select type utilities (#785)
1 parent 0521957 commit a35be0a

26 files changed

Lines changed: 570 additions & 69 deletions

File tree

src/client/ResultSet/ResultSet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export type Object$<$SelectionSet, $Node extends Schema.Output.Object$2, $Index
4343
}>
4444

4545
// dprint-ignore
46-
type Union<$SelectionSet, $Node extends Schema.Output.Union, $Index extends Schema.Index> =
46+
export type Union<$SelectionSet, $Node extends Schema.Output.Union, $Index extends Schema.Index> =
4747
OnTypeFragment<$SelectionSet,$Node['members'][number], $Index>
4848

4949
// dprint-ignore
50-
type Interface<$SelectionSet, $Node extends Schema.Output.Interface, $Index extends Schema.Index> =
50+
export type Interface<$SelectionSet, $Node extends Schema.Output.Interface, $Index extends Schema.Index> =
5151
OnTypeFragment<$SelectionSet, $Node['implementors'][number], $Index>
5252

5353
// dprint-ignore

src/entrypoints/alpha/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export { ResultSet } from '../../client/ResultSet/__.js'
2+
export { SelectionSet } from '../../client/SelectionSet/__.js'
13
export * from '../../Schema/__.js'

src/generator/__snapshots__/files.test.ts.snap

Lines changed: 225 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,130 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`generates types from GraphQL SDL file 1`] = `
4-
"import type * as $ from '../../../../../src/Schema/__.js'
4+
"import { ResultSet, SelectionSet } from '../../../../../src/entrypoints/alpha/schema.js'
5+
import { Index } from './Index.js'
6+
7+
// Root Types
8+
// ----------
9+
10+
export type Query<$SelectionSet extends SelectionSet.Root<Index, 'Query'>> = ResultSet.Root<
11+
$SelectionSet,
12+
Index,
13+
'Query'
14+
>
15+
16+
// Object Types
17+
// ------------
18+
19+
export type DateObject1<$SelectionSet extends SelectionSet.Object<Index['objects']['DateObject1'], Index>> =
20+
ResultSet.Object$<$SelectionSet, Index['objects']['DateObject1'], Index>
21+
22+
export type DateObject2<$SelectionSet extends SelectionSet.Object<Index['objects']['DateObject2'], Index>> =
23+
ResultSet.Object$<$SelectionSet, Index['objects']['DateObject2'], Index>
24+
25+
export type ObjectUnion<$SelectionSet extends SelectionSet.Object<Index['objects']['ObjectUnion'], Index>> =
26+
ResultSet.Object$<$SelectionSet, Index['objects']['ObjectUnion'], Index>
27+
28+
export type Foo<$SelectionSet extends SelectionSet.Object<Index['objects']['Foo'], Index>> = ResultSet.Object$<
29+
$SelectionSet,
30+
Index['objects']['Foo'],
31+
Index
32+
>
33+
34+
export type Bar<$SelectionSet extends SelectionSet.Object<Index['objects']['Bar'], Index>> = ResultSet.Object$<
35+
$SelectionSet,
36+
Index['objects']['Bar'],
37+
Index
38+
>
39+
40+
export type ObjectNested<$SelectionSet extends SelectionSet.Object<Index['objects']['ObjectNested'], Index>> =
41+
ResultSet.Object$<$SelectionSet, Index['objects']['ObjectNested'], Index>
42+
43+
export type lowerCaseObject<$SelectionSet extends SelectionSet.Object<Index['objects']['lowerCaseObject'], Index>> =
44+
ResultSet.Object$<$SelectionSet, Index['objects']['lowerCaseObject'], Index>
45+
46+
export type lowerCaseObject2<$SelectionSet extends SelectionSet.Object<Index['objects']['lowerCaseObject2'], Index>> =
47+
ResultSet.Object$<$SelectionSet, Index['objects']['lowerCaseObject2'], Index>
48+
49+
export type Object1<$SelectionSet extends SelectionSet.Object<Index['objects']['Object1'], Index>> = ResultSet.Object$<
50+
$SelectionSet,
51+
Index['objects']['Object1'],
52+
Index
53+
>
54+
55+
export type Object1ImplementingInterface<
56+
$SelectionSet extends SelectionSet.Object<Index['objects']['Object1ImplementingInterface'], Index>,
57+
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object1ImplementingInterface'], Index>
58+
59+
export type Object2ImplementingInterface<
60+
$SelectionSet extends SelectionSet.Object<Index['objects']['Object2ImplementingInterface'], Index>,
61+
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object2ImplementingInterface'], Index>
62+
63+
// Union Types
64+
// -----------
65+
66+
export type DateUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['DateUnion'], Index>> = ResultSet.Union<
67+
$SelectionSet,
68+
Index['unions']['DateUnion'],
69+
Index
70+
>
71+
72+
export type FooBarUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['FooBarUnion'], Index>> =
73+
ResultSet.Union<$SelectionSet, Index['unions']['FooBarUnion'], Index>
74+
75+
export type lowerCaseUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['lowerCaseUnion'], Index>> =
76+
ResultSet.Union<$SelectionSet, Index['unions']['lowerCaseUnion'], Index>
77+
78+
// Interface Types
79+
// ---------------
80+
81+
export type DateInterface1<$SelectionSet extends SelectionSet.Interface<Index['interfaces']['DateInterface1'], Index>> =
82+
ResultSet.Interface<$SelectionSet, Index['interfaces']['DateInterface1'], Index>
83+
84+
export type Interface<$SelectionSet extends SelectionSet.Interface<Index['interfaces']['Interface'], Index>> =
85+
ResultSet.Interface<$SelectionSet, Index['interfaces']['Interface'], Index>
86+
"
87+
`;
88+
89+
exports[`generates types from GraphQL SDL file 2`] = `
90+
"/* eslint-disable */
91+
92+
import type * as Schema from './SchemaBuildtime.js'
93+
94+
export interface Index {
95+
Root: {
96+
Query: Schema.Root.Query
97+
Mutation: null
98+
Subscription: null
99+
}
100+
objects: {
101+
DateObject1: Schema.Object.DateObject1
102+
DateObject2: Schema.Object.DateObject2
103+
ObjectUnion: Schema.Object.ObjectUnion
104+
Foo: Schema.Object.Foo
105+
Bar: Schema.Object.Bar
106+
ObjectNested: Schema.Object.ObjectNested
107+
lowerCaseObject: Schema.Object.lowerCaseObject
108+
lowerCaseObject2: Schema.Object.lowerCaseObject2
109+
Object1: Schema.Object.Object1
110+
Object1ImplementingInterface: Schema.Object.Object1ImplementingInterface
111+
Object2ImplementingInterface: Schema.Object.Object2ImplementingInterface
112+
}
113+
unions: {
114+
DateUnion: Schema.Union.DateUnion
115+
FooBarUnion: Schema.Union.FooBarUnion
116+
lowerCaseUnion: Schema.Union.lowerCaseUnion
117+
}
118+
interfaces: {
119+
DateInterface1: Schema.Interface.DateInterface1
120+
Interface: Schema.Interface.Interface
121+
}
122+
}
123+
"
124+
`;
125+
126+
exports[`generates types from GraphQL SDL file 3`] = `
127+
"import type * as $ from '../../../../../src/entrypoints/alpha/schema.js'
5128
import type * as $Scalar from './Scalar.ts'
6129
7130
// ------------------------------------------------------------ //
@@ -296,7 +419,7 @@ export namespace Union {
296419
"
297420
`;
298421
299-
exports[`generates types from GraphQL SDL file 2`] = `
422+
exports[`generates types from GraphQL SDL file 4`] = `
300423
"import type * as CustomScalar from '../../../../_/customScalarCodecs.js'
301424
302425
declare global {
@@ -310,10 +433,10 @@ export * from '../../../../_/customScalarCodecs.js'
310433
"
311434
`;
312435
313-
exports[`generates types from GraphQL SDL file 3`] = `
436+
exports[`generates types from GraphQL SDL file 5`] = `
314437
"/* eslint-disable */
315438
316-
import * as $ from '../../../../../src/Schema/__.js'
439+
import * as $ from '../../../../../src/entrypoints/alpha/schema.js'
317440
import * as $Scalar from './Scalar.js'
318441
319442
export const ABCEnum = $.Enum(\`ABCEnum\`, [\`A\`, \`B\`, \`C\`])
@@ -530,7 +653,101 @@ export const $Index = {
530653
`;
531654
532655
exports[`schema2 1`] = `
533-
"import type * as $ from '../../../../src/Schema/__.js'
656+
"import { ResultSet, SelectionSet } from '../../../../src/entrypoints/alpha/schema.js'
657+
import { Index } from './Index.js'
658+
659+
// Root Types
660+
// ----------
661+
662+
export type Mutation<$SelectionSet extends SelectionSet.Root<Index, 'Mutation'>> = ResultSet.Root<
663+
$SelectionSet,
664+
Index,
665+
'Mutation'
666+
>
667+
668+
export type Query<$SelectionSet extends SelectionSet.Root<Index, 'Query'>> = ResultSet.Root<
669+
$SelectionSet,
670+
Index,
671+
'Query'
672+
>
673+
674+
// Object Types
675+
// ------------
676+
677+
export type Bar<$SelectionSet extends SelectionSet.Object<Index['objects']['Bar'], Index>> = ResultSet.Object$<
678+
$SelectionSet,
679+
Index['objects']['Bar'],
680+
Index
681+
>
682+
683+
export type DateObject1<$SelectionSet extends SelectionSet.Object<Index['objects']['DateObject1'], Index>> =
684+
ResultSet.Object$<$SelectionSet, Index['objects']['DateObject1'], Index>
685+
686+
export type Foo<$SelectionSet extends SelectionSet.Object<Index['objects']['Foo'], Index>> = ResultSet.Object$<
687+
$SelectionSet,
688+
Index['objects']['Foo'],
689+
Index
690+
>
691+
692+
export type Object1<$SelectionSet extends SelectionSet.Object<Index['objects']['Object1'], Index>> = ResultSet.Object$<
693+
$SelectionSet,
694+
Index['objects']['Object1'],
695+
Index
696+
>
697+
698+
export type Object1ImplementingInterface<
699+
$SelectionSet extends SelectionSet.Object<Index['objects']['Object1ImplementingInterface'], Index>,
700+
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object1ImplementingInterface'], Index>
701+
702+
export type Object2ImplementingInterface<
703+
$SelectionSet extends SelectionSet.Object<Index['objects']['Object2ImplementingInterface'], Index>,
704+
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object2ImplementingInterface'], Index>
705+
706+
// Union Types
707+
// -----------
708+
709+
export type FooBarUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['FooBarUnion'], Index>> =
710+
ResultSet.Union<$SelectionSet, Index['unions']['FooBarUnion'], Index>
711+
712+
// Interface Types
713+
// ---------------
714+
715+
export type Interface<$SelectionSet extends SelectionSet.Interface<Index['interfaces']['Interface'], Index>> =
716+
ResultSet.Interface<$SelectionSet, Index['interfaces']['Interface'], Index>
717+
"
718+
`;
719+
720+
exports[`schema2 2`] = `
721+
"/* eslint-disable */
722+
723+
import type * as Schema from './SchemaBuildtime.js'
724+
725+
export interface Index {
726+
Root: {
727+
Query: Schema.Root.Query
728+
Mutation: Schema.Root.Mutation
729+
Subscription: null
730+
}
731+
objects: {
732+
Bar: Schema.Object.Bar
733+
DateObject1: Schema.Object.DateObject1
734+
Foo: Schema.Object.Foo
735+
Object1: Schema.Object.Object1
736+
Object1ImplementingInterface: Schema.Object.Object1ImplementingInterface
737+
Object2ImplementingInterface: Schema.Object.Object2ImplementingInterface
738+
}
739+
unions: {
740+
FooBarUnion: Schema.Union.FooBarUnion
741+
}
742+
interfaces: {
743+
Interface: Schema.Interface.Interface
744+
}
745+
}
746+
"
747+
`;
748+
749+
exports[`schema2 3`] = `
750+
"import type * as $ from '../../../../src/entrypoints/alpha/schema.js'
534751
import type * as $Scalar from './Scalar.ts'
535752
536753
// ------------------------------------------------------------ //
@@ -659,7 +876,7 @@ export namespace Union {
659876
"
660877
`;
661878
662-
exports[`schema2 2`] = `
879+
exports[`schema2 4`] = `
663880
"import type * as CustomScalar from '../../customScalarCodecs.js'
664881
665882
declare global {
@@ -673,10 +890,10 @@ export * from '../../customScalarCodecs.js'
673890
"
674891
`;
675892
676-
exports[`schema2 3`] = `
893+
exports[`schema2 5`] = `
677894
"/* eslint-disable */
678895
679-
import * as $ from '../../../../src/Schema/__.js'
896+
import * as $ from '../../../../src/entrypoints/alpha/schema.js'
680897
import * as $Scalar from './Scalar.js'
681898
682899
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { Code } from '../../lib/Code.js'
22
import { hasMutation, hasQuery, hasSubscription } from '../../lib/graphql.js'
3-
import type { Config } from './code.js'
3+
import type { Config } from './generateCode.js'
4+
import { moduleNameSchemaBuildtime } from './SchemaBuildtime2.js'
5+
6+
export const moduleNameIndex = `Index`
47

58
export const generateIndex = (config: Config) => {
69
const namespace = `Schema`
710
const code = []
811
code.push(`/* eslint-disable */\n`)
9-
code.push(`import type * as ${namespace} from './SchemaBuildtime.js'\n`)
12+
code.push(`import type * as ${namespace} from './${moduleNameSchemaBuildtime}.js'\n`)
1013
code.push(Code.export$(
1114
Code.interface$(
1215
`Index`,
@@ -31,5 +34,8 @@ export const generateIndex = (config: Config) => {
3134
),
3235
))
3336

34-
return code.join(`\n`)
37+
return {
38+
code: code.join(`\n`),
39+
moduleName: moduleNameIndex,
40+
}
3541
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { Config } from './code.js'
1+
import type { Config } from './generateCode.js'
2+
3+
export const moduleNameScalar = `Scalar`
24

35
export const generateScalar = (config: Config) => {
46
let code = ``
@@ -51,5 +53,8 @@ ${
5153
`
5254
}
5355

54-
return code
56+
return {
57+
code,
58+
moduleName: moduleNameScalar,
59+
}
5560
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
unwrapToNonNull,
2727
} from '../../lib/graphql.js'
2828
import { entries, values } from '../../lib/prelude.js'
29-
import type { Config } from './code.js'
29+
import type { Config } from './generateCode.js'
30+
31+
export const moduleNameSchemaBuildtime = `SchemaBuildtime`
3032

3133
const namespaceNames = {
3234
GraphQLEnumType: `Enum`,
@@ -316,7 +318,7 @@ export const generateSchemaBuildtime = (config: Config) => {
316318
if (name === `GraphQLScalarTypeCustom`) continue
317319
if (name === `GraphQLScalarTypeStandard`) continue
318320

319-
const namespaceName = name === `GraphQLRootTypes` ? `Root` : namespaceNames[name]
321+
const namespaceName = name === `GraphQLRootType` ? `Root` : namespaceNames[name]
320322
code += Code.commentSectionTitle(namespaceName)
321323
code += Code.export$(
322324
Code.namespace(
@@ -330,5 +332,8 @@ export const generateSchemaBuildtime = (config: Config) => {
330332
)
331333
}
332334

333-
return code
335+
return {
336+
code,
337+
moduleName: moduleNameSchemaBuildtime,
338+
}
334339
}

0 commit comments

Comments
 (0)