Skip to content

Commit d39fe18

Browse files
committed
Fix unions
1 parent 0f01422 commit d39fe18

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

packages/schema/src/definition.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,8 @@ export class GObjectType<Source, Context> extends GraphQLObjectType<
122122
}
123123
}
124124

125-
export type GUnionTypeConfig<Source, Context> = Flatten<
126-
Omit<GraphQLUnionTypeConfig<Source, Context>, "types"> & {
127-
types: OrFunc<readonly GObjectType<Source, Context>[]>;
128-
}
129-
>;
130-
131125
export class GUnionType<Source, Context> extends GraphQLUnionType {
132-
constructor(config: Readonly<GUnionTypeConfig<Source, Context>>) {
126+
constructor(config: Readonly<GraphQLUnionTypeConfig<Source, Context>>) {
133127
super(config);
134128
}
135129
declare resolveType: Maybe<GraphQLTypeResolver<Source, Context>>;

packages/schema/src/output.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
GraphQLInterfaceType,
99
GraphQLObjectType,
1010
GraphQLUnionType,
11+
GraphQLUnionTypeConfig,
1112
} from "graphql/type/definition";
1213
import type { InferValueFromArgs } from "./api-without-context";
1314
import type {
@@ -28,7 +29,6 @@ import {
2829
GObjectType,
2930
GOutputType,
3031
GUnionType,
31-
GUnionTypeConfig,
3232
} from "./definition";
3333

3434
export type __toMakeTypeScriptEmitImportsForItemsOnlyUsedInJSDoc = [
@@ -293,9 +293,22 @@ export type GraphQLSchemaAPIWithContext<Context> = {
293293
* });
294294
* ```
295295
*/
296-
union: <Source>(
297-
config: GUnionTypeConfig<Source, Context>
298-
) => GUnionType<Source, Context>;
296+
union: <Type extends GObjectType<any, Context>>(
297+
config: Flatten<
298+
Omit<
299+
GraphQLUnionTypeConfig<
300+
Type extends GObjectType<infer Source, Context> ? Source : never,
301+
Context
302+
>,
303+
"types"
304+
> & {
305+
types: readonly Type[] | (() => readonly Type[]);
306+
}
307+
>
308+
) => GUnionType<
309+
Type extends GObjectType<infer Source, Context> ? Source : never,
310+
Context
311+
>;
299312
/**
300313
* Creates a GraphQL field.
301314
*
@@ -596,3 +609,7 @@ export function bindGraphQLSchemaAPIToContext<
596609
},
597610
};
598611
}
612+
613+
type Flatten<T> = {
614+
[Key in keyof T]: T[Key];
615+
} & {};

test-project/index.test-d.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
GInputType,
1313
GOutputType,
1414
GScalarType,
15+
GUnionType,
1516
} from "@graphql-ts/schema/definition";
1617

1718
// this isn't really right
@@ -1376,3 +1377,51 @@ const someInputFields = {
13761377
Invariant<typeof Something>
13771378
>();
13781379
}
1380+
1381+
{
1382+
const A = g.object<{
1383+
__typename: "A";
1384+
}>()({
1385+
name: "A",
1386+
fields: {
1387+
something: g.field({
1388+
type: g.nonNull(g.String),
1389+
resolve() {
1390+
return "A";
1391+
},
1392+
}),
1393+
},
1394+
});
1395+
const B = g.object<{
1396+
__typename: "B";
1397+
}>()({
1398+
name: "B",
1399+
fields: {
1400+
something: g.field({
1401+
type: g.nonNull(g.String),
1402+
resolve() {
1403+
return "B";
1404+
},
1405+
}),
1406+
},
1407+
});
1408+
1409+
const Something = g.union({
1410+
name: "Something",
1411+
types: [A, B],
1412+
});
1413+
assertCompatible<
1414+
Invariant<
1415+
GUnionType<
1416+
| {
1417+
__typename: "A";
1418+
}
1419+
| {
1420+
__typename: "B";
1421+
},
1422+
unknown
1423+
>
1424+
>,
1425+
Invariant<typeof Something>
1426+
>();
1427+
}

0 commit comments

Comments
 (0)