Skip to content

Commit 71b488d

Browse files
committed
pass unit test
1 parent 9520b94 commit 71b488d

File tree

11 files changed

+30
-21
lines changed

11 files changed

+30
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
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/config/should-exclude-type-definition.ts renamed to src/config/should-include-type-definition.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import { TypeDefinitionNode } from "graphql";
1514
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
1615

17-
export function shouldExcludeTypeDefinition(
18-
node: TypeDefinitionNode,
16+
export function shouldIncludeTypeDefinition(
17+
typeName: string,
1918
config: CodegenConfigWithDefaults,
2019
) {
21-
return config.onlyTypes && !config.onlyTypes.includes(node.name.value);
20+
return !config.onlyTypes || config.onlyTypes.includes(typeName);
2221
}

src/definitions/enum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ limitations under the License.
1414
import { EnumTypeDefinitionNode, EnumValueDefinitionNode } from "graphql";
1515
import { indentMultiline } from "@graphql-codegen/visitor-plugin-common";
1616
import { buildAnnotations } from "../annotations/build-annotations";
17-
import { shouldExcludeTypeDefinition } from "../config/should-exclude-type-definition";
17+
import { shouldIncludeTypeDefinition } from "../config/should-include-type-definition";
1818
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
1919
import { sanitizeName } from "../utils/sanitize-name";
2020
import { GraphQLSchema } from "graphql";
@@ -24,7 +24,7 @@ export function buildEnumTypeDefinition(
2424
schema: GraphQLSchema,
2525
config: CodegenConfigWithDefaults,
2626
) {
27-
if (shouldExcludeTypeDefinition(node, config)) {
27+
if (!shouldIncludeTypeDefinition(node.name.value, config)) {
2828
return "";
2929
}
3030

src/definitions/input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ limitations under the License.
1212
*/
1313

1414
import { GraphQLSchema, InputObjectTypeDefinitionNode } from "graphql";
15-
import { shouldExcludeTypeDefinition } from "../config/should-exclude-type-definition";
15+
import { shouldIncludeTypeDefinition } from "../config/should-include-type-definition";
1616
import { buildTypeMetadata } from "../utils/build-type-metadata";
1717
import { buildAnnotations } from "../annotations/build-annotations";
1818
import { indent } from "@graphql-codegen/visitor-plugin-common";
@@ -25,7 +25,7 @@ export function buildInputObjectDefinition(
2525
schema: GraphQLSchema,
2626
config: CodegenConfigWithDefaults,
2727
) {
28-
if (shouldExcludeTypeDefinition(node, config)) {
28+
if (!shouldIncludeTypeDefinition(node.name.value, config)) {
2929
return "";
3030
}
3131

src/definitions/interface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ limitations under the License.
1313

1414
import { GraphQLSchema, InterfaceTypeDefinitionNode } from "graphql";
1515
import { buildAnnotations } from "../annotations/build-annotations";
16-
import { shouldExcludeTypeDefinition } from "../config/should-exclude-type-definition";
16+
import { shouldIncludeTypeDefinition } from "../config/should-include-type-definition";
1717
import { buildInterfaceFieldDefinition } from "./field";
1818
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
1919
import { getDependentInterfaceNames } from "../utils/dependent-type-utils";
@@ -24,7 +24,7 @@ export function buildInterfaceDefinition(
2424
schema: GraphQLSchema,
2525
config: CodegenConfigWithDefaults,
2626
) {
27-
if (shouldExcludeTypeDefinition(node, config)) {
27+
if (!shouldIncludeTypeDefinition(node.name.value, config)) {
2828
return "";
2929
}
3030

@@ -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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
ObjectTypeDefinitionNode,
1919
} from "graphql";
2020
import { buildAnnotations } from "../annotations/build-annotations";
21-
import { shouldExcludeTypeDefinition } from "../config/should-exclude-type-definition";
21+
import { shouldIncludeTypeDefinition } from "../config/should-include-type-definition";
2222
import {
2323
getDependentInterfaceNames,
2424
getDependentUnionsForType,
@@ -37,7 +37,7 @@ export function buildObjectTypeDefinition(
3737
schema: GraphQLSchema,
3838
config: CodegenConfigWithDefaults,
3939
) {
40-
if (shouldExcludeTypeDefinition(node, config)) {
40+
if (!shouldIncludeTypeDefinition(node.name.value, config)) {
4141
return "";
4242
}
4343

@@ -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/definitions/union.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ limitations under the License.
1212
*/
1313

1414
import { GraphQLSchema, UnionTypeDefinitionNode } from "graphql";
15-
import { shouldExcludeTypeDefinition } from "../config/should-exclude-type-definition";
15+
import { shouldIncludeTypeDefinition } from "../config/should-include-type-definition";
1616
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
1717
import {
1818
buildAnnotations,
@@ -25,7 +25,7 @@ export function buildUnionTypeDefinition(
2525
schema: GraphQLSchema,
2626
config: CodegenConfigWithDefaults,
2727
) {
28-
if (shouldExcludeTypeDefinition(node, config)) {
28+
if (!shouldIncludeTypeDefinition(node.name.value, config)) {
2929
return "";
3030
}
3131
const annotations = buildAnnotations({

src/utils/dependent-type-utils.ts

Lines changed: 13 additions & 3 deletions
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+
shouldIncludeTypeDefinition(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,6 @@ 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) => shouldIncludeTypeDefinition(unionName, config));
6979
}

test/unit/should_honor_onlyTypes_when_implementing_external_interfaces/codegen.config.ts renamed to test/unit/should_honor_onlyTypes_when_implementing_external_interfaces_and_unions/codegen.config.ts

File renamed without changes.

test/unit/should_honor_onlyTypes_when_implementing_external_interfaces/expected.kt renamed to test/unit/should_honor_onlyTypes_when_implementing_external_interfaces_and_unions/expected.kt

File renamed without changes.

0 commit comments

Comments
 (0)