From 130a4d01329476e8ed095a74f859a884a12e4af9 Mon Sep 17 00:00:00 2001 From: Miorel-Lucian Palii Date: Tue, 8 Oct 2024 21:13:54 -0700 Subject: [PATCH] Don't require a variables argument when no GraphQL variables --- .../fetchGraphQL.generated.ts | 6 ++---- .../api/active-daily-coding-challenge-question/main.ts | 3 +-- .../src/scripts/codegen/graphqlCodegenPlugin.ts | 10 +++++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/fetchGraphQL.generated.ts b/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/fetchGraphQL.generated.ts index 0d66dc63..46f49568 100644 --- a/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/fetchGraphQL.generated.ts +++ b/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/fetchGraphQL.generated.ts @@ -27,10 +27,8 @@ export const queryResultZodType = z.object({ export type QueryResult = z.infer; export type QueryVariables = OriginalQueryVariables; -export async function fetchGraphQL( - variables: QueryVariables, -): Promise { - const untrustedData = await getGraphQLClient().request(QUERY, variables); +export async function fetchGraphQL(): Promise { + const untrustedData = await getGraphQLClient().request(QUERY); // The type annotation serves as a TypeScript assert that the generated // Zod type is compatible with the types generated by GraphQL Codegen. diff --git a/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/main.ts b/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/main.ts index 3722026b..2e613bb5 100644 --- a/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/main.ts +++ b/workspaces/leetcode-api/src/api/active-daily-coding-challenge-question/main.ts @@ -29,8 +29,7 @@ export type ActiveDailyCodingChallengeQuestion = z.infer< >; export async function fetchActiveDailyCodingChallengeQuestionWithoutDateValidation(): Promise { - // TODO: have a way to omit variables when there aren't any - const { activeDailyCodingChallengeQuestion } = await fetchGraphQL({}); + const { activeDailyCodingChallengeQuestion } = await fetchGraphQL(); return activeDailyCodingChallengeQuestionZodType.parse( activeDailyCodingChallengeQuestion, diff --git a/workspaces/leetcode-api/src/scripts/codegen/graphqlCodegenPlugin.ts b/workspaces/leetcode-api/src/scripts/codegen/graphqlCodegenPlugin.ts index 07a8e75f..90cac6ef 100644 --- a/workspaces/leetcode-api/src/scripts/codegen/graphqlCodegenPlugin.ts +++ b/workspaces/leetcode-api/src/scripts/codegen/graphqlCodegenPlugin.ts @@ -6,7 +6,6 @@ import invariant from "invariant"; import nullthrows from "nullthrows"; 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"; @@ -28,12 +27,13 @@ export const plugin: PluginFunction<{}> = async function plugin( const { document, rawSDL: unminifiedGraphQL, location } = only(documents); const definition = only(nullthrows(document).definitions); invariant( - isObject(definition) && - definition.kind === "OperationDefinition" && + definition.kind === "OperationDefinition" && definition.operation === "query", "Expected a query!", ); + const hasVariables = (definition?.variableDefinitions ?? []).length > 0; + // TODO: should we call it a Zod schema instead? // Generate a Zod type to parse the result! @@ -86,8 +86,8 @@ export const plugin: PluginFunction<{}> = async function plugin( export type QueryResult = z.infer; export type QueryVariables = OriginalQueryVariables; - export async function fetchGraphQL(variables: QueryVariables): Promise { - const untrustedData = await getGraphQLClient().request(QUERY, variables); + export async function fetchGraphQL(${hasVariables ? "variables: QueryVariables" : ""}): Promise { + const untrustedData = await getGraphQLClient().request(QUERY${hasVariables ? ", variables" : ""}); // The type annotation serves as a TypeScript assert that the generated // Zod type is compatible with the types generated by GraphQL Codegen.