Skip to content

Commit 4e4ef2f

Browse files
committed
rename directive and add errors
1 parent 0bbad9b commit 4e4ef2f

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ export const configSchema = object({
8282
union([
8383
string(),
8484
object({
85+
/**
86+
* The name of the annotation to replace the directive with.
87+
*/
8588
annotationName: string(),
86-
retainArguments: array(string()),
89+
/**
90+
* The arguments to retain from the directive. Can be INT, FLOAT, STRING, BOOLEAN, or ENUM.
91+
*/
92+
argumentsToRetain: array(string()),
8793
}),
8894
]),
8995
),

src/helpers/build-directive-annotations.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ export function buildDirectiveAnnotations(
6060
const kotlinAnnotations =
6161
directiveReplacementFromConfig.kotlinAnnotations.map((item) => {
6262
if (typeof item === "string") return item;
63-
const directiveArguments = item.retainArguments
63+
const directiveArguments = item.argumentsToRetain
6464
?.map((argumentToRetain) => {
6565
const argumentValueNode = directive.arguments?.find(
6666
(argument) => argument.name.value === argumentToRetain,
6767
)?.value;
68-
if (!argumentValueNode) throw new Error();
69-
if (!("value" in argumentValueNode)) throw new Error();
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+
);
7076
const argumentValue =
7177
argumentValueNode.kind === "StringValue"
7278
? `"${argumentValueNode.value}"`

test/unit/should_honor_directiveReplacements_config/codegen.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export default {
77
kotlinAnnotations: ["@SomeAnnotation1", "@SomeAnnotation2"],
88
},
99
{
10-
directive: "directive2",
10+
directive: "directiveWithArgs",
1111
kotlinAnnotations: [
1212
{
1313
annotationName: "SomeAnnotation3",
14-
retainArguments: ["arg1", "arg2"],
14+
argumentsToRetain: ["arg1", "arg2"],
1515
},
1616
],
1717
},

test/unit/should_honor_directiveReplacements_config/schema.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
directive @directive1 on OBJECT | UNION
2-
directive @directive2(arg1: String, arg2: Int) on OBJECT | UNION
2+
directive @directiveWithArgs(arg1: String, arg2: Int) on OBJECT | UNION
33

44
"A description for MyDirectiveType"
5-
type MyDirectiveType @directive1 @directive2(arg1: "arg1", arg2: 0) {
5+
type MyDirectiveType @directive1 @directiveWithArgs(arg1: "arg1", arg2: 0) {
66
field: String
77
}
88

0 commit comments

Comments
 (0)