Skip to content

Commit

Permalink
feat(ts-client): select type utilities (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 19, 2024
1 parent 0521957 commit a35be0a
Show file tree
Hide file tree
Showing 26 changed files with 570 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/client/ResultSet/ResultSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export type Object$<$SelectionSet, $Node extends Schema.Output.Object$2, $Index
}>

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

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

// dprint-ignore
Expand Down
2 changes: 2 additions & 0 deletions src/entrypoints/alpha/schema.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { ResultSet } from '../../client/ResultSet/__.js'
export { SelectionSet } from '../../client/SelectionSet/__.js'
export * from '../../Schema/__.js'
233 changes: 225 additions & 8 deletions src/generator/__snapshots__/files.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,130 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generates types from GraphQL SDL file 1`] = `
"import type * as $ from '../../../../../src/Schema/__.js'
"import { ResultSet, SelectionSet } from '../../../../../src/entrypoints/alpha/schema.js'
import { Index } from './Index.js'
// Root Types
// ----------
export type Query<$SelectionSet extends SelectionSet.Root<Index, 'Query'>> = ResultSet.Root<
$SelectionSet,
Index,
'Query'
>
// Object Types
// ------------
export type DateObject1<$SelectionSet extends SelectionSet.Object<Index['objects']['DateObject1'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['DateObject1'], Index>
export type DateObject2<$SelectionSet extends SelectionSet.Object<Index['objects']['DateObject2'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['DateObject2'], Index>
export type ObjectUnion<$SelectionSet extends SelectionSet.Object<Index['objects']['ObjectUnion'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['ObjectUnion'], Index>
export type Foo<$SelectionSet extends SelectionSet.Object<Index['objects']['Foo'], Index>> = ResultSet.Object$<
$SelectionSet,
Index['objects']['Foo'],
Index
>
export type Bar<$SelectionSet extends SelectionSet.Object<Index['objects']['Bar'], Index>> = ResultSet.Object$<
$SelectionSet,
Index['objects']['Bar'],
Index
>
export type ObjectNested<$SelectionSet extends SelectionSet.Object<Index['objects']['ObjectNested'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['ObjectNested'], Index>
export type lowerCaseObject<$SelectionSet extends SelectionSet.Object<Index['objects']['lowerCaseObject'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['lowerCaseObject'], Index>
export type lowerCaseObject2<$SelectionSet extends SelectionSet.Object<Index['objects']['lowerCaseObject2'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['lowerCaseObject2'], Index>
export type Object1<$SelectionSet extends SelectionSet.Object<Index['objects']['Object1'], Index>> = ResultSet.Object$<
$SelectionSet,
Index['objects']['Object1'],
Index
>
export type Object1ImplementingInterface<
$SelectionSet extends SelectionSet.Object<Index['objects']['Object1ImplementingInterface'], Index>,
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object1ImplementingInterface'], Index>
export type Object2ImplementingInterface<
$SelectionSet extends SelectionSet.Object<Index['objects']['Object2ImplementingInterface'], Index>,
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object2ImplementingInterface'], Index>
// Union Types
// -----------
export type DateUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['DateUnion'], Index>> = ResultSet.Union<
$SelectionSet,
Index['unions']['DateUnion'],
Index
>
export type FooBarUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['FooBarUnion'], Index>> =
ResultSet.Union<$SelectionSet, Index['unions']['FooBarUnion'], Index>
export type lowerCaseUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['lowerCaseUnion'], Index>> =
ResultSet.Union<$SelectionSet, Index['unions']['lowerCaseUnion'], Index>
// Interface Types
// ---------------
export type DateInterface1<$SelectionSet extends SelectionSet.Interface<Index['interfaces']['DateInterface1'], Index>> =
ResultSet.Interface<$SelectionSet, Index['interfaces']['DateInterface1'], Index>
export type Interface<$SelectionSet extends SelectionSet.Interface<Index['interfaces']['Interface'], Index>> =
ResultSet.Interface<$SelectionSet, Index['interfaces']['Interface'], Index>
"
`;
exports[`generates types from GraphQL SDL file 2`] = `
"/* eslint-disable */
import type * as Schema from './SchemaBuildtime.js'
export interface Index {
Root: {
Query: Schema.Root.Query
Mutation: null
Subscription: null
}
objects: {
DateObject1: Schema.Object.DateObject1
DateObject2: Schema.Object.DateObject2
ObjectUnion: Schema.Object.ObjectUnion
Foo: Schema.Object.Foo
Bar: Schema.Object.Bar
ObjectNested: Schema.Object.ObjectNested
lowerCaseObject: Schema.Object.lowerCaseObject
lowerCaseObject2: Schema.Object.lowerCaseObject2
Object1: Schema.Object.Object1
Object1ImplementingInterface: Schema.Object.Object1ImplementingInterface
Object2ImplementingInterface: Schema.Object.Object2ImplementingInterface
}
unions: {
DateUnion: Schema.Union.DateUnion
FooBarUnion: Schema.Union.FooBarUnion
lowerCaseUnion: Schema.Union.lowerCaseUnion
}
interfaces: {
DateInterface1: Schema.Interface.DateInterface1
Interface: Schema.Interface.Interface
}
}
"
`;
exports[`generates types from GraphQL SDL file 3`] = `
"import type * as $ from '../../../../../src/entrypoints/alpha/schema.js'
import type * as $Scalar from './Scalar.ts'
// ------------------------------------------------------------ //
Expand Down Expand Up @@ -296,7 +419,7 @@ export namespace Union {
"
`;
exports[`generates types from GraphQL SDL file 2`] = `
exports[`generates types from GraphQL SDL file 4`] = `
"import type * as CustomScalar from '../../../../_/customScalarCodecs.js'
declare global {
Expand All @@ -310,10 +433,10 @@ export * from '../../../../_/customScalarCodecs.js'
"
`;
exports[`generates types from GraphQL SDL file 3`] = `
exports[`generates types from GraphQL SDL file 5`] = `
"/* eslint-disable */
import * as $ from '../../../../../src/Schema/__.js'
import * as $ from '../../../../../src/entrypoints/alpha/schema.js'
import * as $Scalar from './Scalar.js'
export const ABCEnum = $.Enum(\`ABCEnum\`, [\`A\`, \`B\`, \`C\`])
Expand Down Expand Up @@ -530,7 +653,101 @@ export const $Index = {
`;
exports[`schema2 1`] = `
"import type * as $ from '../../../../src/Schema/__.js'
"import { ResultSet, SelectionSet } from '../../../../src/entrypoints/alpha/schema.js'
import { Index } from './Index.js'
// Root Types
// ----------
export type Mutation<$SelectionSet extends SelectionSet.Root<Index, 'Mutation'>> = ResultSet.Root<
$SelectionSet,
Index,
'Mutation'
>
export type Query<$SelectionSet extends SelectionSet.Root<Index, 'Query'>> = ResultSet.Root<
$SelectionSet,
Index,
'Query'
>
// Object Types
// ------------
export type Bar<$SelectionSet extends SelectionSet.Object<Index['objects']['Bar'], Index>> = ResultSet.Object$<
$SelectionSet,
Index['objects']['Bar'],
Index
>
export type DateObject1<$SelectionSet extends SelectionSet.Object<Index['objects']['DateObject1'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['DateObject1'], Index>
export type Foo<$SelectionSet extends SelectionSet.Object<Index['objects']['Foo'], Index>> = ResultSet.Object$<
$SelectionSet,
Index['objects']['Foo'],
Index
>
export type Object1<$SelectionSet extends SelectionSet.Object<Index['objects']['Object1'], Index>> = ResultSet.Object$<
$SelectionSet,
Index['objects']['Object1'],
Index
>
export type Object1ImplementingInterface<
$SelectionSet extends SelectionSet.Object<Index['objects']['Object1ImplementingInterface'], Index>,
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object1ImplementingInterface'], Index>
export type Object2ImplementingInterface<
$SelectionSet extends SelectionSet.Object<Index['objects']['Object2ImplementingInterface'], Index>,
> = ResultSet.Object$<$SelectionSet, Index['objects']['Object2ImplementingInterface'], Index>
// Union Types
// -----------
export type FooBarUnion<$SelectionSet extends SelectionSet.Union<Index['unions']['FooBarUnion'], Index>> =
ResultSet.Union<$SelectionSet, Index['unions']['FooBarUnion'], Index>
// Interface Types
// ---------------
export type Interface<$SelectionSet extends SelectionSet.Interface<Index['interfaces']['Interface'], Index>> =
ResultSet.Interface<$SelectionSet, Index['interfaces']['Interface'], Index>
"
`;
exports[`schema2 2`] = `
"/* eslint-disable */
import type * as Schema from './SchemaBuildtime.js'
export interface Index {
Root: {
Query: Schema.Root.Query
Mutation: Schema.Root.Mutation
Subscription: null
}
objects: {
Bar: Schema.Object.Bar
DateObject1: Schema.Object.DateObject1
Foo: Schema.Object.Foo
Object1: Schema.Object.Object1
Object1ImplementingInterface: Schema.Object.Object1ImplementingInterface
Object2ImplementingInterface: Schema.Object.Object2ImplementingInterface
}
unions: {
FooBarUnion: Schema.Union.FooBarUnion
}
interfaces: {
Interface: Schema.Interface.Interface
}
}
"
`;
exports[`schema2 3`] = `
"import type * as $ from '../../../../src/entrypoints/alpha/schema.js'
import type * as $Scalar from './Scalar.ts'
// ------------------------------------------------------------ //
Expand Down Expand Up @@ -659,7 +876,7 @@ export namespace Union {
"
`;
exports[`schema2 2`] = `
exports[`schema2 4`] = `
"import type * as CustomScalar from '../../customScalarCodecs.js'
declare global {
Expand All @@ -673,10 +890,10 @@ export * from '../../customScalarCodecs.js'
"
`;
exports[`schema2 3`] = `
exports[`schema2 5`] = `
"/* eslint-disable */
import * as $ from '../../../../src/Schema/__.js'
import * as $ from '../../../../src/entrypoints/alpha/schema.js'
import * as $Scalar from './Scalar.js'
// @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.
Expand Down
12 changes: 9 additions & 3 deletions src/generator/code/index.ts → src/generator/code/Index2.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Code } from '../../lib/Code.js'
import { hasMutation, hasQuery, hasSubscription } from '../../lib/graphql.js'
import type { Config } from './code.js'
import type { Config } from './generateCode.js'
import { moduleNameSchemaBuildtime } from './SchemaBuildtime2.js'

export const moduleNameIndex = `Index`

export const generateIndex = (config: Config) => {
const namespace = `Schema`
const code = []
code.push(`/* eslint-disable */\n`)
code.push(`import type * as ${namespace} from './SchemaBuildtime.js'\n`)
code.push(`import type * as ${namespace} from './${moduleNameSchemaBuildtime}.js'\n`)
code.push(Code.export$(
Code.interface$(
`Index`,
Expand All @@ -31,5 +34,8 @@ export const generateIndex = (config: Config) => {
),
))

return code.join(`\n`)
return {
code: code.join(`\n`),
moduleName: moduleNameIndex,
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Config } from './code.js'
import type { Config } from './generateCode.js'

export const moduleNameScalar = `Scalar`

export const generateScalar = (config: Config) => {
let code = ``
Expand Down Expand Up @@ -51,5 +53,8 @@ ${
`
}

return code
return {
code,
moduleName: moduleNameScalar,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import {
unwrapToNonNull,
} from '../../lib/graphql.js'
import { entries, values } from '../../lib/prelude.js'
import type { Config } from './code.js'
import type { Config } from './generateCode.js'

export const moduleNameSchemaBuildtime = `SchemaBuildtime`

const namespaceNames = {
GraphQLEnumType: `Enum`,
Expand Down Expand Up @@ -316,7 +318,7 @@ export const generateSchemaBuildtime = (config: Config) => {
if (name === `GraphQLScalarTypeCustom`) continue
if (name === `GraphQLScalarTypeStandard`) continue

const namespaceName = name === `GraphQLRootTypes` ? `Root` : namespaceNames[name]
const namespaceName = name === `GraphQLRootType` ? `Root` : namespaceNames[name]
code += Code.commentSectionTitle(namespaceName)
code += Code.export$(
Code.namespace(
Expand All @@ -330,5 +332,8 @@ export const generateSchemaBuildtime = (config: Config) => {
)
}

return code
return {
code,
moduleName: moduleNameSchemaBuildtime,
}
}

0 comments on commit a35be0a

Please sign in to comment.