Skip to content

Commit

Permalink
feat: add externalUnionsAsInterfaces config (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Mar 14, 2024
1 parent b50dafb commit cb48d1d
Show file tree
Hide file tree
Showing 18 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const configSchema = object({
* @description If `MyType` depends on `MyDependentType1` and `MyDependentType2`, we can allow `MyDependentType2` to be imported externally by including its import in `extraImports` and omitting it in the `dependentTypesInScope` list: `["MyType", "MyDependentType1"]`
*/
dependentTypesInScope: optional(array(string())),
/**
* Denotes Kotlin classes representing union types to be treated as interfaces rather than annotation classes.
* This should be used for types outside `dependentTypesInScope` that are not generated by the plugin.
*/
externalUnionsAsInterfaces: optional(array(string())),
/**
* Additional imports to add to the generated file.
* @example ["com.example.additional.import.*"]
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/build-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export function buildAnnotations({
const directiveAnnotations = definitionNode
? buildDirectiveAnnotations(definitionNode, config, description)
: "";
const unionAnnotation = resolvedType?.annotation
? `@${resolvedType.annotation}\n`
const unionAnnotation = resolvedType?.unionAnnotation
? `@${resolvedType.unionAnnotation}\n`
: "";

const annotations = [
Expand Down
13 changes: 10 additions & 3 deletions src/helpers/build-type-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { CodegenConfig } from "../plugin";

export interface TypeMetadata {
typeName: string;
annotation?: string;
unionAnnotation?: string;
defaultValue: string;
isNullable: boolean;
}
Expand Down Expand Up @@ -59,10 +59,17 @@ export function buildTypeMetadata(
typeName: buildListType(typeNode, scalarTypeName),
};
} else if (isUnionType(schemaType)) {
const shouldTreatUnionAsInterface =
config.externalUnionsAsInterfaces?.includes(schemaType.name);
return {
...commonMetadata,
annotation: schemaType.name,
typeName: buildListType(typeNode, "Any"),
unionAnnotation: shouldTreatUnionAsInterface
? undefined
: schemaType.name,
typeName: buildListType(
typeNode,
shouldTreatUnionAsInterface ? schemaType.name : "Any",
),
};
} else {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export default {
extraImports: [
"com.some.import.TypeOutOfScope",
"com.some.import.UnionOutOfScope",
"com.some.import.ExternalUnionAsInterface",
],
onlyTypes: ["MyTypeInOnlyTypes"],
dependentTypesInScope: ["TypeInScope", "UnionInScope", "Type1"],
externalUnionsAsInterfaces: ["ExternalUnionAsInterface"],
} satisfies GraphQLKotlinCodegenConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kotlin.generated
import com.expediagroup.graphql.generator.annotations.*
import com.some.import.TypeOutOfScope
import com.some.import.UnionOutOfScope
import com.some.import.ExternalUnionAsInterface

data class MyTypeInOnlyTypes(
val field: TypeInScope,
Expand All @@ -14,7 +15,8 @@ data class TypeInScope(
@UnionInScope
val unionInScopeField: Any? = null,
@UnionOutOfScope
val unionOutOfScopeField: Any? = null
val unionOutOfScopeField: Any? = null,
val externalUnionAsInterfaceField: ExternalUnionAsInterface? = null
)

@GraphQLUnion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type TypeInScope {
field: String
unionInScopeField: UnionInScope
unionOutOfScopeField: UnionOutOfScope
externalUnionAsInterfaceField: ExternalUnionAsInterface
}

type TypeOutOfScope {
Expand All @@ -17,6 +18,8 @@ union UnionInScope = Type1

union UnionOutOfScope = Type2

union ExternalUnionAsInterface = Type1 | Type2

type Type1 {
field: String
}
Expand Down

0 comments on commit cb48d1d

Please sign in to comment.