Skip to content

Commit 2abd79c

Browse files
authored
fix: honor includeDependentTypes false when using onlyTypes (#131)
1 parent bdeae1f commit 2abd79c

File tree

7 files changed

+83
-7
lines changed

7 files changed

+83
-7
lines changed

src/config/add-dependent-types-to-only-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function getDependentTypeNames(
4747
): string[] {
4848
const namedTypes = getDependentFieldTypeNames(node, config)
4949
.concat(getDependentUnionNames(node))
50-
.concat(getDependentInterfaceNames(node));
50+
.concat(getDependentInterfaceNames(node, config));
5151
const recursivelyFoundTypes = namedTypes
5252
.map((typeName) => schema.getType(typeName)?.astNode)
5353
.filter(Boolean)

src/definitions/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function buildInterfaceDefinition(
4545
definitionNode: node,
4646
});
4747

48-
const interfacesToInherit = getDependentInterfaceNames(node);
48+
const interfacesToInherit = getDependentInterfaceNames(node, config);
4949
const interfaceInheritance = `${interfacesToInherit.length ? ` : ${interfacesToInherit.join(", ")}` : ""}`;
5050

5151
return `${annotations}interface ${sanitizeName(node.name.value)}${interfaceInheritance} {

src/definitions/object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function buildObjectTypeDefinition(
4646
config,
4747
definitionNode: node,
4848
});
49-
const dependentInterfaces = getDependentInterfaceNames(node);
50-
const dependentUnions = getDependentUnionsForType(schema, node);
49+
const dependentInterfaces = getDependentInterfaceNames(node, config);
50+
const dependentUnions = getDependentUnionsForType(schema, node, config);
5151
const interfacesToInherit =
5252
config.unionGeneration === "MARKER_INTERFACE"
5353
? dependentInterfaces.concat(dependentUnions)

src/utils/dependent-type-utils.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from "graphql";
2222
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
2323
import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common";
24+
import { shouldIncludeTypeDefinition } from "../config/should-include-type-definition";
2425

2526
export function getDependentFieldTypeNames(
2627
node: TypeDefinitionNode,
@@ -41,9 +42,16 @@ function getFieldTypeName(fieldType: TypeNode) {
4142
return getBaseTypeNode(fieldType).name.value;
4243
}
4344

44-
export function getDependentInterfaceNames(node: TypeDefinitionNode) {
45+
export function getDependentInterfaceNames(
46+
node: TypeDefinitionNode,
47+
config: CodegenConfigWithDefaults,
48+
) {
4549
return "interfaces" in node
46-
? (node.interfaces?.map((interfaceNode) => interfaceNode.name.value) ?? [])
50+
? (node.interfaces
51+
?.map((interfaceNode) => interfaceNode.name.value)
52+
.filter((interfaceName) =>
53+
shouldIncludeDependentType(interfaceName, config),
54+
) ?? [])
4755
: [];
4856
}
4957

@@ -56,6 +64,7 @@ export function getDependentUnionNames(node: TypeDefinitionNode) {
5664
export function getDependentUnionsForType(
5765
schema: GraphQLSchema,
5866
node: TypeDefinitionNode,
67+
config: CodegenConfigWithDefaults,
5968
) {
6069
const typeMap = schema.getTypeMap();
6170
const unions = Object.values(typeMap).filter((type) =>
@@ -65,5 +74,16 @@ export function getDependentUnionsForType(
6574
.filter((union) =>
6675
union.getTypes().some((type) => type.name === node.name.value),
6776
)
68-
.map((union) => union.name);
77+
.map((union) => union.name)
78+
.filter((unionName) => shouldIncludeDependentType(unionName, config));
79+
}
80+
81+
function shouldIncludeDependentType(
82+
typeName: string,
83+
config: CodegenConfigWithDefaults,
84+
) {
85+
return (
86+
config.includeDependentTypes ||
87+
shouldIncludeTypeDefinition(typeName, config)
88+
);
6989
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { GraphQLKotlinCodegenConfig } from "../../../src/plugin";
2+
3+
export default {
4+
onlyTypes: [
5+
"MyIncludedType",
6+
"MyIncludedInterfaceInOnlyTypes",
7+
"MyIncludedType2",
8+
"MyIncludedUnion",
9+
],
10+
includeDependentTypes: false,
11+
} satisfies GraphQLKotlinCodegenConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.kotlin.generated
2+
3+
import com.expediagroup.graphql.generator.annotations.*
4+
5+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
6+
data class MyIncludedType(
7+
val field: String? = null,
8+
val field2: String
9+
)
10+
11+
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
12+
data class MyIncludedType2(
13+
val field: String? = null,
14+
val field2: String
15+
) : MyIncludedUnion
16+
17+
interface MyIncludedUnion
18+
19+
interface MyIncludedInterfaceInOnlyTypes {
20+
val field: String?
21+
val field2: String
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type MyIncludedType {
2+
field: String
3+
field2: String!
4+
}
5+
6+
union MyExcludedUnion = MyIncludedType
7+
8+
type MyIncludedType2 {
9+
field: String
10+
field2: String!
11+
}
12+
13+
union MyIncludedUnion = MyIncludedType2
14+
15+
interface MyIncludedInterfaceInOnlyTypes {
16+
field: String
17+
field2: String!
18+
}
19+
20+
type MyExcludedType implements MyIncludedInterfaceInOnlyTypes {
21+
field: String
22+
field2: String!
23+
}

0 commit comments

Comments
 (0)