Skip to content

Commit b50dafb

Browse files
authored
fix: annotate fields with out-of-scope unions (#12)
1 parent 079e92c commit b50dafb

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

src/helpers/build-annotations.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
import { buildDirectiveAnnotations } from "./build-directive-annotations";
2222
import { CodegenConfig } from "../plugin";
2323
import { TypeMetadata } from "./build-type-metadata";
24-
import { dependentTypeIsInScope } from "./dependent-type-is-in-scope";
2524

2625
export type DefinitionNode =
2726
| TypeDefinitionNode
@@ -56,11 +55,9 @@ export function buildAnnotations({
5655
const directiveAnnotations = definitionNode
5756
? buildDirectiveAnnotations(definitionNode, config, description)
5857
: "";
59-
const unionAnnotation =
60-
resolvedType?.baseType &&
61-
dependentTypeIsInScope(resolvedType.baseType, config)
62-
? `@${resolvedType.baseType}\n`
63-
: "";
58+
const unionAnnotation = resolvedType?.annotation
59+
? `@${resolvedType.annotation}\n`
60+
: "";
6461

6562
const annotations = [
6663
unionAnnotation,

src/helpers/build-type-metadata.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ import {
2121
} from "graphql";
2222
import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common";
2323
import { wrapTypeWithModifiers } from "@graphql-codegen/java-common";
24-
import { dependentTypeIsInScope } from "./dependent-type-is-in-scope";
2524
import { CodegenConfig } from "../plugin";
2625

2726
export interface TypeMetadata {
2827
typeName: string;
29-
baseType?: string;
28+
annotation?: string;
3029
defaultValue: string;
3130
isNullable: boolean;
3231
}
@@ -48,7 +47,6 @@ export function buildTypeMetadata(
4847
defaultValue,
4948
isNullable,
5049
};
51-
const defaultTypeName = schemaType.name;
5250

5351
if (isScalarType(schemaType)) {
5452
const scalars = [...KOTLIN_SCALARS, ...(config.extraScalars ?? [])];
@@ -61,18 +59,15 @@ export function buildTypeMetadata(
6159
typeName: buildListType(typeNode, scalarTypeName),
6260
};
6361
} else if (isUnionType(schemaType)) {
64-
const unionTypeName = dependentTypeIsInScope(defaultTypeName, config)
65-
? "Any"
66-
: defaultTypeName;
6762
return {
6863
...commonMetadata,
69-
baseType: defaultTypeName,
70-
typeName: buildListType(typeNode, unionTypeName),
64+
annotation: schemaType.name,
65+
typeName: buildListType(typeNode, "Any"),
7166
};
7267
} else {
7368
return {
7469
...commonMetadata,
75-
typeName: buildListType(typeNode, defaultTypeName),
70+
typeName: buildListType(typeNode, schemaType.name),
7671
};
7772
}
7873
}

test/unit/should_honor_dependentTypesInScope_config/expected.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ data class TypeInScope(
1313
val field: String? = null,
1414
@UnionInScope
1515
val unionInScopeField: Any? = null,
16-
val unionOutOfScopeField: UnionOutOfScope? = null
16+
@UnionOutOfScope
17+
val unionOutOfScopeField: Any? = null
1718
)
1819

1920
@GraphQLUnion(

0 commit comments

Comments
 (0)