Skip to content

Commit

Permalink
fix: avoid using @deprecated on union type (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Mar 19, 2024
1 parent bde1389 commit dcede66
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 26 deletions.
23 changes: 18 additions & 5 deletions src/helpers/build-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ export function buildAnnotations({
}) {
const description =
inputDescription ?? definitionNode?.description?.value ?? "";
const descriptionAnnotator = isDeprecatedDescription(description)
const descriptionAnnotator = isDeprecatedDescription(
description,
resolvedType,
)
? "@Deprecated"
: "@GraphQLDescription";
const descriptionValue = isDeprecatedDescription(description)
const descriptionValue = isDeprecatedDescription(description, resolvedType)
? description.replace("DEPRECATED: ", "")
: description;
const trimmedDescription = trimDescription(descriptionValue);
Expand All @@ -53,7 +56,12 @@ export function buildAnnotations({
: "";

const directiveAnnotations = definitionNode
? buildDirectiveAnnotations(definitionNode, config, description)
? buildDirectiveAnnotations(
definitionNode,
config,
description,
resolvedType,
)
: "";
const unionAnnotation = resolvedType?.unionAnnotation
? `@${resolvedType.unionAnnotation}\n`
Expand All @@ -78,8 +86,13 @@ export function buildAnnotations({
);
}

export function isDeprecatedDescription(description?: string) {
return description?.startsWith("DEPRECATED: ");
export function isDeprecatedDescription(
description?: string,
resolvedType?: TypeMetadata,
) {
return (
description?.startsWith("DEPRECATED: ") && !resolvedType?.unionAnnotation
);
}

function trimDescription(description: string) {
Expand Down
15 changes: 11 additions & 4 deletions src/helpers/build-directive-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ limitations under the License.
import { CodegenConfig } from "../plugin";
import { DefinitionNode, isDeprecatedDescription } from "./build-annotations";
import { getFederationDirectiveReplacement } from "./get-federation-directive-replacement";
import { TypeMetadata } from "./build-type-metadata";

export function buildDirectiveAnnotations(
incomingNode: DefinitionNode,
config: CodegenConfig,
description?: string,
resolvedType?: TypeMetadata,
) {
const kind = incomingNode.kind;
const directives = incomingNode.directives ?? [];
Expand All @@ -30,12 +32,17 @@ export function buildDirectiveAnnotations(
directiveName === "deprecated" &&
!isDeprecatedDescription(description)
) {
const deprecatedReason = directive.arguments?.find(
const deprecatedReasonNode = directive.arguments?.find(
(arg) => arg.name.value === "reason",
)?.value;
return `@Deprecated("${
deprecatedReason?.kind === "StringValue" ? deprecatedReason.value : ""
}")\n`;
const deprecatedReason =
deprecatedReasonNode?.kind === "StringValue"
? deprecatedReasonNode.value
: "";
const descriptionAnnotator = resolvedType?.unionAnnotation
? "@GraphQLDescription"
: "@Deprecated";
return `${descriptionAnnotator}("${deprecatedReason}")\n`;
}
const federationReplacement =
getFederationDirectiveReplacement(directive);
Expand Down
23 changes: 22 additions & 1 deletion test/unit/should_annotate_types_properly/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,26 @@ data class MyType(
@GraphQLDescription("A description for email")
val email: String? = null,
@GraphQLDescription("A `weird` description for name")
val name: String? = null
val name: String? = null,
@Deprecated("Use something else instead")
val deprecated1: String? = null,
@Deprecated("")
val deprecated2: String? = null,
@Deprecated("Deprecated directive works too")
val deprecated3: String? = null,
@Deprecated("It only takes the first one")
val deprecated4: String? = null,
@MyUnion
@GraphQLDescription("DEPRECATED: It uses the GraphQLDescription annotation for union types")
val deprecated5: Any? = null,
@MyUnion
@GraphQLDescription("It uses the GraphQLDescription annotation for union types")
val deprecated6: Any? = null
)

@GraphQLUnion(
name = "MyUnion",
possibleTypes = [MyType::class],
description = ""
)
annotation class MyUnion
15 changes: 15 additions & 0 deletions test/unit/should_annotate_types_properly/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ type MyType {
A \`weird\` description for name
"""
name: String
"DEPRECATED: Use something else instead"
deprecated1: String
deprecated2: String @deprecated
deprecated3: String @deprecated(reason: "Deprecated directive works too")
"DEPRECATED: It only takes the first one"
deprecated4: String
@deprecated(reason: "when you have multiple deprecated annotations")
"DEPRECATED: It uses the GraphQLDescription annotation for union types"
deprecated5: MyUnion
deprecated6: MyUnion
@deprecated(
reason: "It uses the GraphQLDescription annotation for union types"
)
}

union MyUnion = MyType
10 changes: 1 addition & 9 deletions test/unit/should_generate_input_types_properly/expected.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@ data class MyInputType(
val username: String? = null,
@GraphQLDescription("A description for email")
val email: String? = null,
val name: String? = null,
@Deprecated("Use something else instead")
val deprecated1: String? = null,
@Deprecated("")
val deprecated2: String? = null,
@Deprecated("Deprecated directive works too")
val deprecated3: String? = null,
@Deprecated("It only takes the first one")
val deprecated4: String? = null
val name: String? = null
)
7 changes: 0 additions & 7 deletions test/unit/should_generate_input_types_properly/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,4 @@ input MyInputType {
"A description for email"
email: String
name: String
"DEPRECATED: Use something else instead"
deprecated1: String
deprecated2: String @deprecated
deprecated3: String @deprecated(reason: "Deprecated directive works too")
"DEPRECATED: It only takes the first one"
deprecated4: String
@deprecated(reason: "when you have multiple deprecated annotations")
}

0 comments on commit dcede66

Please sign in to comment.