Skip to content

Commit cf3caf4

Browse files
committed
fix-error-message-with-empty-query
1 parent 0b5db7e commit cf3caf4

5 files changed

Lines changed: 41 additions & 7 deletions

File tree

packages/app/src/cli/api/graphql/admin/generated/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ export type Scalars = {
8989
JSON: {input: JsonMapType | string; output: JsonMapType}
9090
/** A monetary value string without a currency symbol or code. Example value: `"100.57"`. */
9191
Money: {input: any; output: any}
92-
/** A scalar value. */
93-
Scalar: {input: any; output: any}
9492
/**
9593
* Represents a unique identifier in the Storefront API. A `StorefrontID` value can
9694
* be used wherever an ID is expected in the Storefront API.

packages/app/src/cli/api/graphql/bulk-operations/generated/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ export type Scalars = {
8989
JSON: {input: JsonMapType | string; output: JsonMapType}
9090
/** A monetary value string without a currency symbol or code. Example value: `"100.57"`. */
9191
Money: {input: any; output: any}
92-
/** A scalar value. */
93-
Scalar: {input: any; output: any}
9492
/**
9593
* Represents a unique identifier in the Storefront API. A `StorefrontID` value can
9694
* be used wherever an ID is expected in the Storefront API.

packages/app/src/cli/utilities/execute-command-helpers.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ describe('prepareExecuteContext', () => {
130130
await expect(prepareExecuteContext(flagsWithoutQuery)).rejects.toThrow('exactlyOne constraint')
131131
})
132132

133+
test('throws AbortError when query flag is empty string', async () => {
134+
const flagsWithEmptyQuery = {...mockFlags, query: ''}
135+
136+
await expect(prepareExecuteContext(flagsWithEmptyQuery)).rejects.toThrow('--query flag value is empty')
137+
})
138+
139+
test('throws AbortError when query flag contains only whitespace', async () => {
140+
const flagsWithWhitespaceQuery = {...mockFlags, query: ' \n\t '}
141+
142+
await expect(prepareExecuteContext(flagsWithWhitespaceQuery)).rejects.toThrow('--query flag value is empty')
143+
})
144+
133145
test('returns query, app context, and store', async () => {
134146
const result = await prepareExecuteContext(mockFlags)
135147

@@ -169,6 +181,24 @@ describe('prepareExecuteContext', () => {
169181
expect(readFile).not.toHaveBeenCalled()
170182
})
171183

184+
test('throws AbortError when query file is empty', async () => {
185+
vi.mocked(fileExists).mockResolvedValue(true)
186+
vi.mocked(readFile).mockResolvedValue('' as any)
187+
188+
const flagsWithQueryFile = {...mockFlags, query: undefined, 'query-file': '/path/to/empty.graphql'}
189+
190+
await expect(prepareExecuteContext(flagsWithQueryFile)).rejects.toThrow('is empty')
191+
})
192+
193+
test('throws AbortError when query file contains only whitespace', async () => {
194+
vi.mocked(fileExists).mockResolvedValue(true)
195+
vi.mocked(readFile).mockResolvedValue(' \n\t ' as any)
196+
197+
const flagsWithQueryFile = {...mockFlags, query: undefined, 'query-file': '/path/to/whitespace.graphql'}
198+
199+
await expect(prepareExecuteContext(flagsWithQueryFile)).rejects.toThrow('is empty')
200+
})
201+
172202
test('validates GraphQL query using validateSingleOperation', async () => {
173203
await prepareExecuteContext(mockFlags)
174204

packages/app/src/cli/utilities/execute-command-helpers.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export async function prepareAppStoreContext(flags: AppStoreContextFlags): Promi
6363
export async function prepareExecuteContext(flags: ExecuteCommandFlags): Promise<ExecuteContext> {
6464
let query: string | undefined
6565

66-
if (flags.query) {
66+
if (flags.query !== undefined) {
67+
if (!flags.query.trim()) {
68+
throw new AbortError('The --query flag value is empty. Please provide a valid GraphQL query or mutation.')
69+
}
6770
query = flags.query
6871
} else if (flags['query-file']) {
6972
const queryFile = flags['query-file']
@@ -73,6 +76,13 @@ export async function prepareExecuteContext(flags: ExecuteCommandFlags): Promise
7376
)
7477
}
7578
query = await readFile(queryFile, {encoding: 'utf8'})
79+
if (!query.trim()) {
80+
throw new AbortError(
81+
outputContent`Query file at ${outputToken.path(
82+
queryFile,
83+
)} is empty. Please provide a valid GraphQL query or mutation.`,
84+
)
85+
}
7686
}
7787

7888
if (!query) {

packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ export type Scalars = {
8989
JSON: {input: JsonMapType | string; output: JsonMapType}
9090
/** A monetary value string without a currency symbol or code. Example value: `"100.57"`. */
9191
Money: {input: any; output: any}
92-
/** A scalar value. */
93-
Scalar: {input: any; output: any}
9492
/**
9593
* Represents a unique identifier in the Storefront API. A `StorefrontID` value can
9694
* be used wherever an ID is expected in the Storefront API.

0 commit comments

Comments
 (0)