Skip to content

Commit 8b5a1c7

Browse files
committed
small refactors
1 parent 6fb41c8 commit 8b5a1c7

16 files changed

+115
-116
lines changed

bun.lockb

1.29 KB
Binary file not shown.

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default [
1313
},
1414
},
1515
rules: {
16+
"no-console": "error",
1617
"@typescript-eslint/no-non-null-assertion": "error",
1718
"@typescript-eslint/no-unsafe-argument": "error",
1819
"@typescript-eslint/no-unsafe-call": "error",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@graphql-codegen/cli": "5.0.2",
2929
"@total-typescript/ts-reset": "0.5.1",
3030
"bun-types": "1.1.4",
31-
"eslint": "9.1.0",
31+
"eslint": "9.1.1",
3232
"husky": "9.0.11",
3333
"prettier": "3.2.5",
3434
"tsup": "8.0.2",
@@ -39,7 +39,7 @@
3939
"build": "tsup src/plugin.ts --clean --dts --external graphql",
4040
"format": "prettier --write .",
4141
"format-check": "prettier --check .",
42-
"integration": "bun run build && graphql-codegen && ./gradlew compileTestKotlin",
42+
"integration": "bun run build && graphql-codegen && ./gradlew build",
4343
"lint": "eslint .",
4444
"prepack": "bun run build",
4545
"prepare": "husky",

src/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ export const configSchema = object({
4646
dependentTypesInScope: optional(array(string())),
4747
/**
4848
* Denotes Kotlin classes representing union types to be treated as interfaces rather than annotation classes.
49-
* This should be used for types outside `dependentTypesInScope` that are not generated by the plugin.
49+
* @description This should be used for types outside `dependentTypesInScope` that are not generated by the plugin. This also should only be used when unionGeneration is set to `ANNOTATION_CLASS`.
5050
*/
5151
externalUnionsAsInterfaces: optional(array(string())),
5252
/**
53-
* Additional imports to add to the generated file.
53+
* Additional imports to add to the generated file. GraphQL Kotlin annotations are always imported.
5454
* @example ["com.example.additional.import.*"]
5555
*/
5656
extraImports: optional(array(string())),
@@ -133,6 +133,7 @@ export const configSchema = object({
133133
),
134134
/**
135135
* Denotes the generation strategy for union types. Defaults to `MARKER_INTERFACE`.
136+
*
136137
* https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/writing-schemas/unions/
137138
*/
138139
unionGeneration: optional(

src/definitions/input.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export function buildInputObjectDefinition(
4747

4848
const annotations = buildAnnotations({
4949
config,
50-
inputDescription: node.description?.value,
5150
definitionNode: node,
5251
});
5352

src/definitions/object.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ export function buildObjectTypeDefinition(
4949

5050
if (isResolverType(node, config)) {
5151
return `${annotations}@GraphQLIgnore\ninterface ${name}${interfaceInheritance} {
52-
${getClassMembers({ node, schema, config })}
52+
${getDataClassMembers({ node, schema, config })}
5353
}
5454
5555
${annotations}@GraphQLIgnore\ninterface ${name}CompletableFuture {
56-
${getClassMembers({ node, schema, config, completableFuture: true })}
56+
${getDataClassMembers({ node, schema, config, completableFuture: true })}
5757
}`;
5858
}
5959

6060
return `${annotations}data class ${name}(
61-
${getClassMembers({ node, schema, config })}
61+
${getDataClassMembers({ node, schema, config })}
6262
)${interfaceInheritance}`;
6363
}
6464

65-
function getClassMembers({
65+
function getDataClassMembers({
6666
node,
6767
schema,
6868
config,

src/definitions/union.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ limitations under the License.
1313

1414
import { UnionTypeDefinitionNode } from "graphql";
1515
import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition";
16-
import { buildDirectiveAnnotations } from "../helpers/build-directive-annotations";
1716
import { CodegenConfigWithDefaults } from "../config";
1817
import {
1918
buildAnnotations,
@@ -32,10 +31,9 @@ export function buildUnionTypeDefinition(
3231
definitionNode: node,
3332
});
3433
if (config.unionGeneration === "ANNOTATION_CLASS") {
35-
const directiveAnnotations = buildDirectiveAnnotations(node, config);
3634
const possibleTypes =
3735
node.types?.map((type) => `${type.name.value}::class`).join(", ") || "";
38-
return `${directiveAnnotations}@GraphQLUnion(
36+
return `${annotations}@GraphQLUnion(
3937
name = "${node.name.value}",
4038
possibleTypes = [${possibleTypes}],
4139
description = "${trimDescription(node.description?.value)}"

src/helpers/add-dependent-types.ts renamed to src/helpers/add-dependent-types-to-only-types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ limitations under the License.
1313

1414
import { CodegenConfigWithDefaults } from "../config";
1515
import { getDependentTypeNames } from "./get-dependent-type-names";
16-
import { dependentTypeIsInScope } from "./dependent-type-is-in-scope";
1716
import { GraphQLSchema } from "graphql";
1817

19-
export function addDependentTypes(
18+
export function addDependentTypesToOnlyTypes(
2019
config: CodegenConfigWithDefaults,
2120
schema: GraphQLSchema,
2221
) {
2322
if (!config.onlyTypes) {
24-
throw new Error(`config.onlyTypes is required to add dependent types`);
23+
throw new Error(`onlyTypes config is required to add dependent types`);
2524
}
2625
const onlyTypesNodes = config.onlyTypes
2726
.map((typeName) => schema.getType(typeName)?.astNode)
2827
.filter(Boolean);
2928
const dependentTypeNames = onlyTypesNodes.flatMap((node) =>
3029
getDependentTypeNames(schema, node, config),
3130
);
32-
const dependentTypesInScope = dependentTypeNames.filter((typeName) =>
33-
dependentTypeIsInScope(typeName, config),
34-
);
31+
const typesInScope = config.dependentTypesInScope;
32+
const dependentTypesInScope = typesInScope
33+
? dependentTypeNames.filter((typeName) => typesInScope.includes(typeName))
34+
: dependentTypeNames;
3535
config.onlyTypes.push(...dependentTypesInScope);
3636
}

src/helpers/build-annotations.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
InputValueDefinitionNode,
1919
TypeDefinitionNode,
2020
} from "graphql";
21+
import { buildDescriptionAnnotation } from "./build-description-annotation";
2122
import { buildDirectiveAnnotations } from "./build-directive-annotations";
2223
import { CodegenConfigWithDefaults } from "../config";
2324
import { TypeMetadata } from "./build-type-metadata";
@@ -30,37 +31,24 @@ export type DefinitionNode =
3031

3132
export function buildAnnotations({
3233
config,
33-
inputDescription,
3434
definitionNode,
3535
resolvedType,
3636
}: {
3737
config: CodegenConfigWithDefaults;
38-
inputDescription?: string;
39-
definitionNode?: DefinitionNode;
38+
definitionNode: DefinitionNode;
4039
resolvedType?: TypeMetadata;
4140
}) {
42-
const description =
43-
inputDescription ?? definitionNode?.description?.value ?? "";
44-
const isDeprecated = isDeprecatedDescription(description, resolvedType);
45-
const descriptionAnnotator = isDeprecated
46-
? "@Deprecated"
47-
: "@GraphQLDescription";
48-
const descriptionValue = isDeprecated
49-
? description.replace("DEPRECATED: ", "")
50-
: description;
51-
const trimmedDescription = trimDescription(descriptionValue);
52-
const descriptionAnnotation = description
53-
? `${descriptionAnnotator}("${trimmedDescription}")\n`
54-
: "";
55-
56-
const directiveAnnotations = definitionNode
57-
? buildDirectiveAnnotations(
58-
definitionNode,
59-
config,
60-
description,
61-
resolvedType,
62-
)
63-
: "";
41+
const description = definitionNode?.description?.value ?? "";
42+
const descriptionAnnotation = buildDescriptionAnnotation(
43+
description,
44+
definitionNode,
45+
config,
46+
resolvedType,
47+
);
48+
const directiveAnnotations = buildDirectiveAnnotations(
49+
definitionNode,
50+
config,
51+
);
6452
const unionAnnotation = resolvedType?.unionAnnotation
6553
? `@${resolvedType.unionAnnotation}\n`
6654
: "";
@@ -84,15 +72,6 @@ export function buildAnnotations({
8472
);
8573
}
8674

87-
export function isDeprecatedDescription(
88-
description?: string,
89-
resolvedType?: TypeMetadata,
90-
) {
91-
return (
92-
description?.startsWith("DEPRECATED: ") && !resolvedType?.unionAnnotation
93-
);
94-
}
95-
9675
export function trimDescription(description?: string) {
9776
return (
9877
description

src/helpers/build-config-with-defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function buildConfigWithDefaults(
1313
...config,
1414
extraImports: [
1515
"com.expediagroup.graphql.generator.annotations.*",
16-
...(config.extraImports ?? ([] satisfies string[])),
16+
...(config.extraImports ?? []),
1717
],
1818
} as const;
1919
}

0 commit comments

Comments
 (0)