Skip to content

Commit 821b2cd

Browse files
committed
small refactors
1 parent 6fb41c8 commit 821b2cd

11 files changed

+55
-40
lines changed

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default [
1313
},
1414
},
1515
rules: {
16+
"no-console": "error",
1617
"@typescript-eslint/no-non-null-assertion": "error",
1718
"@typescript-eslint/no-unsafe-argument": "error",
1819
"@typescript-eslint/no-unsafe-call": "error",

src/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ export const configSchema = object({
4646
dependentTypesInScope: optional(array(string())),
4747
/**
4848
* Denotes Kotlin classes representing union types to be treated as interfaces rather than annotation classes.
49-
* This should be used for types outside `dependentTypesInScope` that are not generated by the plugin.
49+
* @description This should be used for types outside `dependentTypesInScope` that are not generated by the plugin. This also should only be used when unionGeneration is set to `ANNOTATION_CLASS`.
5050
*/
5151
externalUnionsAsInterfaces: optional(array(string())),
5252
/**
53-
* Additional imports to add to the generated file.
53+
* Additional imports to add to the generated file. GraphQL Kotlin annotations are always imported.
5454
* @example ["com.example.additional.import.*"]
5555
*/
5656
extraImports: optional(array(string())),
@@ -133,6 +133,7 @@ export const configSchema = object({
133133
),
134134
/**
135135
* Denotes the generation strategy for union types. Defaults to `MARKER_INTERFACE`.
136+
*
136137
* https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/writing-schemas/unions/
137138
*/
138139
unionGeneration: optional(

src/definitions/input.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export function buildInputObjectDefinition(
4747

4848
const annotations = buildAnnotations({
4949
config,
50-
inputDescription: node.description?.value,
5150
definitionNode: node,
5251
});
5352

src/definitions/union.ts

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

1414
import { UnionTypeDefinitionNode } from "graphql";
1515
import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition";
16-
import { buildDirectiveAnnotations } from "../helpers/build-directive-annotations";
1716
import { CodegenConfigWithDefaults } from "../config";
1817
import {
1918
buildAnnotations,
@@ -32,10 +31,9 @@ export function buildUnionTypeDefinition(
3231
definitionNode: node,
3332
});
3433
if (config.unionGeneration === "ANNOTATION_CLASS") {
35-
const directiveAnnotations = buildDirectiveAnnotations(node, config);
3634
const possibleTypes =
3735
node.types?.map((type) => `${type.name.value}::class`).join(", ") || "";
38-
return `${directiveAnnotations}@GraphQLUnion(
36+
return `${annotations}@GraphQLUnion(
3937
name = "${node.name.value}",
4038
possibleTypes = [${possibleTypes}],
4139
description = "${trimDescription(node.description?.value)}"

src/helpers/add-dependent-types.ts renamed to src/helpers/add-dependent-types-to-only-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import { getDependentTypeNames } from "./get-dependent-type-names";
1616
import { dependentTypeIsInScope } from "./dependent-type-is-in-scope";
1717
import { GraphQLSchema } from "graphql";
1818

19-
export function addDependentTypes(
19+
export function addDependentTypesToOnlyTypes(
2020
config: CodegenConfigWithDefaults,
2121
schema: GraphQLSchema,
2222
) {
2323
if (!config.onlyTypes) {
24-
throw new Error(`config.onlyTypes is required to add dependent types`);
24+
throw new Error(`onlyTypes config is required to add dependent types`);
2525
}
2626
const onlyTypesNodes = config.onlyTypes
2727
.map((typeName) => schema.getType(typeName)?.astNode)

src/helpers/build-annotations.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
EnumValueDefinitionNode,
1717
FieldDefinitionNode,
1818
InputValueDefinitionNode,
19+
Kind,
1920
TypeDefinitionNode,
2021
} from "graphql";
2122
import { buildDirectiveAnnotations } from "./build-directive-annotations";
@@ -30,17 +31,14 @@ export type DefinitionNode =
3031

3132
export function buildAnnotations({
3233
config,
33-
inputDescription,
3434
definitionNode,
3535
resolvedType,
3636
}: {
3737
config: CodegenConfigWithDefaults;
38-
inputDescription?: string;
3938
definitionNode?: DefinitionNode;
4039
resolvedType?: TypeMetadata;
4140
}) {
42-
const description =
43-
inputDescription ?? definitionNode?.description?.value ?? "";
41+
const description = definitionNode?.description?.value ?? "";
4442
const isDeprecated = isDeprecatedDescription(description, resolvedType);
4543
const descriptionAnnotator = isDeprecated
4644
? "@Deprecated"
@@ -49,15 +47,18 @@ export function buildAnnotations({
4947
? description.replace("DEPRECATED: ", "")
5048
: description;
5149
const trimmedDescription = trimDescription(descriptionValue);
52-
const descriptionAnnotation = description
53-
? `${descriptionAnnotator}("${trimmedDescription}")\n`
54-
: "";
50+
const descriptionAnnotation =
51+
!description ||
52+
(definitionNode?.kind === Kind.UNION_TYPE_DEFINITION &&
53+
config.unionGeneration === "ANNOTATION_CLASS")
54+
? ""
55+
: `${descriptionAnnotator}("${trimmedDescription}")\n`;
5556

5657
const directiveAnnotations = definitionNode
5758
? buildDirectiveAnnotations(
5859
definitionNode,
5960
config,
60-
description,
61+
isDeprecated,
6162
resolvedType,
6263
)
6364
: "";

src/helpers/build-config-with-defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function buildConfigWithDefaults(
1313
...config,
1414
extraImports: [
1515
"com.expediagroup.graphql.generator.annotations.*",
16-
...(config.extraImports ?? ([] satisfies string[])),
16+
...(config.extraImports ?? []),
1717
],
1818
} as const;
1919
}

src/helpers/build-directive-annotations.ts

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

1414
import { CodegenConfigWithDefaults } from "../config";
15-
import {
16-
DefinitionNode,
17-
isDeprecatedDescription,
18-
trimDescription,
19-
} from "./build-annotations";
15+
import { DefinitionNode, trimDescription } from "./build-annotations";
2016
import { getFederationDirectiveReplacement } from "./get-federation-directive-replacement";
2117
import { TypeMetadata } from "./build-type-metadata";
2218
import { ConstDirectiveNode } from "graphql/language";
2319

2420
export function buildDirectiveAnnotations(
25-
incomingNode: DefinitionNode,
21+
definitionNode: DefinitionNode,
2622
config: CodegenConfigWithDefaults,
27-
description?: string,
23+
isDeprecated?: boolean,
2824
resolvedType?: TypeMetadata,
2925
) {
30-
const kind = incomingNode.kind;
31-
const directives = incomingNode.directives ?? [];
26+
const kind = definitionNode.kind;
27+
const directives = definitionNode.directives ?? [];
3228

3329
return directives
3430
.map((directive) => {
3531
const directiveName = directive.name.value;
36-
if (
37-
directiveName === "deprecated" &&
38-
!isDeprecatedDescription(description)
39-
) {
32+
if (directiveName === "deprecated" && !isDeprecated) {
4033
const deprecatedReasonNode = directive.arguments?.find(
4134
(arg) => arg.name.value === "reason",
4235
)?.value;
4336
const deprecatedReason =
4437
deprecatedReasonNode?.kind === "StringValue"
4538
? deprecatedReasonNode.value
4639
: "";
47-
if (incomingNode.description?.value && resolvedType?.unionAnnotation) {
40+
if (
41+
definitionNode.description?.value &&
42+
resolvedType?.unionAnnotation
43+
) {
4844
return "";
4945
}
50-
const descriptionAnnotator = resolvedType?.unionAnnotation
51-
? "@GraphQLDescription"
52-
: "@Deprecated";
5346
const trimmedDeprecatedReason = trimDescription(deprecatedReason);
54-
return `${descriptionAnnotator}("${trimmedDeprecatedReason}")\n`;
47+
return `@Deprecated("${trimmedDeprecatedReason}")\n`;
5548
}
5649
const federationReplacement =
5750
getFederationDirectiveReplacement(directive);

src/plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from "@graphql-codegen/visitor-plugin-common";
2323
import { Input, safeParse } from "valibot";
2424
import { configSchema } from "./config";
25-
import { addDependentTypes } from "./helpers/add-dependent-types";
25+
import { addDependentTypesToOnlyTypes } from "./helpers/add-dependent-types-to-only-types";
2626
import { visit } from "graphql";
2727
import { buildConfigWithDefaults } from "./helpers/build-config-with-defaults";
2828

@@ -56,7 +56,7 @@ export const plugin: PluginFunction<GraphQLKotlinCodegenConfig> = (
5656
configWithDefaults.onlyTypes &&
5757
configWithDefaults.includeDependentTypes
5858
) {
59-
addDependentTypes(configWithDefaults, schema);
59+
addDependentTypesToOnlyTypes(configWithDefaults, schema);
6060
}
6161
const visitor = new KotlinVisitor(configWithDefaults, schema);
6262
const astNode = getCachedDocumentNodeFromSchema(schema);
@@ -67,7 +67,10 @@ export const plugin: PluginFunction<GraphQLKotlinCodegenConfig> = (
6767
.map((annotation) => `import ${annotation}`)
6868
.join("\n") + "\n";
6969
const typeDefinitions = definitions
70-
.filter((d: unknown) => typeof d === "string" && d.length)
70+
.filter(
71+
(definition: unknown) =>
72+
typeof definition === "string" && definition.length,
73+
)
7174
.join("\n\n");
7275

7376
return [packageName, imports, typeDefinitions].join("\n") + "\n";

test/unit/should_honor_union_generation_config/expected.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ data class UnionForHonoringUnionGenerationConfig(
2424
val field: Any? = null,
2525
@UnionAsAnnotation
2626
@GraphQLDescription("DEPRECATED: It uses the GraphQLDescription annotation for union annotations")
27-
val field2: Any? = null
27+
val deprecated1: Any? = null,
28+
@UnionAsAnnotation
29+
@GraphQLDescription("DEPRECATED: It uses the GraphQLDescription annotation for union types")
30+
val deprecated2: Any? = null,
31+
@UnionAsAnnotation
32+
@Deprecated("It uses the GraphQLDescription annotation for union types")
33+
val deprecated3: Any? = null,
34+
@UnionAsAnnotation
35+
@GraphQLDescription("When there is a description")
36+
val deprecated4: Any? = null
2837
)

test/unit/should_honor_union_generation_config/schema.graphql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ type UnionForHonoringUnionGenerationConfig {
1818
"A description for field"
1919
field: UnionAsAnnotation
2020
"DEPRECATED: It uses the GraphQLDescription annotation for union annotations"
21-
field2: UnionAsAnnotation
21+
deprecated1: UnionAsAnnotation
22+
@deprecated(reason: "when you have multiple deprecated annotations")
23+
"DEPRECATED: It uses the GraphQLDescription annotation for union types"
24+
deprecated2: UnionAsAnnotation
25+
deprecated3: UnionAsAnnotation
26+
@deprecated(
27+
reason: "It uses the GraphQLDescription annotation for union types"
28+
)
29+
"When there is a description"
30+
deprecated4: UnionAsAnnotation
31+
@deprecated(reason: "It uses the @Deprecated annotation for the reason")
2232
}

0 commit comments

Comments
 (0)