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
1 change: 1 addition & 0 deletions workspaces/leetcode-api/.gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
schema.graphql linguist-generated
*.generated.ts linguist-generated

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

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

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

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
Expand Up @@ -6,15 +6,19 @@ import { z } from "zod";

import { isObject } from "@code-chronicles/util/isObject";
import { only } from "@code-chronicles/util/only";
import { spliceString } from "@code-chronicles/util/spliceString";

const operationNameZodType = z
.object({ value: z.string() })
.transform(({ value }) => value);
const nonNegativeIntZodType = z.number().int().nonnegative();

const operationNameZodType = z.object({
value: z.string(),
loc: z.object({ start: nonNegativeIntZodType, end: nonNegativeIntZodType }),
});

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

const minifiedGraphql = graphqlQueryCompress(nullthrows(unminifiedGraphql));
const operationName = operationNameZodType.parse(definition.name);
// Extract the operation name from the definition, as well as information
// about its location in the raw, unminified GraphQL.
const {
value: operationName,
loc: { start: operationNameStart, end: operationNameEnd },
} = operationNameZodType.parse(definition.name);
const operationNameLength = operationNameEnd - operationNameStart;
invariant(
operationName.length === operationNameLength,
"Operation name length mismatch!",
);

// TODO: strip out the operation name from the minified GraphQL
// Minify the GraphQL we use for the query.
const minifiedGraphQL = graphqlQueryCompress(
spliceString(
nullthrows(unminifiedGraphQL),
operationNameStart,
operationNameLength,
),
);

return {
prepend: [
Expand All @@ -42,7 +62,7 @@ export const plugin: PluginFunction<{}> = function plugin(_schema, documents) {
export type QueryVariables = Simplify<${operationName}QueryVariables>;
export type Query = Simplify<${operationName}Query>;

export const QUERY = ${JSON.stringify(minifiedGraphql)};
export const QUERY = ${JSON.stringify(minifiedGraphQL)};

export function fetchGraphQL(variables: QueryVariables): Promise<Query> {
return getGraphQLClient().request(QUERY, variables);
Expand Down
10 changes: 10 additions & 0 deletions workspaces/util/src/spliceString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function spliceString(
s: string,
start: number,
deleteCount: number = Infinity,
...items: unknown[]
): string {
const chars: unknown[] = [...s];
chars.splice(start, deleteCount, ...items);
return chars.join("");
}
Loading