Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions workspaces/leetcode-api/schema-patched.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions workspaces/leetcode-api/src/scripts/codegen/graphqlToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type DirectivesConfig = {
nonnegative?: boolean;
slug?: boolean;
trim?: boolean;
enumValues?: string[];
};

Expand Down Expand Up @@ -49,6 +50,11 @@ function parseDirectives(
res.slug = true;
break;
}
case "trim": {
invariant(!directive.arguments?.length, "No arguments allowed!");
res.trim = true;
break;
}
default: {
throw new Error("Unsupported directive " + directive.name.value);
}
Expand Down Expand Up @@ -91,10 +97,7 @@ function generateZod(
}
case "Int": {
const { nonnegative, ...rest } = parseDirectives(directives);
invariant(
Object.values(rest).every((v) => v === false),
"Unsupported directives!",
);
invariant(Object.keys(rest).length === 0, "Unsupported directives!");

return [
new ZodOutput(
Expand All @@ -120,28 +123,29 @@ function generateZod(
return [new ZodOutput("z.string()", true), []];
}
case "String": {
const { slug, enumValues, ...rest } = parseDirectives(directives);
invariant(!(slug && enumValues), "Incompatible directive combination!");
invariant(
Object.values(rest).every((v) => v === false),
"Unsupported directives!",
);
const { slug, trim, enumValues, ...rest } = parseDirectives(directives);
invariant(Object.keys(rest).length === 0, "Unsupported directives!");

if (enumValues) {
invariant(!slug && !trim, "Incompatible directive combination!");
return [
new ZodOutput(`z.enum(${JSON.stringify(enumValues)})`, true),
[],
];
}

if (slug) {
invariant(!trim, "@trim is redundant when @slug is enabled!");
return [
new ZodOutput("slugZodType", true),
['import { slugZodType } from "../../zod-types/slugZodType.ts"'],
];
}

return [new ZodOutput("z.string()", true), []];
return [
new ZodOutput(["z.string()", trim ? ".trim()" : ""].join(""), true),
[],
];
}
default: {
throw new Error("Unsupported scalar type: " + currentType.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { nameNode } from "./astNodeBuilders.ts";

// TODO: keep in sync with DirectivesConfig in graphqlToZod

export const CUSTOM_DIRECTIVES = ["nonnegative", "slug"] as const;
export const CUSTOM_DIRECTIVES = ["nonnegative", "slug", "trim"] as const;

// TODO: memoize by directiveName?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ export const FIELD_MODIFICATIONS: SafeObjectMap<
solutionNum: [markNonNull, addDirective("nonnegative")],
stats: [markNonNull],
submitUrl: [markNonNull],
title: [addDirective("trim")],
titleSlug: [addDirective("slug")],
topicTags: [markNonNull],
urlManager: [markNonNull],
},
SubmissionDumpNode: {
id: [markNonNull],
timestamp: [markNonNull],
title: [markNonNull],
title: [markNonNull, addDirective("trim")],
titleSlug: [markNonNull, addDirective("slug")],
},
};
Loading