Skip to content

Commit 24a4c29

Browse files
committed
use customDirectives config
1 parent f57b917 commit 24a4c29

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

src/annotations/build-directive-annotations.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,43 @@ export function buildDirectiveAnnotations(
6060
).join("\n") + "\n"
6161
);
6262
}
63-
return buildDefaultKotlinAnnotations(directive, config);
63+
if (config.customDirectives) {
64+
return buildCustomDirectives(directive);
65+
}
66+
return "";
6467
})
6568
.join("");
6669
}
6770

68-
function buildDefaultKotlinAnnotations(
71+
function buildCustomDirectives(directive: ConstDirectiveNode) {
72+
const directiveName = directive.name.value;
73+
return `@${titleCase(directiveName)}\n`;
74+
}
75+
76+
function buildDirectiveArguments(
6977
directive: ConstDirectiveNode,
70-
config: CodegenConfigWithDefaults,
78+
argumentsToRetain: string[],
7179
) {
72-
const directiveName = directive.name.value;
73-
if (
74-
config.directiveReplacements?.find(
75-
({ directive }) => directive === directiveName,
76-
)
77-
) {
78-
return "";
79-
}
80-
return directive.name.value === "deprecated"
81-
? ""
82-
: `@${titleCase(directiveName)}\n`;
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(", ");
83100
}
84101

85102
function buildKotlinAnnotationsFromConfig(
@@ -90,26 +107,10 @@ function buildKotlinAnnotationsFromConfig(
90107
) {
91108
return kotlinAnnotations.map((kotlinAnnotation) => {
92109
if (typeof kotlinAnnotation === "string") return kotlinAnnotation;
93-
const directiveArguments = kotlinAnnotation.argumentsToRetain
94-
?.map((argumentToRetain) => {
95-
const argumentValueNode = directive.arguments?.find(
96-
(argument) => argument.name.value === argumentToRetain,
97-
)?.value;
98-
if (!argumentValueNode)
99-
throw new Error(
100-
`Argument ${argumentToRetain} was provided in argumentsToRetain config but was not found in directive ${directive.name.value}`,
101-
);
102-
if (!("value" in argumentValueNode))
103-
throw new Error(
104-
`Directive argument ${argumentToRetain} in directive ${directive.name.value} has an unsupported type. Only INT, FLOAT, STRING, BOOLEAN, and ENUM are supported.`,
105-
);
106-
const argumentValue =
107-
argumentValueNode.kind === Kind.STRING
108-
? `"${argumentValueNode.value}"`
109-
: argumentValueNode.value;
110-
return `${argumentToRetain} = ${argumentValue}`;
111-
})
112-
.join(", ");
110+
const directiveArguments = buildDirectiveArguments(
111+
directive,
112+
kotlinAnnotation.argumentsToRetain,
113+
);
113114
return `@${kotlinAnnotation.annotationName}(${directiveArguments})`;
114115
});
115116
}

src/config/schema.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ export const configSchema = object({
3232
* @link https://opensource.expediagroup.com/graphql-kotlin-codegen/docs/class-consolidation
3333
*/
3434
classConsolidationEnabled: optional(boolean()),
35+
/**
36+
* Denotes directives to generate as @GraphQLDirective annotations.
37+
*
38+
* Directive arguments are not yet supported and will be ignored.
39+
*
40+
* @example ["myCustomDirective"]
41+
*
42+
* @link https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/customizing-schemas/directives/#custom-directives
43+
*/
44+
customDirectives: optional(array(string())),
3545
/**
3646
* Limits dependent types to include from `onlyTypes` list. Can be used to exclude classes that are imported from external packages.
3747
*

src/definitions/directive.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ limitations under the License.
1414
import { DirectiveDefinitionNode } from "graphql";
1515
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
1616
import { titleCase } from "../utils/title-case";
17-
import { FEDERATION_DIRECTIVES } from "../annotations/build-directive-annotations";
1817

1918
export function buildDirectiveDefinition(
2019
node: DirectiveDefinitionNode,
2120
config: CodegenConfigWithDefaults,
2221
): string {
23-
if (
24-
config.directiveReplacements?.find(
25-
({ directive }) => directive === node.name.value,
26-
) ||
27-
Object.values(FEDERATION_DIRECTIVES).includes(node.name.value)
28-
) {
22+
const directiveName = node.name.value;
23+
const isCustomDirective = config.customDirectives?.includes(directiveName);
24+
if (!isCustomDirective) {
2925
return "";
3026
}
3127
return `@GraphQLDirective(
32-
name = "${titleCase(node.name.value)}",
28+
name = "${titleCase(directiveName)}",
3329
description = "${node.description?.value ?? ""}",
3430
locations = [${node.locations.map((location) => `graphql.introspection.Introspection.DirectiveLocation.${location.value}`).join(", ")}]
3531
)
36-
annotation class MyCustomDirective`;
32+
annotation class ${titleCase(directiveName)}`;
3733
}

test/unit/should_generate_default_directives/codegen.config.ts renamed to test/unit/should_generate_custom_directives/codegen.config.ts

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

33
export default {
4+
customDirectives: ["myCustomDirective", "myCustomDirectiveWithArgs"],
45
extraImports: ["should_honor_directiveReplacements_config.*"],
56
directiveReplacements: [
67
{

0 commit comments

Comments
 (0)