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
62 changes: 62 additions & 0 deletions workspaces/leetcode-api/graphql-codegen.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { CodegenConfig } from "@graphql-codegen/cli";

const commonTypeScriptPluginConfig = {
arrayInputCoercion: false,
enumsAsTypes: true,
defaultScalarType: "unknown",
skipTypename: true,
useTypeImports: true,

// TODO: add strictScalars: true
};

const headerPlugin = {
add: {
content: `
// THIS FILE IS GENERATED! DO NOT MODIFY IT MANUALLY!!
// Instead, update the generation process or inputs and run \`yarn codegen\`.
`,
},
};

const config: CodegenConfig = {
schema: "schema.graphql",
documents: ["src/**/*.graphql"],
overwrite: true,
emitLegacyCommonJSImports: false,
generates: {
"src/graphqlTypes.generated.ts": {
plugins: [headerPlugin, "typescript"],
config: commonTypeScriptPluginConfig,
},
"src/": {
preset: "near-operation-file",
presetConfig: {
// The base types are not imported because of the use of
// `globalNamespace` below, instead our custom plugin will add the
// import, so that it can end up _below_ the header.
baseTypesPath: "<not-used-but-cannot-be-empty>",

extension: ".generated.ts",
fileName: "fetchGraphQL",
},
plugins: [
headerPlugin,
{ "typescript-operations": { globalNamespace: false } },
"./src/scripts/codegen/graphqlCodegenPlugin.ts",
],
config: {
...commonTypeScriptPluginConfig,

// Our custom plugin will handle the imports and exports!
globalNamespace: true,
noExport: true,
},
},
},
hooks: {
afterAllFileWrite: ["prettier --write"],
},
};

export default config;
39 changes: 0 additions & 39 deletions workspaces/leetcode-api/graphql-codegen.ts

This file was deleted.

6 changes: 5 additions & 1 deletion workspaces/leetcode-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "module",
"exports": "./src/main.ts",
"scripts": {
"codegen": "graphql-codegen-esm --config graphql-codegen.ts",
"codegen": "cross-env NODE_OPTIONS=\"--import tsx\" graphql-codegen-esm --config graphql-codegen.config.ts",
"format": "prettier --color --write .",
"lint": "eslint --color --max-warnings=0 .",
"scrape-graphql-schema": "tsx src/scripts/scrape-graphql-schema/main.ts",
Expand All @@ -32,14 +32,18 @@
},
"devDependencies": {
"@code-chronicles/eslint-config": "workspace:*",
"@graphql-codegen/add": "5.0.3",
"@graphql-codegen/cli": "5.0.2",
"@graphql-codegen/near-operation-file-preset": "3.0.0",
"@types/node": "22.7.4",
"cross-env": "7.0.3",
"eslint": "9.12.0",
"graphql-query-compress": "1.2.4",
"jest": "29.7.0",
"prettier": "3.3.3",
"ts-jest": "29.2.5",
"tsx": "4.19.1",
"type-fest": "4.26.1",
"typescript": "5.6.2"
}
}
9 changes: 8 additions & 1 deletion workspaces/leetcode-api/schema.graphql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// THIS FILE IS GENERATED! DO NOT MODIFY IT MANUALLY!!
// Instead, update the generation process or inputs and run `yarn codegen`.

import type { Simplify } from "type-fest";

import { getGraphQLClient } from "../../getGraphQLClient.ts";
import type * as Types from "../../graphqlTypes.generated.ts";

type ActiveDailyCodingChallengeQuestionQueryVariables = Types.Exact<{
[key: string]: never;
}>;

type ActiveDailyCodingChallengeQuestionQuery = {
activeDailyCodingChallengeQuestion?: {
date: unknown;
question: {
difficulty?: string | null;
questionFrontendId?: string | null;
title: string;
titleSlug: string;
};
} | null;
};

export type QueryVariables =
Simplify<ActiveDailyCodingChallengeQuestionQueryVariables>;
export type Query = Simplify<ActiveDailyCodingChallengeQuestionQuery>;

export const QUERY =
"query ActiveDailyCodingChallengeQuestion{activeDailyCodingChallengeQuestion{date question{difficulty questionFrontendId title titleSlug}}}";

