Skip to content

Commit ff1811a

Browse files
authored
refactor: reorganize repo (#75)
* unit test * refactor * pass test * add mutation tests * fix integration test * refactor * reorganize * separate field definition functions * pull out boolean arg * pull out other boolean arg * refactor * refactor * bun version * set up docs workspace * undo * fix copywright headers
1 parent 11097d5 commit ff1811a

34 files changed

+602
-392
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ jobs:
3838

3939
- name: Integration Test
4040
run: bun integration
41+
42+
- name: Build Docs
43+
run: bun docs:build

.github/workflows/docs.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,8 @@ jobs:
3131
- name: Install Dependencies
3232
run: bun install
3333

34-
- name: Build Plugin
35-
run: bun run build
36-
37-
- name: Install Docs Dependencies
38-
run: bun install
39-
working-directory: docs
40-
4134
- name: Build Docs
4235
run: bun docs:build
43-
working-directory: docs
4436

4537
- name: Setup Pages
4638
uses: actions/configure-pages@v5

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bun lint && bun format && git add .
1+
bun lint:fix && bun format && git add .

bun.lockb

373 KB
Binary file not shown.

docs/bun.lockb

-472 KB
Binary file not shown.

docs/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "docs",
33
"private": true,
44
"scripts": {
5-
"docs:start": "docusaurus start",
6-
"docs:build": "tsc && bun generate && docusaurus build",
7-
"generate": "jsdoc2md --files ../dist/plugin.cjs --partial partials/main.hbs --partial partials/scope.hbs -c jsdoc.conf > docs/configuration.md"
5+
"build": "tsc && bun generate && docusaurus build",
6+
"generate": "jsdoc2md --files ../dist/plugin.cjs --partial partials/main.hbs --partial partials/scope.hbs -c jsdoc.conf > docs/configuration.md",
7+
"start": "docusaurus start"
88
},
99
"devDependencies": {
1010
"@docusaurus/core": "3.3.2",
@@ -13,6 +13,7 @@
1313
"@docusaurus/tsconfig": "3.3.2",
1414
"@docusaurus/types": "3.3.2",
1515
"@mdx-js/react": "3.0.1",
16+
"ajv": "8.14.0",
1617
"clsx": "2.1.1",
1718
"jsdoc-to-markdown": "8.0.1",
1819
"prism-react-renderer": "2.3.1",

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export default [
2020
},
2121
},
2222
{
23-
ignores: ["build", "dist", "docs"],
23+
ignores: ["build", "dist", "docs", "scripts"],
2424
},
2525
];

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@expediagroup/graphql-kotlin-codegen",
3-
"packageManager": "bun@1.1.6",
3+
"packageManager": "bun@1.1.10",
44
"main": "dist/plugin.cjs",
55
"types": "dist/plugin.d.cts",
66
"files": [
@@ -22,6 +22,7 @@
2222
"@graphql-codegen/java-common": "3.0.0",
2323
"@graphql-codegen/plugin-helpers": "5.0.4",
2424
"@graphql-codegen/visitor-plugin-common": "5.2.0",
25+
"ts-deepmerge": "7.0.0",
2526
"valibot": "0.30.0"
2627
},
2728
"devDependencies": {
@@ -37,13 +38,19 @@
3738
},
3839
"scripts": {
3940
"build": "tsup src/plugin.ts --clean --dts --external graphql",
41+
"docs:build": "bun run build && bun --filter docs build",
42+
"docs:start": "bun --filter docs start",
4043
"format": "prettier --write .",
4144
"format-check": "prettier --check .",
4245
"integration": "bun run build && graphql-codegen && ./gradlew graphqlGenerateSDL && bun test ./test/integration.test.ts",
43-
"lint": "eslint .",
46+
"lint": "bun ./scripts/check-headers.ts && eslint .",
47+
"lint:fix": "bun ./scripts/fix-headers.ts && eslint --fix .",
4448
"prepack": "bun run build",
4549
"prepare": "husky",
4650
"unit": "bun test ./test/plugin.test.ts"
4751
},
48-
"type": "module"
52+
"type": "module",
53+
"workspaces": [
54+
"docs"
55+
]
4956
}

