Skip to content

Commit 59197af

Browse files
committed
Separate GraphQL patching into separate, visitor-based script
1 parent 86b42e0 commit 59197af

File tree

20 files changed

+472
-264
lines changed

20 files changed

+472
-264
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
diff --git a/language/printer.js b/language/printer.js
2+
index eed3b7cf24d74d744c860ad9cb8e546ed6f4afd7..4754051981ec40f41bce700b96dc569492cf4b41 100644
3+
--- a/language/printer.js
4+
+++ b/language/printer.js
5+
@@ -148,7 +148,7 @@ const printDocASTReducer = {
6+
// Type System Definitions
7+
SchemaDefinition: {
8+
leave: ({ description, directives, operationTypes }) =>
9+
- wrap('', description, '\n') +
10+
+ wrap('\n', description, '\n') +
11+
join(['schema', join(directives, ' '), block(operationTypes)], ' '),
12+
},
13+
OperationTypeDefinition: {
14+
@@ -156,12 +156,12 @@ const printDocASTReducer = {
15+
},
16+
ScalarTypeDefinition: {
17+
leave: ({ description, name, directives }) =>
18+
- wrap('', description, '\n') +
19+
+ wrap('\n', description, '\n') +
20+
join(['scalar', name, join(directives, ' ')], ' '),
21+
},
22+
ObjectTypeDefinition: {
23+
leave: ({ description, name, interfaces, directives, fields }) =>
24+
- wrap('', description, '\n') +
25+
+ wrap('\n', description, '\n') +
26+
join(
27+
[
28+
'type',
29+
@@ -175,7 +175,7 @@ const printDocASTReducer = {
30+
},
31+
FieldDefinition: {
32+
leave: ({ description, name, arguments: args, type, directives }) =>
33+
- wrap('', description, '\n') +
34+
+ wrap('\n', description, '\n') +
35+
name +
36+
(hasMultilineItems(args)
37+
? wrap('(\n', indent(join(args, '\n')), '\n)')
38+
@@ -186,7 +186,7 @@ const printDocASTReducer = {
39+
},
40+
InputValueDefinition: {
41+
leave: ({ description, name, type, defaultValue, directives }) =>
42+
- wrap('', description, '\n') +
43+
+ wrap('\n', description, '\n') +
44+
join(
45+
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
46+
' ',
47+
@@ -194,7 +194,7 @@ const printDocASTReducer = {
48+
},
49+
InterfaceTypeDefinition: {
50+
leave: ({ description, name, interfaces, directives, fields }) =>
51+
- wrap('', description, '\n') +
52+
+ wrap('\n', description, '\n') +
53+
join(
54+
[
55+
'interface',
56+
@@ -208,7 +208,7 @@ const printDocASTReducer = {
57+
},
58+
UnionTypeDefinition: {
59+
leave: ({ description, name, directives, types }) =>
60+
- wrap('', description, '\n') +
61+
+ wrap('\n', description, '\n') +
62+
join(
63+
['union', name, join(directives, ' '), wrap('= ', join(types, ' | '))],
64+
' ',
65+
@@ -216,21 +216,21 @@ const printDocASTReducer = {
66+
},
67+
EnumTypeDefinition: {
68+
leave: ({ description, name, directives, values }) =>
69+
- wrap('', description, '\n') +
70+
+ wrap('\n', description, '\n') +
71+
join(['enum', name, join(directives, ' '), block(values)], ' '),
72+
},
73+
EnumValueDefinition: {
74+
leave: ({ description, name, directives }) =>
75+
- wrap('', description, '\n') + join([name, join(directives, ' ')], ' '),
76+
+ wrap('\n', description, '\n') + join([name, join(directives, ' ')], ' '),
77+
},
78+
InputObjectTypeDefinition: {
79+
leave: ({ description, name, directives, fields }) =>
80+
- wrap('', description, '\n') +
81+
+ wrap('\n', description, '\n') +
82+
join(['input', name, join(directives, ' '), block(fields)], ' '),
83+
},
84+
DirectiveDefinition: {
85+
leave: ({ description, name, arguments: args, repeatable, locations }) =>
86+
- wrap('', description, '\n') +
87+
+ wrap('\n', description, '\n') +
88+
'directive @' +
89+
name +
90+
(hasMultilineItems(args)
91+
diff --git a/language/printer.mjs b/language/printer.mjs
92+
index cffc2bf05b7440abe0f6cb92163092666127f182..3cfbdda9f3b0fc3076c287740e4dfa32577895c9 100644
93+
--- a/language/printer.mjs
94+
+++ b/language/printer.mjs
95+
@@ -136,7 +136,7 @@ const printDocASTReducer = {
96+
// Type System Definitions
97+
SchemaDefinition: {
98+
leave: ({ description, directives, operationTypes }) =>
99+
- wrap('', description, '\n') +
100+
+ wrap('\n', description, '\n') +
101+
join(['schema', join(directives, ' '), block(operationTypes)], ' '),
102+
},
103+
OperationTypeDefinition: {
104+
@@ -144,12 +144,12 @@ const printDocASTReducer = {
105+
},
106+
ScalarTypeDefinition: {
107+
leave: ({ description, name, directives }) =>
108+
- wrap('', description, '\n') +
109+
+ wrap('\n', description, '\n') +
110+
join(['scalar', name, join(directives, ' ')], ' '),
111+
},
112+
ObjectTypeDefinition: {
113+
leave: ({ description, name, interfaces, directives, fields }) =>
114+
- wrap('', description, '\n') +
115+
+ wrap('\n', description, '\n') +
116+
join(
117+
[
118+
'type',
119+
@@ -163,7 +163,7 @@ const printDocASTReducer = {
120+
},
121+
FieldDefinition: {
122+
leave: ({ description, name, arguments: args, type, directives }) =>
123+
- wrap('', description, '\n') +
124+
+ wrap('\n', description, '\n') +
125+
name +
126+
(hasMultilineItems(args)
127+
? wrap('(\n', indent(join(args, '\n')), '\n)')
128+
@@ -174,7 +174,7 @@ const printDocASTReducer = {
129+
},
130+
InputValueDefinition: {
131+
leave: ({ description, name, type, defaultValue, directives }) =>
132+
- wrap('', description, '\n') +
133+
+ wrap('\n', description, '\n') +
134+
join(
135+
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
136+
' ',
137+
@@ -182,7 +182,7 @@ const printDocASTReducer = {
138+
},
139+
InterfaceTypeDefinition: {
140+
leave: ({ description, name, interfaces, directives, fields }) =>
141+
- wrap('', description, '\n') +
142+
+ wrap('\n', description, '\n') +
143+
join(
144+
[
145+
'interface',
146+
@@ -196,7 +196,7 @@ const printDocASTReducer = {
147+
},
148+
UnionTypeDefinition: {
149+
leave: ({ description, name, directives, types }) =>
150+
- wrap('', description, '\n') +
151+
+ wrap('\n', description, '\n') +
152+
join(
153+
['union', name, join(directives, ' '), wrap('= ', join(types, ' | '))],
154+
' ',
155+
@@ -204,21 +204,21 @@ const printDocASTReducer = {
156+
},
157+
EnumTypeDefinition: {
158+
leave: ({ description, name, directives, values }) =>
159+
- wrap('', description, '\n') +
160+
+ wrap('\n', description, '\n') +
161+
join(['enum', name, join(directives, ' '), block(values)], ' '),
162+
},
163+
EnumValueDefinition: {
164+
leave: ({ description, name, directives }) =>
165+
- wrap('', description, '\n') + join([name, join(directives, ' ')], ' '),
166+
+ wrap('\n', description, '\n') + join([name, join(directives, ' ')], ' '),
167+
},
168+
InputObjectTypeDefinition: {
169+
leave: ({ description, name, directives, fields }) =>
170+
- wrap('', description, '\n') +
171+
+ wrap('\n', description, '\n') +
172+
join(['input', name, join(directives, ' '), block(fields)], ' '),
173+
},
174+
DirectiveDefinition: {
175+
leave: ({ description, name, arguments: args, repeatable, locations }) =>
176+
- wrap('', description, '\n') +
177+
+ wrap('\n', description, '\n') +
178+
'directive @' +
179+
name +
180+
(hasMultilineItems(args)

workspaces/leetcode-api/graphql-codegen.config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import dedent from "dedent";
2+
13
import type { CodegenConfig } from "@graphql-codegen/cli";
24
import type { Types as GraphQLCodegen } from "@graphql-codegen/plugin-helpers";
35

4-
import { SCHEMA_PATCHED_FILE } from "./src/scripts/scrape-graphql-schema/constants.ts";
6+
import { SCHEMA_FILE_PATCHED } from "./src/scripts/patch-graphql-schema/constants.ts";
57

68
const commonTypeScriptPluginConfig: GraphQLCodegen.PluginConfig = {
79
arrayInputCoercion: false,
@@ -38,16 +40,17 @@ const commonTypeScriptPluginConfig: GraphQLCodegen.PluginConfig = {
3840

3941
const headerPlugin: GraphQLCodegen.OutputConfig = {
4042
add: {
41-
content: `
43+
content:
44+
dedent`
4245
// THIS FILE IS GENERATED! DO NOT MODIFY IT MANUALLY!!
4346
// Instead, update the generation process or inputs and run \`yarn codegen\`.
44-
`,
47+
` + "\n\n",
4548
placement: "prepend",
4649
},
4750
};
4851

4952
const config: CodegenConfig = {
50-
schema: SCHEMA_PATCHED_FILE,
53+
schema: SCHEMA_FILE_PATCHED,
5154
documents: ["src/api/**/query.graphql"],
5255
overwrite: true,
5356
emitLegacyCommonJSImports: false,

workspaces/leetcode-api/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
"codegen": "cross-env NODE_OPTIONS=\"--import tsx\" graphql-codegen-esm --config graphql-codegen.config.ts",
2222
"format": "prettier --color --write .",
2323
"lint": "eslint --color --max-warnings=0 .",
24+
"patch-graphql-schema": "tsx src/scripts/patch-graphql-schema/main.ts",
2425
"scrape-graphql-schema": "tsx src/scripts/scrape-graphql-schema/main.ts",
2526
"test": "tsx ./jest.config.ts",
2627
"typecheck": "tsc --pretty --project ."
2728
},
2829
"dependencies": {
2930
"@code-chronicles/util": "workspace:*",
30-
"graphql": "16.9.0",
31+
"graphql": "patch:graphql@npm%3A16.9.0#~/.yarn/patches/graphql-npm-16.9.0-a36f71845f.patch",
3132
"graphql-request": "7.1.0",
3233
"invariant": "2.2.4",
3334
"nullthrows": "patch:nullthrows@npm%3A1.1.1#~/.yarn/patches/nullthrows-npm-1.1.1-3d1f817134.patch",
@@ -40,6 +41,7 @@
4041
"@graphql-codegen/near-operation-file-preset": "3.0.0",
4142
"@types/node": "22.7.5",
4243
"cross-env": "7.0.3",
44+
"dedent": "1.5.3",
4345
"eslint": "9.12.0",
4446
"graphql-query-compress": "1.2.4",
4547
"immutability-helper": "patch:immutability-helper@npm%3A3.1.1#~/.yarn/patches/immutability-helper-npm-3.1.1-482f1f8f58.patch",

workspaces/leetcode-api/schema-patched.graphql

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/__tests__/validSchema-test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import { readFile } from "node:fs/promises";
33
import { describe, expect, it } from "@jest/globals";
44
import { buildSchema, validateSchema } from "graphql";
55

6-
import {
7-
SCHEMA_ORIGINAL_FILE,
8-
SCHEMA_PATCHED_FILE,
9-
} from "../scripts/scrape-graphql-schema/constants.ts";
6+
import { SCHEMA_FILE_ORIGINAL } from "../scripts/scrape-graphql-schema/constants.ts";
7+
import { SCHEMA_FILE_PATCHED } from "../scripts/patch-graphql-schema/constants.ts";
108

119
describe("GraphQL schema", () => {
12-
it.each([SCHEMA_ORIGINAL_FILE, SCHEMA_PATCHED_FILE])(
10+
it.each([SCHEMA_FILE_ORIGINAL, SCHEMA_FILE_PATCHED])(
1311
"validates %s",
1412
async (schemaPath) => {
1513
const schema = buildSchema(await readFile(schemaPath, "utf8"));

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

Lines changed: 16 additions & 13 deletions
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: 21 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)