export function fetchGraphQL(variables: QueryVariables): Promise<Query> {
return getGraphQLClient().request(QUERY, variables);
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import { gql } from "graphql-request";
import { z } from "zod";

import { numericIdAsNumberZodType } from "@code-chronicles/util/numericIdAsNumberZodType";
import { sleep } from "@code-chronicles/util/sleep";
import { MS_IN_SEC } from "@code-chronicles/util/timeConstants";
import { timestampInSecondsToYearMonthDay } from "@code-chronicles/util/timestampInSecondsToYearMonthDay";

import { getGraphQLClient } from "./getGraphQLClient.ts";
import { questionDifficultyZodType } from "./zod-types/questionDifficultyZodType.ts";
import { questionTitleSlugZodType } from "./zod-types/questionTitleSlugZodType.ts";

const QUERY = gql`
query fetchActiveDailyCodingChallengeQuestion {
activeDailyCodingChallengeQuestion {
date
question {
difficulty
questionFrontendId
title
titleSlug
}
}
}
`;
import { questionDifficultyZodType } from "../../zod-types/questionDifficultyZodType.ts";
import { questionTitleSlugZodType } from "../../zod-types/questionTitleSlugZodType.ts";
import { fetchGraphQL } from "./fetchGraphQL.generated.ts";

const questionZodType = z.object({
difficulty: questionDifficultyZodType,
Expand All @@ -48,7 +33,8 @@ export type ActiveDailyCodingChallengeQuestion = z.infer<
>;

export async function fetchActiveDailyCodingChallengeQuestionWithoutDateValidation(): Promise<ActiveDailyCodingChallengeQuestion> {
const data = await getGraphQLClient().request(QUERY);
// TODO: have a way to omit variables when there aren't any
const data = await fetchGraphQL({});

return activeDailyCodingChallengeQuestionZodType.parse(data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query ActiveDailyCodingChallengeQuestion {
activeDailyCodingChallengeQuestion {
date
question {
difficulty
questionFrontendId
title
titleSlug
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// THIS FILE IS GENERATED! DO NOT MODIFY IT MANUALLY!!
// Instead, update the generation process or inputs and run `yarn codegen`.

import type { Simplify } from "type-fest";

import { getGraphQLClient } from "../../getGraphQLClient.ts";
import type * as Types from "../../graphqlTypes.generated.ts";

type QuestionListQueryVariables = Types.Exact<{
categorySlug: Types.Scalars["String"]["input"];
limit?: Types.InputMaybe<Types.Scalars["Int"]["input"]>;
skip?: Types.InputMaybe<Types.Scalars["Int"]["input"]>;
filters: Types.QuestionListFilterInput;
}>;

type QuestionListQuery = {
questionList?: {
totalNum: number;
data: Array<{
difficulty?: string | null;
isPaidOnly?: boolean | null;
questionFrontendId?: string | null;
title: string;
titleSlug: string;
}>;
} | null;
};

export type QueryVariables = Simplify<QuestionListQueryVariables>;
export type Query = Simplify<QuestionListQuery>;

export const QUERY =
"query QuestionList($categorySlug:String!,$limit:Int,$skip:Int,$filters:QuestionListFilterInput!){questionList(categorySlug:$categorySlug limit:$limit skip:$skip filters:$filters){data{difficulty isPaidOnly questionFrontendId title titleSlug}totalNum}}";

export function fetchGraphQL(variables: QueryVariables): Promise<Query> {
return getGraphQLClient().request(QUERY, variables);
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
import { gql } from "graphql-request";
import { z } from "zod";

import { numericIdAsNumberZodType } from "@code-chronicles/util/numericIdAsNumberZodType";

import { getGraphQLClient } from "./getGraphQLClient.ts";
import { questionDifficultyZodType } from "./zod-types/questionDifficultyZodType.ts";
import { questionTitleSlugZodType } from "./zod-types/questionTitleSlugZodType.ts";

const QUERY = gql`
query (
$categorySlug: String!
$limit: Int
$skip: Int
$filters: QuestionListFilterInput!
) {
questionList(
categorySlug: $categorySlug
limit: $limit
skip: $skip
filters: $filters
) {
data {
difficulty
isPaidOnly
questionFrontendId
title
titleSlug
}
totalNum
}
}
`;
import { fetchGraphQL, type QueryVariables } from "./fetchGraphQL.generated.ts";
import { questionDifficultyZodType } from "../../zod-types/questionDifficultyZodType.ts";
import { questionTitleSlugZodType } from "../../zod-types/questionTitleSlugZodType.ts";

const questionZodType = z.object({
difficulty: questionDifficultyZodType,
Expand Down Expand Up @@ -74,12 +48,11 @@ export async function fetchQuestionList({
skip,
}: {
categorySlug?: CategorySlug;
// TODO: more specific type if possible
filters?: Record<string, unknown>;
limit?: number;
skip?: number;
} = {}): Promise<QuestionList> {
const data = await getGraphQLClient().request(QUERY, {
filters?: QueryVariables["filters"];
limit: number;
skip: number;
}): Promise<QuestionList> {
const data = await fetchGraphQL({
categorySlug,
filters,
limit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query fetchQuestionList(
query QuestionList(
$categorySlug: String!
$limit: Int
$skip: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// THIS FILE IS GENERATED! DO NOT MODIFY IT MANUALLY!!
// Instead, update the generation process or inputs and run `yarn codegen`.

import type { Simplify } from "type-fest";

import { getGraphQLClient } from "../../getGraphQLClient.ts";
import type * as Types from "../../graphqlTypes.generated.ts";

type RecentAcSubmissionListQueryVariables = Types.Exact<{
username: Types.Scalars["String"]["input"];
limit: Types.Scalars["Int"]["input"];
}>;

type RecentAcSubmissionListQuery = {
recentAcSubmissionList?: Array<{
id?: string | null;
title?: string | null;
titleSlug?: string | null;
timestamp?: string | null;
}> | null;
};

export type QueryVariables = Simplify<RecentAcSubmissionListQueryVariables>;
export type Query = Simplify<RecentAcSubmissionListQuery>;

export const QUERY =
"query RecentAcSubmissionList($username:String!,$limit:Int!){recentAcSubmissionList(username:$username,limit:$limit){id title titleSlug timestamp}}";

export function fetchGraphQL(variables: QueryVariables): Promise<Query> {
return getGraphQLClient().request(QUERY, variables);
}
Loading
Loading