Skip to content

Commit

Permalink
refactor: use graphql visitor (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Mar 10, 2024
1 parent 9ed59e2 commit 86132d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
19 changes: 9 additions & 10 deletions src/definitions/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ limitations under the License.
import { EnumTypeDefinitionNode, EnumValueDefinitionNode } from "graphql";
import { indentMultiline } from "@graphql-codegen/visitor-plugin-common";
import { buildAnnotations } from "../helpers/build-annotations";
import { KotlinResolversVisitor } from "../visitor";
import { shouldIncludeTypeDefinition } from "../helpers/should-include-type-definition";
import { GraphQLKotlinCodegenConfig } from "../plugin";

Expand All @@ -27,14 +26,17 @@ export function buildEnumTypeDefinition(
}

const enumName = node.name.value;
const enumValues = indentMultiline((node.values ?? []).join(",\n") + ";", 2);
const enumValues =
node.values?.map((valueNode) => {
return buildEnumValueDefinition(valueNode, config);
}) ?? [];

const annotations = buildAnnotations({
config,
definitionNode: node,
});
return `${annotations}enum class ${enumName}(val label: String) {
${enumValues}
${indentMultiline(enumValues.join(",\n") + ";", 2)}
companion object {
@JvmStatic
Expand All @@ -45,16 +47,13 @@ ${enumValues}
}`;
}

export function buildEnumValueDefinition(
this: KotlinResolversVisitor,
function buildEnumValueDefinition(
node: EnumValueDefinitionNode,
config: GraphQLKotlinCodegenConfig,
) {
const annotations = buildAnnotations({
config: this.config,
config,
definitionNode: node,
});
return `${annotations}${this.convertName(node, {
useTypesPrefix: false,
transformUnderscore: true,
})}("${node.name.value}")`;
return `${annotations}${config.convert(node)}("${node.name.value}")`;
}
21 changes: 12 additions & 9 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ limitations under the License.
import { dirname, normalize } from "path";
import {
getCachedDocumentNodeFromSchema,
oldVisit,
PluginFunction,
} from "@graphql-codegen/plugin-helpers";
import { buildPackageNameFromPath } from "@graphql-codegen/java-common";
import { KotlinResolversVisitor } from "./visitor";
import { RawConfig } from "@graphql-codegen/visitor-plugin-common";
import { KotlinVisitor } from "./visitor";
import {
ParsedConfig,
RawConfig,
} from "@graphql-codegen/visitor-plugin-common";
import { Input, safeParse } from "valibot";
import { configSchema } from "./config";
import { addDependentTypes } from "./helpers/add-dependent-types";
import { visit } from "graphql";

export type GraphQLKotlinCodegenConfig = RawConfig & Input<typeof configSchema>;
export type GraphQLKotlinCodegenConfig = RawConfig &
ParsedConfig &
Input<typeof configSchema>;
export const plugin: PluginFunction<GraphQLKotlinCodegenConfig> = (
schema,
_,
Expand All @@ -48,11 +53,9 @@ export const plugin: PluginFunction<GraphQLKotlinCodegenConfig> = (
}

addDependentTypes(config, schema);
const visitor = new KotlinResolversVisitor(config, schema);
const visitor = new KotlinVisitor(config, schema);
const astNode = getCachedDocumentNodeFromSchema(schema);
const visitorResult: { definitions: unknown[] } = oldVisit(astNode, {
leave: visitor as Parameters<typeof oldVisit>[1]["leave"],
});
const { definitions } = visit(astNode, visitor);
const packageName = `package ${
config.packageName ?? buildPackageNameFromPath(relevantPath)
}\n`;
Expand All @@ -62,7 +65,7 @@ export const plugin: PluginFunction<GraphQLKotlinCodegenConfig> = (
.concat(config.extraImports ?? [])
.map((annotation) => `import ${annotation}`)
.join("\n") + "\n";
const typeDefinitions = visitorResult.definitions
const typeDefinitions = definitions
.filter((d: unknown) => typeof d === "string" && d.length)
.join("\n\n");

Expand Down
22 changes: 5 additions & 17 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,31 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {
BaseVisitor,
ParsedConfig,
RawConfig,
} from "@graphql-codegen/visitor-plugin-common";
import { BaseVisitor, RawConfig } from "@graphql-codegen/visitor-plugin-common";
import {
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
GraphQLSchema,
InterfaceTypeDefinitionNode,
InputObjectTypeDefinitionNode,
ObjectTypeDefinitionNode,
UnionTypeDefinitionNode,
} from "graphql";
import { GraphQLKotlinCodegenConfig } from "./plugin";
import {
buildEnumTypeDefinition,
buildEnumValueDefinition,
} from "./definitions/enum";
import { buildEnumTypeDefinition } from "./definitions/enum";
import { buildInterfaceDefinition } from "./definitions/interface";
import { buildInputObjectDefinition } from "./definitions/input";
import { buildObjectTypeDefinition } from "./definitions/object";
import { buildUnionTypeDefinition } from "./definitions/union";

export class KotlinResolversVisitor extends BaseVisitor<
export class KotlinVisitor extends BaseVisitor<
RawConfig,
GraphQLKotlinCodegenConfig & ParsedConfig
GraphQLKotlinCodegenConfig
> {
constructor(
rawConfig: GraphQLKotlinCodegenConfig,
protected _schema: GraphQLSchema,
) {
super(rawConfig, rawConfig as GraphQLKotlinCodegenConfig & ParsedConfig);
}

EnumValueDefinition(node: EnumValueDefinitionNode): string {
return buildEnumValueDefinition.bind(this)(node);
super(rawConfig, rawConfig);
}

EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
Expand Down
3 changes: 1 addition & 2 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ const testCases = await Promise.all(
}
return {
testName,
config: (await import(`${absolutePath}/codegen.config.ts`))
.default as GraphQLKotlinCodegenConfig,
config: (await import(`${absolutePath}/codegen.config.ts`)).default,
};
}),
);
Expand Down

0 comments on commit 86132d4

Please sign in to comment.