Skip to content

Commit

Permalink
feat(ts-client): union non-null field (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Apr 13, 2024
1 parent 1287eda commit b85b50b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/Schema/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ export const field = <$Type extends Output.Any, $Args extends null | Args<any> =
}
}

// todo test non null union and non null interface fields
// todo test non null interface fields
export type SomeField = Field<
Hybrid.Enum | Hybrid.Scalar.Any | Output.List<any> | Output.Nullable<any> | Output.Object$2<any, any>,
| Hybrid.Enum
| Hybrid.Scalar.Any
| Output.List<any>
| Output.Nullable<any>
| Output.Object$2<string, any>
| Output.Union<string, [any, ...any[]]>,
Args<any> | null
>

Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Output/typeGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Nullable } from './types/Nullable.js'
import type { Object$2 } from './types/Object.js'
import type { Union } from './types/Union.js'

export type Named = Interface | Enum | Object$2 | Union | Hybrid.Scalar.Any
export type Named = Interface | Enum | Object$2 | Union<string, [Object$2, ...Object$2[]]> | Hybrid.Scalar.Any

export type Unnamed = List<any> | Nullable<any>

Expand Down
2 changes: 1 addition & 1 deletion src/SelectionSet/SelectionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export type Field_<
$type extends Schema.Scalar.Any ? Indicator<$Field> :
$type extends Schema.Enum ? Indicator<$Field> :
$type extends Schema.Object$2 ? Object<$type, $Index> & FieldDirectives & Arguments<$Field> :
$type extends Schema.Union ? Union<$type, $Index> :
$type extends Schema.Union ? Union<$type, $Index> :
$type extends Schema.Interface ? Interface<$type, $Index> :
TSError<'Field', '$Field case not handled', { $Field: $Field }>
// dprint-ignore
Expand Down
17 changes: 17 additions & 0 deletions src/generator/__snapshots__/files.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export namespace Root {
*/
abcEnum: $.Field<$.Output.Nullable<Enum.ABCEnum>, null>
lowerCaseUnion: $.Field<$.Output.Nullable<Union.lowerCaseUnion>, null>
unionFooBar: $.Field<$.Output.Nullable<Union.FooBarUnion>, null>
unionObject: $.Field<$.Output.Nullable<Object.ObjectUnion>, null>
unionFooBarNonNull: $.Field<Union.FooBarUnion, null>
unionObjectNonNull: $.Field<Object.ObjectUnion, null>
}>
}
Expand Down Expand Up @@ -188,6 +192,10 @@ export namespace Object {
date2: $.Field<$.Output.Nullable<$Scalar.Date>, null>
}>
export type ObjectUnion = $.Object$2<'ObjectUnion', {
fooBarUnion: $.Field<$.Output.Nullable<Union.FooBarUnion>, null>
}>
/**
* Object documentation.
*/
Expand Down Expand Up @@ -288,6 +296,10 @@ export const DateObject2 = $.Object$(\`DateObject2\`, {
date2: $.field($.Output.Nullable($Scalar.Date)),
})
export const ObjectUnion = $.Object$(\`ObjectUnion\`, {
fooBarUnion: $.field($.Output.Nullable(() => FooBarUnion)),
})
export const Foo = $.Object$(\`Foo\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})
Expand Down Expand Up @@ -412,6 +424,10 @@ export const Query = $.Object$(\`Query\`, {
fooBarUnion: $.field($.Output.Nullable(() => FooBarUnion)),
abcEnum: $.field($.Output.Nullable(ABCEnum)),
lowerCaseUnion: $.field($.Output.Nullable(() => lowerCaseUnion)),
unionFooBar: $.field($.Output.Nullable(() => FooBarUnion)),
unionObject: $.field($.Output.Nullable(() => ObjectUnion)),
unionFooBarNonNull: $.field(() => FooBarUnion),
unionObjectNonNull: $.field(() => ObjectUnion),
})
export const $Index = {
Expand All @@ -423,6 +439,7 @@ export const $Index = {
objects: {
DateObject1,
DateObject2,
ObjectUnion,
Foo,
Bar,
ObjectNested,
Expand Down
1 change: 1 addition & 0 deletions tests/ts/_/schema/generated/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Index {
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
Expand Down
8 changes: 8 additions & 0 deletions tests/ts/_/schema/generated/SchemaBuildtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export namespace Root {
*/
abcEnum: $.Field<$.Output.Nullable<Enum.ABCEnum>, null>
lowerCaseUnion: $.Field<$.Output.Nullable<Union.lowerCaseUnion>, null>
unionFooBar: $.Field<$.Output.Nullable<Union.FooBarUnion>, null>
unionObject: $.Field<$.Output.Nullable<Object.ObjectUnion>, null>
unionFooBarNonNull: $.Field<Union.FooBarUnion, null>
unionObjectNonNull: $.Field<Object.ObjectUnion, null>
}>
}

Expand Down Expand Up @@ -185,6 +189,10 @@ export namespace Object {
date2: $.Field<$.Output.Nullable<$Scalar.Date>, null>
}>

export type ObjectUnion = $.Object$2<'ObjectUnion', {
fooBarUnion: $.Field<$.Output.Nullable<Union.FooBarUnion>, null>
}>

/**
* Object documentation.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/ts/_/schema/generated/SchemaRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const DateObject2 = $.Object$(`DateObject2`, {
date2: $.field($.Output.Nullable($Scalar.Date)),
})

export const ObjectUnion = $.Object$(`ObjectUnion`, {
fooBarUnion: $.field($.Output.Nullable(() => FooBarUnion)),
})

export const Foo = $.Object$(`Foo`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})
Expand Down Expand Up @@ -142,6 +146,10 @@ export const Query = $.Object$(`Query`, {
fooBarUnion: $.field($.Output.Nullable(() => FooBarUnion)),
abcEnum: $.field($.Output.Nullable(ABCEnum)),
lowerCaseUnion: $.field($.Output.Nullable(() => lowerCaseUnion)),
unionFooBar: $.field($.Output.Nullable(() => FooBarUnion)),
unionObject: $.field($.Output.Nullable(() => ObjectUnion)),
unionFooBarNonNull: $.field(() => FooBarUnion),
unionObjectNonNull: $.field(() => ObjectUnion),
})

export const $Index = {
Expand All @@ -153,6 +161,7 @@ export const $Index = {
objects: {
DateObject1,
DateObject2,
ObjectUnion,
Foo,
Bar,
ObjectNested,
Expand Down
9 changes: 9 additions & 0 deletions tests/ts/_/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type Query {
"""
abcEnum: ABCEnum
lowerCaseUnion: lowerCaseUnion
unionFooBar: FooBarUnion
unionObject: ObjectUnion
unionFooBarNonNull: FooBarUnion!
unionObjectNonNull: ObjectUnion!

}

interface DateInterface1 {
Expand All @@ -62,6 +67,10 @@ input InputObject {
dateRequired: Date!
}

type ObjectUnion {
fooBarUnion: FooBarUnion
}

"""
Union documentation.
"""
Expand Down

0 comments on commit b85b50b

Please sign in to comment.