From cf3caf4c1865a3e12a5ef6a312f1cec08a895a51 Mon Sep 17 00:00:00 2001 From: Eric Lee Date: Wed, 21 Jan 2026 15:00:11 -0800 Subject: [PATCH] fix-error-message-with-empty-query --- .../api/graphql/admin/generated/types.d.ts | 2 -- .../bulk-operations/generated/types.d.ts | 2 -- .../utilities/execute-command-helpers.test.ts | 30 +++++++++++++++++++ .../cli/utilities/execute-command-helpers.ts | 12 +++++++- .../api/graphql/admin/generated/types.d.ts | 2 -- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/app/src/cli/api/graphql/admin/generated/types.d.ts b/packages/app/src/cli/api/graphql/admin/generated/types.d.ts index a15582d7847..98ff1c678d8 100644 --- a/packages/app/src/cli/api/graphql/admin/generated/types.d.ts +++ b/packages/app/src/cli/api/graphql/admin/generated/types.d.ts @@ -89,8 +89,6 @@ export type Scalars = { JSON: {input: JsonMapType | string; output: JsonMapType} /** A monetary value string without a currency symbol or code. Example value: `"100.57"`. */ Money: {input: any; output: any} - /** A scalar value. */ - Scalar: {input: any; output: any} /** * Represents a unique identifier in the Storefront API. A `StorefrontID` value can * be used wherever an ID is expected in the Storefront API. diff --git a/packages/app/src/cli/api/graphql/bulk-operations/generated/types.d.ts b/packages/app/src/cli/api/graphql/bulk-operations/generated/types.d.ts index f52c0a5a9a9..c68fb733477 100644 --- a/packages/app/src/cli/api/graphql/bulk-operations/generated/types.d.ts +++ b/packages/app/src/cli/api/graphql/bulk-operations/generated/types.d.ts @@ -89,8 +89,6 @@ export type Scalars = { JSON: {input: JsonMapType | string; output: JsonMapType} /** A monetary value string without a currency symbol or code. Example value: `"100.57"`. */ Money: {input: any; output: any} - /** A scalar value. */ - Scalar: {input: any; output: any} /** * Represents a unique identifier in the Storefront API. A `StorefrontID` value can * be used wherever an ID is expected in the Storefront API. diff --git a/packages/app/src/cli/utilities/execute-command-helpers.test.ts b/packages/app/src/cli/utilities/execute-command-helpers.test.ts index f3d0f685562..5d31edc2378 100644 --- a/packages/app/src/cli/utilities/execute-command-helpers.test.ts +++ b/packages/app/src/cli/utilities/execute-command-helpers.test.ts @@ -130,6 +130,18 @@ describe('prepareExecuteContext', () => { await expect(prepareExecuteContext(flagsWithoutQuery)).rejects.toThrow('exactlyOne constraint') }) + test('throws AbortError when query flag is empty string', async () => { + const flagsWithEmptyQuery = {...mockFlags, query: ''} + + await expect(prepareExecuteContext(flagsWithEmptyQuery)).rejects.toThrow('--query flag value is empty') + }) + + test('throws AbortError when query flag contains only whitespace', async () => { + const flagsWithWhitespaceQuery = {...mockFlags, query: ' \n\t '} + + await expect(prepareExecuteContext(flagsWithWhitespaceQuery)).rejects.toThrow('--query flag value is empty') + }) + test('returns query, app context, and store', async () => { const result = await prepareExecuteContext(mockFlags) @@ -169,6 +181,24 @@ describe('prepareExecuteContext', () => { expect(readFile).not.toHaveBeenCalled() }) + test('throws AbortError when query file is empty', async () => { + vi.mocked(fileExists).mockResolvedValue(true) + vi.mocked(readFile).mockResolvedValue('' as any) + + const flagsWithQueryFile = {...mockFlags, query: undefined, 'query-file': '/path/to/empty.graphql'} + + await expect(prepareExecuteContext(flagsWithQueryFile)).rejects.toThrow('is empty') + }) + + test('throws AbortError when query file contains only whitespace', async () => { + vi.mocked(fileExists).mockResolvedValue(true) + vi.mocked(readFile).mockResolvedValue(' \n\t ' as any) + + const flagsWithQueryFile = {...mockFlags, query: undefined, 'query-file': '/path/to/whitespace.graphql'} + + await expect(prepareExecuteContext(flagsWithQueryFile)).rejects.toThrow('is empty') + }) + test('validates GraphQL query using validateSingleOperation', async () => { await prepareExecuteContext(mockFlags) diff --git a/packages/app/src/cli/utilities/execute-command-helpers.ts b/packages/app/src/cli/utilities/execute-command-helpers.ts index 4e5f2f4a00e..a4602022b60 100644 --- a/packages/app/src/cli/utilities/execute-command-helpers.ts +++ b/packages/app/src/cli/utilities/execute-command-helpers.ts @@ -63,7 +63,10 @@ export async function prepareAppStoreContext(flags: AppStoreContextFlags): Promi export async function prepareExecuteContext(flags: ExecuteCommandFlags): Promise { let query: string | undefined - if (flags.query) { + if (flags.query !== undefined) { + if (!flags.query.trim()) { + throw new AbortError('The --query flag value is empty. Please provide a valid GraphQL query or mutation.') + } query = flags.query } else if (flags['query-file']) { const queryFile = flags['query-file'] @@ -73,6 +76,13 @@ export async function prepareExecuteContext(flags: ExecuteCommandFlags): Promise ) } query = await readFile(queryFile, {encoding: 'utf8'}) + if (!query.trim()) { + throw new AbortError( + outputContent`Query file at ${outputToken.path( + queryFile, + )} is empty. Please provide a valid GraphQL query or mutation.`, + ) + } } if (!query) { diff --git a/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts b/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts index da424d13e6f..3ecac10dacd 100644 --- a/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts +++ b/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts @@ -89,8 +89,6 @@ export type Scalars = { JSON: {input: JsonMapType | string; output: JsonMapType} /** A monetary value string without a currency symbol or code. Example value: `"100.57"`. */ Money: {input: any; output: any} - /** A scalar value. */ - Scalar: {input: any; output: any} /** * Represents a unique identifier in the Storefront API. A `StorefrontID` value can * be used wherever an ID is expected in the Storefront API.