Skip to content

Commit 4922dca

Browse files
committed
Exclude operation name from generated GraphQL query constants
1 parent 1810b6f commit 4922dca

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
schema.graphql linguist-generated
2+
*.generated.ts linguist-generated

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/api/topic/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/graphqlCodegenPlugin.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ import { z } from "zod";
66

77
import { isObject } from "@code-chronicles/util/isObject";
88
import { only } from "@code-chronicles/util/only";
9+
import { spliceString } from "@code-chronicles/util/spliceString";
910

10-
const operationNameZodType = z
11-
.object({ value: z.string() })
12-
.transform(({ value }) => value);
11+
const nonNegativeIntZodType = z.number().int().nonnegative();
12+
13+
const operationNameZodType = z.object({
14+
value: z.string(),
15+
loc: z.object({ start: nonNegativeIntZodType, end: nonNegativeIntZodType }),
16+
});
1317

1418
export const plugin: PluginFunction<{}> = function plugin(_schema, documents) {
1519
// Encode some assumptions as invariants, namely that there is a single query
1620
// operation in the file.
17-
const { document, rawSDL: unminifiedGraphql } = only(documents);
21+
const { document, rawSDL: unminifiedGraphQL } = only(documents);
1822
const definition = only(nullthrows(document).definitions);
1923
invariant(
2024
isObject(definition) &&
@@ -23,10 +27,26 @@ export const plugin: PluginFunction<{}> = function plugin(_schema, documents) {
2327
"Expected a query!",
2428
);
2529

26-
const minifiedGraphql = graphqlQueryCompress(nullthrows(unminifiedGraphql));
27-
const operationName = operationNameZodType.parse(definition.name);
30+
// Extract the operation name from the definition, as well as information
31+
// about its location in the raw, unminified GraphQL.
32+
const {
33+
value: operationName,
34+
loc: { start: operationNameStart, end: operationNameEnd },
35+
} = operationNameZodType.parse(definition.name);
36+
const operationNameLength = operationNameEnd - operationNameStart;
37+
invariant(
38+
operationName.length === operationNameLength,
39+
"Operation name length mismatch!",
40+
);
2841

29-
// TODO: strip out the operation name from the minified GraphQL
42+
// Minify the GraphQL we use for the query.
43+
const minifiedGraphQL = graphqlQueryCompress(
44+
spliceString(
45+
nullthrows(unminifiedGraphQL),
46+
operationNameStart,
47+
operationNameLength,
48+
),
49+
);
3050

3151
return {
3252
prepend: [
@@ -42,7 +62,7 @@ export const plugin: PluginFunction<{}> = function plugin(_schema, documents) {
4262
export type QueryVariables = Simplify<${operationName}QueryVariables>;
4363
export type Query = Simplify<${operationName}Query>;
4464
45-
export const QUERY = ${JSON.stringify(minifiedGraphql)};
65+
export const QUERY = ${JSON.stringify(minifiedGraphQL)};
4666
4767
export function fetchGraphQL(variables: QueryVariables): Promise<Query> {
4868
return getGraphQLClient().request(QUERY, variables);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function spliceString(
2+
s: string,
3+
start: number,
4+
deleteCount: number = Infinity,
5+
...items: unknown[]
6+
): string {
7+
const chars: unknown[] = [...s];
8+
chars.splice(start, deleteCount, ...items);
9+
return chars.join("");
10+
}

0 commit comments

Comments
 (0)