Skip to content

Commit c51350c

Browse files
committed
pass test but broke others
1 parent 32d0f6c commit c51350c

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

src/annotations/build-directive-annotations.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ConstDirectiveNode } from "graphql/language";
1717
import { GraphQLSchema, isInputObjectType, Kind } from "graphql";
1818
import { shouldConsolidateTypes } from "../utils/should-consolidate-types";
1919
import { sanitizeName } from "../utils/sanitize-name";
20+
import { titleCase } from "../definitions/object";
2021

2122
export function buildDirectiveAnnotations(
2223
definitionNode: DefinitionNode,
@@ -50,17 +51,22 @@ export function buildDirectiveAnnotations(
5051
return !typeWillBeConsolidated;
5152
},
5253
);
53-
if (!directiveReplacementFromConfig) return "";
54-
const kotlinAnnotations = buildKotlinAnnotations(
54+
if (!directiveReplacementFromConfig)
55+
return buildDefaultKotlinAnnotations(directive);
56+
const kotlinAnnotationsFromConfig = buildKotlinAnnotationsFromConfig(
5557
directive,
5658
directiveReplacementFromConfig.kotlinAnnotations,
5759
);
58-
return kotlinAnnotations.join("\n") + "\n";
60+
return kotlinAnnotationsFromConfig.join("\n") + "\n";
5961
})
6062
.join("");
6163
}
6264

63-
function buildKotlinAnnotations(
65+
function buildDefaultKotlinAnnotations(directive: ConstDirectiveNode) {
66+
return `@${titleCase(directive.name.value)}\n`;
67+
}
68+
69+
function buildKotlinAnnotationsFromConfig(
6470
directive: ConstDirectiveNode,
6571
kotlinAnnotations: NonNullable<
6672
CodegenConfigWithDefaults["directiveReplacements"]

src/definitions/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,6 @@ export function shouldGenerateFunctionsInClass(
162162
);
163163
}
164164

165-
function titleCase(str: string) {
165+
export function titleCase(str: string) {
166166
return str.charAt(0).toUpperCase() + str.slice(1);
167167
}

src/visitor.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ limitations under the License.
1313

1414
import { BaseVisitor, RawConfig } from "@graphql-codegen/visitor-plugin-common";
1515
import {
16+
DirectiveDefinitionNode,
1617
EnumTypeDefinitionNode,
1718
GraphQLSchema,
1819
InputObjectTypeDefinitionNode,
@@ -24,7 +25,7 @@ import { CodegenConfigWithDefaults } from "./config/build-config-with-defaults";
2425
import { buildEnumTypeDefinition } from "./definitions/enum";
2526
import { buildInterfaceDefinition } from "./definitions/interface";
2627
import { buildInputObjectDefinition } from "./definitions/input";
27-
import { buildObjectTypeDefinition } from "./definitions/object";
28+
import { buildObjectTypeDefinition, titleCase } from "./definitions/object";
2829
import { buildUnionTypeDefinition } from "./definitions/union";
2930
import { ParsedConfig } from "@graphql-codegen/visitor-plugin-common/typings/base-visitor";
3031

@@ -39,6 +40,10 @@ export class KotlinVisitor extends BaseVisitor<
3940
super(rawConfig, rawConfig);
4041
}
4142

43+
DirectiveDefinition(node: DirectiveDefinitionNode): string {
44+
return buildDirectiveDefinition(node);
45+
}
46+
4247
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
4348
return buildEnumTypeDefinition(node, this._schema, this.config);
4449
}
@@ -59,3 +64,12 @@ export class KotlinVisitor extends BaseVisitor<
5964
return buildUnionTypeDefinition(node, this._schema, this.config);
6065
}
6166
}
67+
68+
function buildDirectiveDefinition(node: DirectiveDefinitionNode): string {
69+
return `@GraphQLDirective(
70+
name = "${titleCase(node.name.value)}",
71+
description = "${node.description?.value ?? ""}",
72+
locations = [${node.locations.map((location) => `graphql.introspection.Introspection.DirectiveLocation.${location.value}`).join(", ")}]
73+
)
74+
annotation class MyCustomDirective`;
75+
}

test/unit/should_generate_default_directives/expected.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.expediagroup.graphql.generator.annotations.*
55
@GraphQLDirective(
66
name = "MyCustomDirective",
77
description = "A description for MyCustomDirective",
8-
locations = [graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION]
8+
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]
99
)
1010
annotation class MyCustomDirective
1111

test/unit/should_generate_default_directives/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"A description for MyCustomDirective"
12
directive @myCustomDirective on OBJECT | FIELD_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION
23

34
type MyTypeWithCustomDirectiveOnObject @myCustomDirective {

0 commit comments

Comments
 (0)