Skip to content

Commit dcede66

Browse files
authored
fix: avoid using @deprecated on union type (#15)
1 parent bde1389 commit dcede66

File tree

6 files changed

+67
-26
lines changed

6 files changed

+67
-26
lines changed

src/helpers/build-annotations.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ export function buildAnnotations({
4141
}) {
4242
const description =
4343
inputDescription ?? definitionNode?.description?.value ?? "";
44-
const descriptionAnnotator = isDeprecatedDescription(description)
44+
const descriptionAnnotator = isDeprecatedDescription(
45+
description,
46+
resolvedType,
47+
)
4548
? "@Deprecated"
4649
: "@GraphQLDescription";
47-
const descriptionValue = isDeprecatedDescription(description)
50+
const descriptionValue = isDeprecatedDescription(description, resolvedType)
4851
? description.replace("DEPRECATED: ", "")
4952
: description;
5053
const trimmedDescription = trimDescription(descriptionValue);
@@ -53,7 +56,12 @@ export function buildAnnotations({
5356
: "";
5457

5558
const directiveAnnotations = definitionNode
56-
? buildDirectiveAnnotations(definitionNode, config, description)
59+
? buildDirectiveAnnotations(
60+
definitionNode,
61+
config,
62+
description,
63+
resolvedType,
64+
)
5765
: "";
5866
const unionAnnotation = resolvedType?.unionAnnotation
5967
? `@${resolvedType.unionAnnotation}\n`
@@ -78,8 +86,13 @@ export function buildAnnotations({
7886
);
7987
}
8088

81-
export function isDeprecatedDescription(description?: string) {
82-
return description?.startsWith("DEPRECATED: ");
89+
export function isDeprecatedDescription(
90+
description?: string,
91+
resolvedType?: TypeMetadata,
92+
) {
93+
return (
94+
description?.startsWith("DEPRECATED: ") && !resolvedType?.unionAnnotation
95+
);
8396
}
8497

8598
function trimDescription(description: string) {

src/helpers/build-directive-annotations.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ limitations under the License.
1414
import { CodegenConfig } from "../plugin";
1515
import { DefinitionNode, isDeprecatedDescription } from "./build-annotations";
1616
import { getFederationDirectiveReplacement } from "./get-federation-directive-replacement";
17+
import { TypeMetadata } from "./build-type-metadata";
1718

1819
export function buildDirectiveAnnotations(
1920
incomingNode: DefinitionNode,
2021
config: CodegenConfig,
2122
description?: string,
23+
resolvedType?: TypeMetadata,
2224
) {
2325
const kind = incomingNode.kind;
2426
const directives = incomingNode.directives ?? [];
@@ -30,12 +32,17 @@ export function buildDirectiveAnnotations(
3032
directiveName === "deprecated" &&
3133
!isDeprecatedDescription(description)
3234
) {
33-
const deprecatedReason = directive.arguments?.find(
35+
const deprecatedReasonNode = directive.arguments?.find(
3436
(arg) => arg.name.value === "reason",
3537
)?.value;
36-
return `@Deprecated("${
37-
deprecatedReason?.kind === "StringValue" ? deprecatedReason.value : ""
38-
}")\n`;
38+
const deprecatedReason =
39+
deprecatedReasonNode?.kind === "StringValue"
40+
? deprecatedReasonNode.value
41+
: "";
42+
const descriptionAnnotator = resolvedType?.unionAnnotation
43+
? "@GraphQLDescription"
44+
: "@Deprecated";
45+
return `${descriptionAnnotator}("${deprecatedReason}")\n`;
3946
}
4047
const federationReplacement =
4148
getFederationDirectiveReplacement(directive);

test/unit/should_annotate_types_properly/expected.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,26 @@ data class MyType(
88
@GraphQLDescription("A description for email")
99
val email: String? = null,
1010
@GraphQLDescription("A `weird` description for name")
11-
val name: String? = null
11+
val name: String? = null,
12+
@Deprecated("Use something else instead")
13+
val deprecated1: String? = null,
14+
@Deprecated("")
15+
val deprecated2: String? = null,
16+
@Deprecated("Deprecated directive works too")
17+
val deprecated3: String? = null,
18+
@Deprecated("It only takes the first one")
19+
val deprecated4: String? = null,
20+
@MyUnion
21+
@GraphQLDescription("DEPRECATED: It uses the GraphQLDescription annotation for union types")
22+
val deprecated5: Any? = null,
23+
@MyUnion
24+
@GraphQLDescription("It uses the GraphQLDescription annotation for union types")
25+
val deprecated6: Any? = null
1226
)
27+
28+
@GraphQLUnion(
29+
name = "MyUnion",
30+
possibleTypes = [MyType::class],
31+
description = ""
32+
)
33+
annotation class MyUnion

test/unit/should_annotate_types_properly/schema.graphql

+15
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,19 @@ type MyType {
1111
A \`weird\` description for name
1212
"""
1313
name: String
14+
"DEPRECATED: Use something else instead"
15+
deprecated1: String
16+
deprecated2: String @deprecated
17+
deprecated3: String @deprecated(reason: "Deprecated directive works too")
18+
"DEPRECATED: It only takes the first one"
19+
deprecated4: String
20+
@deprecated(reason: "when you have multiple deprecated annotations")
21+
"DEPRECATED: It uses the GraphQLDescription annotation for union types"
22+
deprecated5: MyUnion
23+
deprecated6: MyUnion
24+
@deprecated(
25+
reason: "It uses the GraphQLDescription annotation for union types"
26+
)
1427
}
28+
29+
union MyUnion = MyType

test/unit/should_generate_input_types_properly/expected.kt

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,5 @@ data class MyInputType(
77
val username: String? = null,
88
@GraphQLDescription("A description for email")
99
val email: String? = null,
10-
val name: String? = null,
11-
@Deprecated("Use something else instead")
12-
val deprecated1: String? = null,
13-
@Deprecated("")
14-
val deprecated2: String? = null,
15-
@Deprecated("Deprecated directive works too")
16-
val deprecated3: String? = null,
17-
@Deprecated("It only takes the first one")
18-
val deprecated4: String? = null
10+
val name: String? = null
1911
)

test/unit/should_generate_input_types_properly/schema.graphql

-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,4 @@ input MyInputType {
44
"A description for email"
55
email: String
66
name: String
7-
"DEPRECATED: Use something else instead"
8-
deprecated1: String
9-
deprecated2: String @deprecated
10-
deprecated3: String @deprecated(reason: "Deprecated directive works too")
11-
"DEPRECATED: It only takes the first one"
12-
deprecated4: String
13-
@deprecated(reason: "when you have multiple deprecated annotations")
147
}

0 commit comments

Comments
 (0)