Skip to content

Commit 2585a38

Browse files
authored
Support a @trim directive when patching GraphQL schema (#485)
1 parent 057b1a3 commit 2585a38

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

workspaces/leetcode-api/schema-patched.graphql

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/fetchGraphQL.generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspaces/leetcode-api/src/api/question-list/fetchGraphQL.generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspaces/leetcode-api/src/api/recent-ac-submission-list/fetchGraphQL.generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspaces/leetcode-api/src/scripts/codegen/graphqlToZod.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type DirectivesConfig = {
1919
nonnegative?: boolean;
2020
slug?: boolean;
21+
trim?: boolean;
2122
enumValues?: string[];
2223
};
2324

@@ -49,6 +50,11 @@ function parseDirectives(
4950
res.slug = true;
5051
break;
5152
}
53+
case "trim": {
54+
invariant(!directive.arguments?.length, "No arguments allowed!");
55+
res.trim = true;
56+
break;
57+
}
5258
default: {
5359
throw new Error("Unsupported directive " + directive.name.value);
5460
}
@@ -91,10 +97,7 @@ function generateZod(
9197
}
9298
case "Int": {
9399
const { nonnegative, ...rest } = parseDirectives(directives);
94-
invariant(
95-
Object.values(rest).every((v) => v === false),
96-
"Unsupported directives!",
97-
);
100+
invariant(Object.keys(rest).length === 0, "Unsupported directives!");
98101

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

130129
if (enumValues) {
130+
invariant(!slug && !trim, "Incompatible directive combination!");
131131
return [
132132
new ZodOutput(`z.enum(${JSON.stringify(enumValues)})`, true),
133133
[],
134134
];
135135
}
136136

137137
if (slug) {
138+
invariant(!trim, "@trim is redundant when @slug is enabled!");
138139
return [
139140
new ZodOutput("slugZodType", true),
140141
['import { slugZodType } from "../../zod-types/slugZodType.ts"'],
141142
];
142143
}
143144

144-
return [new ZodOutput("z.string()", true), []];
145+
return [
146+
new ZodOutput(["z.string()", trim ? ".trim()" : ""].join(""), true),
147+
[],
148+
];
145149
}
146150
default: {
147151
throw new Error("Unsupported scalar type: " + currentType.name);

workspaces/leetcode-api/src/scripts/patch-graphql-schema/addDirectiveToField.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { nameNode } from "./astNodeBuilders.ts";
99

1010
// TODO: keep in sync with DirectivesConfig in graphqlToZod
1111

12-
export const CUSTOM_DIRECTIVES = ["nonnegative", "slug"] as const;
12+
export const CUSTOM_DIRECTIVES = ["nonnegative", "slug", "trim"] as const;
1313

1414
// TODO: memoize by directiveName?
1515

workspaces/leetcode-api/src/scripts/patch-graphql-schema/modifications.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ export const FIELD_MODIFICATIONS: SafeObjectMap<
6868
solutionNum: [markNonNull, addDirective("nonnegative")],
6969
stats: [markNonNull],
7070
submitUrl: [markNonNull],
71+
title: [addDirective("trim")],
7172
titleSlug: [addDirective("slug")],
7273
topicTags: [markNonNull],
7374
urlManager: [markNonNull],
7475
},
7576
SubmissionDumpNode: {
7677
id: [markNonNull],
7778
timestamp: [markNonNull],
78-
title: [markNonNull],
79+
title: [markNonNull, addDirective("trim")],
7980
titleSlug: [markNonNull, addDirective("slug")],
8081
},
8182
};

0 commit comments

Comments
 (0)