scripts/check-headers.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Glob } from "bun";
2+
3+
const filePaths = new Glob("src/**/*.ts").scan();
4+
5+
let filesWithoutCopyrightHeader: string[] = [];
6+
for await (const filePath of filePaths) {
7+
const fileContents = await Bun.file(filePath).text();
8+
if (!fileContents.startsWith("/*\nCopyright")) {
9+
filesWithoutCopyrightHeader.push(filePath);
10+
}
11+
}
12+
13+
if (filesWithoutCopyrightHeader.length) {
14+
console.error(
15+
`\nThe following files are missing a valid copyright header:${filesWithoutCopyrightHeader.map((file) => `\n • ${file}`).join()}`,
16+
);
17+
process.exit(1);
18+
}

scripts/fix-headers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Glob } from "bun";
2+
3+
const copyrightHeader = `/*
4+
Copyright 2024 Expedia, Inc.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
`;
17+
18+
const filePaths = new Glob("src/**/*.ts").scan();
19+
for await (const filePath of filePaths) {
20+
const fileContents = await Bun.file(filePath).text();
21+
if (!fileContents.startsWith("/*\nCopyright")) {
22+
await Bun.write(filePath, `${copyrightHeader}${fileContents}`);
23+
}
24+
}

src/helpers/build-annotations.ts renamed to src/annotations/build-annotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
} from "graphql";
2222
import { buildDescriptionAnnotation } from "./build-description-annotation";
2323
import { buildDirectiveAnnotations } from "./build-directive-annotations";
24-
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
25-
import { TypeMetadata } from "./build-type-metadata";
24+
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
25+
import { TypeMetadata } from "../utils/build-type-metadata";
2626

2727
export type DefinitionNode =
2828
| TypeDefinitionNode

src/helpers/build-description-annotation.ts renamed to src/annotations/build-description-annotation.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
2-
import { TypeMetadata } from "./build-type-metadata";
1+
/*
2+
Copyright 2024 Expedia, Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
https://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
15+
import { TypeMetadata } from "../utils/build-type-metadata";
316
import { indent } from "@graphql-codegen/visitor-plugin-common";
417
import { Kind } from "graphql/index";
518
import { DefinitionNode, trimDescription } from "./build-annotations";

src/helpers/build-directive-annotations.ts renamed to src/annotations/build-directive-annotations.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
14+
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
1515
import { DefinitionNode } from "./build-annotations";
16-
import { getFederationDirectiveReplacement } from "./get-federation-directive-replacement";
1716
import { ConstDirectiveNode } from "graphql/language";
1817
import { Kind } from "graphql";
1918

@@ -75,3 +74,23 @@ function buildKotlinAnnotations(
7574
return `@${kotlinAnnotation.annotationName}(${directiveArguments})`;
7675
});
7776
}
77+
78+
function getFederationDirectiveReplacement(directive: ConstDirectiveNode) {
79+
const federationDirectivePrefix =
80+
"com.expediagroup.graphql.generator.federation.directives.";
81+
switch (directive.name.value) {
82+
case "key":
83+
if (
84+
directive.arguments?.[0] &&
85+
directive.arguments[0].value.kind === Kind.STRING
86+
) {
87+
const fieldArg = directive.arguments[0]?.value.value;
88+
return `@${federationDirectivePrefix}KeyDirective(${federationDirectivePrefix}FieldSet("${fieldArg}"))`;
89+
}
90+
return undefined;
91+
case "extends":
92+
return `@${federationDirectivePrefix}ExtendsDirective`;
93+
case "external":
94+
return `@${federationDirectivePrefix}ExternalDirective`;
95+
}
96+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ limitations under the License.
1212
*/
1313

1414
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
15-
import { getDependentTypeNames } from "./get-dependent-type-names";
1615
import { GraphQLSchema } from "graphql";
16+
import { TypeDefinitionNode } from "graphql/index";
17+
import {
18+
getDependentFieldTypeNames,
19+
getDependentInterfaceNames,
20+
getDependentUnionNames,
21+
} from "../utils/dependent-type-utils";
1722

