Skip to content

Commit 924fb1f

Browse files
committed
Also migrate schema scraping
1 parent 0149aea commit 924fb1f

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

workspaces/leetcode-api/src/fetchGraphQLData.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

workspaces/leetcode-api/src/fetchGraphQLTypeInformation.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import {
99
import { squashWhitespace } from "@code-chronicles/util/squashWhitespace";
1010
import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow";
1111

12-
import { fetchGraphQLData } from "./fetchGraphQLData.ts";
1312
import { normalizeGraphQLDescription } from "./normalizeGraphQLDescription.ts";
1413
import { sortByName } from "./sortByName.ts";
14+
import { getGraphQLClient } from "./getGraphQLClient.ts";
15+
import { rawRequest } from "graphql-request";
16+
import { GraphQLError } from "graphql";
1517

1618
function getTypeFields(depth: number): string {
1719
const base = "name kind";
@@ -78,9 +80,10 @@ function graphqlDecode(s: string): string {
7880
).toString("utf8");
7981
}
8082

81-
function getQueryAndVariables(
82-
typeNames: Iterable<string>,
83-
): [string, Record<string, string>] {
83+
function getQueryAndVariables(typeNames: Iterable<string>): {
84+
query: string;
85+
variables: Record<string, string>;
86+
} {
8487
const variables = Object.fromEntries(
8588
// TODO: .map the iterator once that's more widespread!
8689
[...typeNames].map((typeName, index) => [`typeName${index}`, typeName]),
@@ -94,12 +97,12 @@ function getQueryAndVariables(
9497
`${graphqlEncode(typeName)}: __type(name: $${variable}) { ...TypeFields }`,
9598
);
9699

97-
return [
98-
squashWhitespace(
100+
return {
101+
query: squashWhitespace(
99102
`query (${queryArgs.join(",")}) {${queryFields.join(",")}}\n${FRAGMENT}`,
100103
),
101104
variables,
102-
];
105+
};
103106
}
104107

105108
const innerTypeZodTypeBase = z.strictObject({
@@ -203,13 +206,18 @@ export type LeetCodeGraphQLType = NonNullable<
203206

204207
export async function fetchGraphQLTypeInformation(
205208
typeNames: string[],
209+
onErrors?: (errors: GraphQLError[]) => void,
206210
): Promise<Record<string, LeetCodeGraphQLType | null>> {
207211
const distinctTypeNames = new Set(typeNames);
208212
if (distinctTypeNames.size === 0) {
209213
return {};
210214
}
211215

212-
const [query, variables] = getQueryAndVariables(distinctTypeNames);
213-
const { data } = await fetchGraphQLData(query, variables);
216+
const { data, errors } = await getGraphQLClient("all").rawRequest(
217+
getQueryAndVariables(distinctTypeNames),
218+
);
219+
220+
errors && errors.length > 0 && onErrors?.(errors);
221+
214222
return graphqlTypeZodType.parse(data);
215223
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { GraphQLClient } from "graphql-request";
22

3-
let client: GraphQLClient | null = null;
3+
type ErrorPolicy = NonNullable<
4+
NonNullable<ConstructorParameters<typeof GraphQLClient>[1]>["errorPolicy"]
5+
>;
46

5-
export function getGraphQLClient(): GraphQLClient {
6-
return (client ??= new GraphQLClient("https://leetcode.com/graphql/"));
7+
export function getGraphQLClient(
8+
errorPolicy: ErrorPolicy = "none",
9+
): GraphQLClient {
10+
return new GraphQLClient("https://leetcode.com/graphql/", {
11+
method: "POST",
12+
headers: { "Content-Type": "application/json" },
13+
excludeOperationName: true,
14+
errorPolicy,
15+
});
716
}

workspaces/leetcode-api/src/scripts/scrape-graphql-schema/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ async function main(): Promise<void> {
5454
const typeNames = popMany(stack, BATCH_SIZE);
5555
console.error(`Fetching ${typeNames.join(", ")}, ${stack.length} to go`);
5656

57-
const typeInfosBatch = await fetchGraphQLTypeInformation(typeNames);
57+
const typeInfosBatch = await fetchGraphQLTypeInformation(
58+
typeNames,
59+
(errors) => {
60+
errors.forEach((err) => {
61+
console.error("Got GraphQLError", err);
62+
});
63+
},
64+
);
5865

5966
for (const typeName of typeNames) {
6067
const typeInfo = typeInfosBatch[typeName];

0 commit comments

Comments
 (0)