Skip to content

Commit

Permalink
fix: annotate fields with out-of-scope unions (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Mar 14, 2024
1 parent 079e92c commit b50dafb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/helpers/build-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
import { buildDirectiveAnnotations } from "./build-directive-annotations";
import { CodegenConfig } from "../plugin";
import { TypeMetadata } from "./build-type-metadata";
import { dependentTypeIsInScope } from "./dependent-type-is-in-scope";

export type DefinitionNode =
| TypeDefinitionNode
Expand Down Expand Up @@ -56,11 +55,9 @@ export function buildAnnotations({
const directiveAnnotations = definitionNode
? buildDirectiveAnnotations(definitionNode, config, description)
: "";
const unionAnnotation =
resolvedType?.baseType &&
dependentTypeIsInScope(resolvedType.baseType, config)
? `@${resolvedType.baseType}\n`
: "";
const unionAnnotation = resolvedType?.annotation
? `@${resolvedType.annotation}\n`
: "";

const annotations = [
unionAnnotation,
Expand Down
13 changes: 4 additions & 9 deletions src/helpers/build-type-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import {
} from "graphql";
import { getBaseTypeNode } from "@graphql-codegen/visitor-plugin-common";
import { wrapTypeWithModifiers } from "@graphql-codegen/java-common";
import { dependentTypeIsInScope } from "./dependent-type-is-in-scope";
import { CodegenConfig } from "../plugin";

export interface TypeMetadata {
typeName: string;
baseType?: string;
annotation?: string;
defaultValue: string;
isNullable: boolean;
}
Expand All @@ -48,7 +47,6 @@ export function buildTypeMetadata(
defaultValue,
isNullable,
};
const defaultTypeName = schemaType.name;

if (isScalarType(schemaType)) {
const scalars = [...KOTLIN_SCALARS, ...(config.extraScalars ?? [])];
Expand All @@ -61,18 +59,15 @@ export function buildTypeMetadata(
typeName: buildListType(typeNode, scalarTypeName),
};
} else if (isUnionType(schemaType)) {
const unionTypeName = dependentTypeIsInScope(defaultTypeName, config)
? "Any"
: defaultTypeName;
return {
...commonMetadata,
baseType: defaultTypeName,
typeName: buildListType(typeNode, unionTypeName),
annotation: schemaType.name,
typeName: buildListType(typeNode, "Any"),
};
} else {
return {
...commonMetadata,
typeName: buildListType(typeNode, defaultTypeName),
typeName: buildListType(typeNode, schemaType.name),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ data class TypeInScope(
val field: String? = null,
@UnionInScope
val unionInScopeField: Any? = null,
val unionOutOfScopeField: UnionOutOfScope? = null
@UnionOutOfScope
val unionOutOfScopeField: Any? = null
)

@GraphQLUnion(
Expand Down

0 comments on commit b50dafb

Please sign in to comment.