1823
export function addDependentTypesToOnlyTypes(
1924
config: CodegenConfigWithDefaults,
@@ -34,3 +39,18 @@ export function addDependentTypesToOnlyTypes(
3439
: dependentTypeNames;
3540
config.onlyTypes.push(...dependentTypesInScope);
3641
}
42+
43+
function getDependentTypeNames(
44+
schema: GraphQLSchema,
45+
node: TypeDefinitionNode,
46+
config: CodegenConfigWithDefaults,
47+
): string[] {
48+
const namedTypes = getDependentFieldTypeNames(node, config)
49+
.concat(getDependentUnionNames(node))
50+
.concat(getDependentInterfaceNames(node));
51+
const recursivelyFoundTypes = namedTypes
52+
.map((typeName) => schema.getType(typeName)?.astNode)
53+
.filter(Boolean)
54+
.flatMap((node) => getDependentTypeNames(schema, node, config));
55+
return namedTypes.concat(recursivelyFoundTypes);
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2024 Expedia, Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
https://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { GraphQLKotlinCodegenConfig } from "../plugin";
15+
import { buildPackageNameFromPath } from "@graphql-codegen/java-common";
16+
import { dirname, normalize } from "path";
17+
import { merge } from "ts-deepmerge";
18+
19+
export function buildConfigWithDefaults(
20+
config: GraphQLKotlinCodegenConfig,
21+
outputFile: string,
22+
) {
23+
const defaultConfig = {
24+
packageName: buildPackageNameFromPath(dirname(normalize(outputFile))),
25+
includeDependentTypes: true,
26+
unionGeneration: "MARKER_INTERFACE",
27+
extraImports: ["com.expediagroup.graphql.generator.annotations.*"],
28+
} as const satisfies GraphQLKotlinCodegenConfig;
29+
30+
return merge(defaultConfig, config) as GraphQLKotlinCodegenConfig &
31+
typeof defaultConfig;
32+
}
33+
34+
export type CodegenConfigWithDefaults = ReturnType<
35+
typeof buildConfigWithDefaults
36+
>;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2024 Expedia, Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
https://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { InterfaceTypeDefinitionNode, ObjectTypeDefinitionNode } from "graphql";
15+
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
16+
17+
export function findTypeInResolverInterfacesConfig(
18+
node: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
19+
config: CodegenConfigWithDefaults,
20+
) {
21+
return config.resolverInterfaces?.findLast(
22+
(resolverInterface) => resolverInterface.typeName === node.name.value,
23+
);
24+
}
File renamed without changes.

src/helpers/should-include-type-definition.ts renamed to src/config/should-exclude-type-definition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ limitations under the License.
1414
import { TypeDefinitionNode } from "graphql";
1515
import { CodegenConfigWithDefaults } from "./build-config-with-defaults";
1616

17-
export function shouldIncludeTypeDefinition(
17+
export function shouldExcludeTypeDefinition(
1818
node: TypeDefinitionNode,
1919
config: CodegenConfigWithDefaults,
2020
) {
21-
return !config.onlyTypes || config.onlyTypes.includes(node.name.value);
21+
return config.onlyTypes && !config.onlyTypes.includes(node.name.value);
2222
}

src/definitions/enum.ts

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

1414
import { EnumTypeDefinitionNode, EnumValueDefinitionNode } from "graphql";
1515
import { indentMultiline } from "@graphql-codegen/visitor-plugin-common";
16-
import { buildAnnotations } from "../helpers/build-annotations";
17-
import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition";
18-
import { CodegenConfigWithDefaults } from "../helpers/build-config-with-defaults";
16+
import { buildAnnotations } from "../annotations/build-annotations";
17+
import { shouldExcludeTypeDefinition } from "../config/should-exclude-type-definition";
18+
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults";
1919

2020
export function buildEnumTypeDefinition(
2121
node: EnumTypeDefinitionNode,
2222
config: CodegenConfigWithDefaults,
2323
) {
24-
if (!shouldIncludeTypeDefinition(node, config)) {
24+
if (shouldExcludeTypeDefinition(node, config)) {
2525
return "";
2626
}
2727

0 commit comments

Comments
 (0)