Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian committed Apr 9, 2024
1 parent 4e4ef2f commit 9c4a72a
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/helpers/build-directive-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { CodegenConfig } from "../plugin";
import { CodegenConfig, GraphQLKotlinCodegenConfig } from "../plugin";
import { DefinitionNode, isDeprecatedDescription } from "./build-annotations";
import { getFederationDirectiveReplacement } from "./get-federation-directive-replacement";
import { TypeMetadata } from "./build-type-metadata";
import { ConstDirectiveNode } from "graphql/language";

export function buildDirectiveAnnotations(
incomingNode: DefinitionNode,
Expand Down Expand Up @@ -57,32 +58,43 @@ export function buildDirectiveAnnotations(
(!definitionType || definitionType === kind),
);
if (!directiveReplacementFromConfig) return "";
const kotlinAnnotations =
directiveReplacementFromConfig.kotlinAnnotations.map((item) => {
if (typeof item === "string") return item;
const directiveArguments = item.argumentsToRetain
?.map((argumentToRetain) => {
const argumentValueNode = directive.arguments?.find(
(argument) => argument.name.value === argumentToRetain,
)?.value;
if (!argumentValueNode)
throw new Error(
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directiveName}`,
);
if (!("value" in argumentValueNode))
throw new Error(
`Directive argument ${argumentToRetain} in directive ${directiveName} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
);
const argumentValue =
argumentValueNode.kind === "StringValue"
? `"${argumentValueNode.value}"`
: argumentValueNode.value;
return `${argumentToRetain}: ${argumentValue}`;
})
.join(", ");
return `@${item.annotationName}(${directiveArguments})`;
});
const kotlinAnnotations = buildFineGrainedKotlinAnnotations(
directive,
directiveReplacementFromConfig.kotlinAnnotations,
);
return kotlinAnnotations.join("\n") + "\n";
})
.join("");
}

function buildFineGrainedKotlinAnnotations(
directive: ConstDirectiveNode,
kotlinAnnotations: NonNullable<
GraphQLKotlinCodegenConfig["directiveReplacements"]
>[number]["kotlinAnnotations"],
) {
return kotlinAnnotations.map((kotlinAnnotation) => {
if (typeof kotlinAnnotation === "string") return kotlinAnnotation;
const directiveArguments = kotlinAnnotation.argumentsToRetain
?.map((argumentToRetain) => {
const argumentValueNode = directive.arguments?.find(
(argument) => argument.name.value === argumentToRetain,
)?.value;
if (!argumentValueNode)
throw new Error(
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directive.name.value}`,
);
if (!("value" in argumentValueNode))
throw new Error(
`Directive argument ${argumentToRetain} in directive ${directive.name.value} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
);
const argumentValue =
argumentValueNode.kind === "StringValue"
? `"${argumentValueNode.value}"`
: argumentValueNode.value;
return `${argumentToRetain}: ${argumentValue}`;
})
.join(", ");
return `@${kotlinAnnotation.annotationName}(${directiveArguments})`;
});
}

0 comments on commit 9c4a72a

Please sign in to comment.