Skip to content

Commit acddaed

Browse files
committed
refactor and update test
1 parent adecc23 commit acddaed

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

src/annotations/build-directive-annotations.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,22 @@ export function buildDirectiveAnnotations(
6060
).join("\n") + "\n"
6161
);
6262
}
63-
if (config.customDirectives) {
64-
return buildCustomDirectives(directive);
63+
const customDirectiveFromConfig = config.customDirectives?.find(
64+
(directive) => directive === directiveName,
65+
);
66+
if (customDirectiveFromConfig) {
67+
return buildCustomDirective(directive);
6568
}
6669
return "";
6770
})
6871
.join("");
6972
}
7073

71-
function buildCustomDirectives(directive: ConstDirectiveNode) {
74+
function buildCustomDirective(directive: ConstDirectiveNode) {
7275
const directiveName = directive.name.value;
7376
return `@${titleCase(directiveName)}\n`;
7477
}
7578

76-
function buildDirectiveArguments(
77-
directive: ConstDirectiveNode,
78-
argumentsToRetain: string[],
79-
) {
80-
return argumentsToRetain
81-
.map((argumentToRetain) => {
82-
const argumentValueNode = directive.arguments?.find(
83-
(argument) => argument.name.value === argumentToRetain,
84-
)?.value;
85-
if (!argumentValueNode)
86-
throw new Error(
87-
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directive.name.value}`,
88-
);
89-
if (!("value" in argumentValueNode))
90-
throw new Error(
91-
`Directive argument ${argumentToRetain} in directive ${directive.name.value} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
92-
);
93-
const argumentValue =
94-
argumentValueNode.kind === Kind.STRING
95-
? `"${argumentValueNode.value}"`
96-
: argumentValueNode.value;
97-
return `${argumentToRetain} = ${argumentValue}`;
98-
})
99-
.join(", ");
100-
}
101-
10279
function buildKotlinAnnotationsFromConfig(
10380
directive: ConstDirectiveNode,
10481
kotlinAnnotations: NonNullable<
@@ -107,10 +84,26 @@ function buildKotlinAnnotationsFromConfig(
10784
) {
10885
return kotlinAnnotations.map((kotlinAnnotation) => {
10986
if (typeof kotlinAnnotation === "string") return kotlinAnnotation;
110-
const directiveArguments = buildDirectiveArguments(
111-
directive,
112-
kotlinAnnotation.argumentsToRetain,
113-
);
87+
const directiveArguments = kotlinAnnotation.argumentsToRetain
88+
?.map((argumentToRetain) => {
89+
const argumentValueNode = directive.arguments?.find(
90+
(argument) => argument.name.value === argumentToRetain,
91+
)?.value;
92+
if (!argumentValueNode)
93+
throw new Error(
94+
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directive.name.value}`,
95+
);
96+
if (!("value" in argumentValueNode))
97+
throw new Error(
98+
`Directive argument ${argumentToRetain} in directive ${directive.name.value} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
99+
);
100+
const argumentValue =
101+
argumentValueNode.kind === Kind.STRING
102+
? `"${argumentValueNode.value}"`
103+
: argumentValueNode.value;
104+
return `${argumentToRetain} = ${argumentValue}`;
105+
})
106+
.join(", ");
114107
return `@${kotlinAnnotation.annotationName}(${directiveArguments})`;
115108
});
116109
}

test/unit/should_generate_custom_directives/codegen.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GraphQLKotlinCodegenConfig } from "../../../src/plugin";
22

33
export default {
4-
customDirectives: ["myCustomDirective", "myCustomDirectiveWithArgs"],
4+
customDirectives: ["myCustomDirective", "myCustomDirective2"],
55
extraImports: ["should_honor_directiveReplacements_config.*"],
66
directiveReplacements: [
77
{

test/unit/should_generate_custom_directives/expected.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import should_honor_directiveReplacements_config.*
1010
)
1111
annotation class MyCustomDirective
1212

13+
@GraphQLDirective(
14+
name = "MyCustomDirective2",
15+
description = "",
16+
locations = [graphql.introspection.Introspection.DirectiveLocation.OBJECT, graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION, graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT, graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION]
17+
)
18+
annotation class MyCustomDirective2
19+
1320
@MyCustomDirective
21+
@MyCustomDirective2
1422
@SomeAnnotation1
1523
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT])
1624
data class MyTypeWithCustomDirectiveOnObject(

test/unit/should_generate_custom_directives/schema.graphql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"A description for MyCustomDirective"
22
directive @myCustomDirective on OBJECT | FIELD_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION
3+
directive @myCustomDirective2 on OBJECT | FIELD_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION
34

45
directive @someDirective1 on OBJECT
56

6-
type MyTypeWithCustomDirectiveOnObject @myCustomDirective @someDirective1 {
7+
type MyTypeWithCustomDirectiveOnObject
8+
@myCustomDirective
9+
@myCustomDirective2
10+
@someDirective1 {
711
field: String
812
}
913

0 commit comments

Comments
 (0)