Skip to content

Commit 24d2585

Browse files
authored
Normalize descriptions in scraped GraphQL schema (#420)
The specific thing we're normalizing here is the description of `EmailTemplateNode`'s `content` field, which seems to be programmatically generated and the list it contains is slightly different each time. After this PR, the list in the description is alphabetized so it should stay consistent across scrapes (unless something _really_ changed).
1 parent 0703412 commit 24d2585

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

workspaces/leetcode-api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"lint": "eslint --color --max-warnings=0 .",
1919
"scrape-graphql-schema": "ts-node src/scripts/scrape-graphql-schema/main.ts",
2020
"typecheck": "tsc --pretty --project .",
21-
"validate-graphql-schema": "ts-node src/scripts/validateGraphQLSchema.ts"
21+
"validate-graphql-schema": "ts-node src/scripts/validate-graphql-schema/main.ts"
2222
},
2323
"dependencies": {
2424
"@code-chronicles/util": "0.0.1",
@@ -32,6 +32,7 @@
3232
"eslint": "9.9.1",
3333
"graphql": "16.9.0",
3434
"prettier": "3.3.3",
35+
"ts-node": "10.9.2",
3536
"typescript": "5.5.4"
3637
}
3738
}

workspaces/leetcode-api/schema.graphql

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

workspaces/leetcode-api/src/fetchGraphQLTypeInformation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { squashWhitespace } from "@code-chronicles/util/squashWhitespace";
1010
import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow";
1111

1212
import { fetchGraphQLData } from "./fetchGraphQLData";
13+
import { normalizeGraphQLDescription } from "./normalizeGraphQLDescription";
1314
import { sortByName } from "./sortByName";
1415

1516
function getTypeFields(depth: number): string {
@@ -124,7 +125,9 @@ const nameAndDescriptionZodType = z.strictObject({
124125
.string()
125126
.nullable()
126127
.transform((desc) =>
127-
desc != null && !isStringEmptyOrWhitespaceOnly(desc) ? desc : undefined,
128+
desc != null && !isStringEmptyOrWhitespaceOnly(desc)
129+
? normalizeGraphQLDescription(desc)
130+
: undefined,
128131
),
129132
});
130133

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { compareStringsCaseInsensitive } from "@code-chronicles/util/compareStringsCaseInsensitive";
2+
3+
export function normalizeGraphQLDescription(desc: string): string {
4+
return desc.replaceAll(/<ul>((?:<li>[^<]*<\/li>)*)<\/ul>/g, (_, lisMatch) => {
5+
const items = Array.from(
6+
lisMatch.matchAll(/<li>([^<]*)<\/li>/g),
7+
([, liMatch]) => liMatch,
8+
)
9+
.sort(compareStringsCaseInsensitive)
10+
.map((item) => `<li>${item}</li>`);
11+
12+
return `<ul>${items.join("")}</ul>`;
13+
});
14+
}

workspaces/leetcode-api/src/scripts/validateGraphQLSchema.ts renamed to workspaces/leetcode-api/src/scripts/validate-graphql-schema/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { buildSchema, validateSchema } from "graphql";
44

55
import { maybeThrow } from "@code-chronicles/util/maybeThrow";
66

7-
import { SCHEMA_FILE } from "./scrape-graphql-schema/constants";
7+
import { SCHEMA_FILE } from "../scrape-graphql-schema/constants";
88

99
async function main(): Promise<void> {
1010
const schema = buildSchema(await readFile(SCHEMA_FILE, "utf8"));

yarn.lock

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

0 commit comments

Comments
 (0)