Skip to content

Commit

Permalink
feat: add support for resolver input types
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann committed Apr 21, 2021
1 parent c17ff55 commit c35b318
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 198 deletions.
5 changes: 4 additions & 1 deletion src/GeneratorConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ interface GeneratorOptions {
export class GeneratorConfig {
readonly basePath: string;

readonly inputArgumentsName: string;

readonly paths: Paths;

readonly prismaClientImportPath: string;

constructor({ generator: { output }, otherGenerators }: GeneratorOptions) {
constructor({ generator: { config, output }, otherGenerators }: GeneratorOptions) {
if (output === null) {
throw new Error("Please define an output directory");
}
Expand All @@ -40,5 +42,6 @@ export class GeneratorConfig {
shared: "shared",
};
this.prismaClientImportPath = prismaClientPath.output.value;
this.inputArgumentsName = config.inputArgumentsName || "input";
}
}
18 changes: 16 additions & 2 deletions src/Handlers/BaseHandler/BaseParser/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DMMF } from "@prisma/generator-helper";

import type { GeneratorConfig } from "../../../GeneratorConfig";
import type { Enum, Field } from "../../../types";
import { NestJSTypes } from "../../../types";

Expand All @@ -26,6 +27,8 @@ interface TSFieldOptions {
}

export class BaseParser {
readonly config: GeneratorConfig;

readonly dmmf: DMMF.Document;

readonly graphqlScalarImports = new Set(["ByteResolver"]);
Expand All @@ -48,7 +51,8 @@ export class BaseParser {

readonly prismaImport = "Prisma";

constructor(dmmf: DMMF.Document) {
constructor(config: GeneratorConfig, dmmf: DMMF.Document) {
this.config = config;
this.dmmf = dmmf;

this.dmmf.schema.inputObjectTypes.prisma.forEach(({ name }) => {
Expand Down Expand Up @@ -133,6 +137,10 @@ export class BaseParser {
};
}

getInputTypeName(name: string): string {
return `${this.pascalCase(name)}${this.pascalCase(this.config.inputArgumentsName)}`;
}

getJsonImports({
field: { location, type },
jsonImports,
Expand Down Expand Up @@ -167,9 +175,10 @@ export class BaseParser {
getModelName(input: string): string | undefined {
// eslint-disable-next-line @typescript-eslint/init-declarations
let modelName: ReturnType<BaseParser["getModelName"]>;
const cleansedInput = input.replace(this.pascalCase(this.config.inputArgumentsName), "");

this.modelsList.forEach((model) => {
if (input.startsWith(model) || input.endsWith(model)) {
if (cleansedInput.startsWith(model) || cleansedInput.endsWith(model)) {
modelName = model;
}
});
Expand Down Expand Up @@ -228,6 +237,11 @@ export class BaseParser {
return inputType;
}

// eslint-disable-next-line class-methods-use-this
pascalCase(word: string): string {
return `${word.charAt(0).toLocaleUpperCase()}${word.slice(1)}`;
}

// eslint-disable-next-line class-methods-use-this
private _mapScalarToGraphQLType(scalar: string): string {
switch (scalar) {
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/BaseHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export abstract class BaseHandler {

constructor({ config, dmmf }: HandlerOptions) {
this.config = config;
this.baseParser = new BaseParser(dmmf);
this.baseParser = new BaseParser(config, dmmf);
this.baseFileGenerator = new BaseFileGenerator(this.baseParser, config);
}
}
4 changes: 4 additions & 0 deletions src/Handlers/EnumHandler/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("EnumHandler", () => {

const config: GeneratorConfig = {
basePath: `${process.cwd()}/fixtures/enumHandler/fixtures/output1`,
inputArgumentsName: "",
paths: {
enums: "./enum",
inputTypes: "",
Expand Down Expand Up @@ -60,6 +61,7 @@ describe("EnumHandler", () => {

const config: GeneratorConfig = {
basePath: `${process.cwd()}/fixtures/enumHandler/fixtures/output2`,
inputArgumentsName: "",
paths: {
enums: "./enum",
inputTypes: "",
Expand Down Expand Up @@ -92,6 +94,7 @@ describe("EnumHandler", () => {

const config: GeneratorConfig = {
basePath: `${process.cwd()}/fixtures/enumHandler/fixtures/output3`,
inputArgumentsName: "",
paths: {
enums: "./enum",
inputTypes: "",
Expand Down Expand Up @@ -153,6 +156,7 @@ describe("EnumHandler", () => {

const config: GeneratorConfig = {
basePath: `${process.cwd()}/fixtures/enumHandler/fixtures/output4`,
inputArgumentsName: "",
paths: {
enums: "./enum",
inputTypes: "",
Expand Down

0 comments on commit c35b318

Please sign in to comment.