Skip to content

Commit 9c4a72a

Browse files
committed
refactor
1 parent 4e4ef2f commit 9c4a72a

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/helpers/build-directive-annotations.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

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

1920
export function buildDirectiveAnnotations(
2021
incomingNode: DefinitionNode,
@@ -57,32 +58,43 @@ export function buildDirectiveAnnotations(
5758
(!definitionType || definitionType === kind),
5859
);
5960
if (!directiveReplacementFromConfig) return "";
60-
const kotlinAnnotations =
61-
directiveReplacementFromConfig.kotlinAnnotations.map((item) => {
62-
if (typeof item === "string") return item;
63-
const directiveArguments = item.argumentsToRetain
64-
?.map((argumentToRetain) => {
65-
const argumentValueNode = directive.arguments?.find(
66-
(argument) => argument.name.value === argumentToRetain,
67-
)?.value;
68-
if (!argumentValueNode)
69-
throw new Error(
70-
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directiveName}`,
71-
);
72-
if (!("value" in argumentValueNode))
73-
throw new Error(
74-
`Directive argument ${argumentToRetain} in directive ${directiveName} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
75-
);
76-
const argumentValue =
77-
argumentValueNode.kind === "StringValue"
78-
? `"${argumentValueNode.value}"`
79-
: argumentValueNode.value;
80-
return `${argumentToRetain}: ${argumentValue}`;
81-
})
82-
.join(", ");
83-
return `@${item.annotationName}(${directiveArguments})`;
84-
});
61+
const kotlinAnnotations = buildFineGrainedKotlinAnnotations(
62+
directive,
63+
directiveReplacementFromConfig.kotlinAnnotations,
64+
);
8565
return kotlinAnnotations.join("\n") + "\n";
8666
})
8767
.join("");
8868
}
69+
70+
function buildFineGrainedKotlinAnnotations(
71+
directive: ConstDirectiveNode,
72+
kotlinAnnotations: NonNullable<
73+
GraphQLKotlinCodegenConfig["directiveReplacements"]
74+
>[number]["kotlinAnnotations"],
75+
) {
76+
return kotlinAnnotations.map((kotlinAnnotation) => {
77+
if (typeof kotlinAnnotation === "string") return kotlinAnnotation;
78+
const directiveArguments = kotlinAnnotation.argumentsToRetain
79+
?.map((argumentToRetain) => {
80+
const argumentValueNode = directive.arguments?.find(
81+
(argument) => argument.name.value === argumentToRetain,
82+
)?.value;
83+
if (!argumentValueNode)
84+
throw new Error(
85+
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directive.name.value}`,
86+
);
87+
if (!("value" in argumentValueNode))
88+
throw new Error(
89+
`Directive argument ${argumentToRetain} in directive ${directive.name.value} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
90+
);
91+
const argumentValue =
92+
argumentValueNode.kind === "StringValue"
93+
? `"${argumentValueNode.value}"`
94+
: argumentValueNode.value;
95+
return `${argumentToRetain}: ${argumentValue}`;
96+
})
97+
.join(", ");
98+
return `@${kotlinAnnotation.annotationName}(${directiveArguments})`;
99+
});
100+
}

0 commit comments

Comments
 (0)