From cca0898fb59a07c4aefabce418957519841313b7 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 15 Aug 2026 00:18:42 +0000 Subject: [PATCH] refactor: extract parseGraphQLDocument helper in bulk operation helpers Extract duplicated GraphQL parsing and error handling logic from validateSingleOperation and isMutation into a private parseGraphQLDocument helper function. --- .../node/api/bulk-operations/helpers.ts | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/cli-kit/src/public/node/api/bulk-operations/helpers.ts b/packages/cli-kit/src/public/node/api/bulk-operations/helpers.ts index 6272ba8d786..8bfdf4078f3 100644 --- a/packages/cli-kit/src/public/node/api/bulk-operations/helpers.ts +++ b/packages/cli-kit/src/public/node/api/bulk-operations/helpers.ts @@ -36,22 +36,25 @@ export function extractBulkOperationId(gid: string): string { return match?.[1] ?? gid } -/** - * Validates that a GraphQL document contains exactly one operation definition. - * - * @param graphqlOperation - The GraphQL query or mutation string to validate. - * @throws AbortError if the document doesn't contain exactly one operation or has syntax errors. - */ -export function validateSingleOperation(graphqlOperation: string): void { - let document +function parseGraphQLDocument(graphqlOperation: string) { try { - document = parse(graphqlOperation) + return parse(graphqlOperation) } catch (error) { if (error instanceof Error) { throw new AbortError(`Invalid GraphQL syntax: ${error.message}`) } throw error } +} + +/** + * Validates that a GraphQL document contains exactly one operation definition. + * + * @param graphqlOperation - The GraphQL query or mutation string to validate. + * @throws AbortError if the document doesn't contain exactly one operation or has syntax errors. + */ +export function validateSingleOperation(graphqlOperation: string): void { + const document = parseGraphQLDocument(graphqlOperation) const operationDefinitions = document.definitions.filter((def) => def.kind === 'OperationDefinition') @@ -70,15 +73,7 @@ export function validateSingleOperation(graphqlOperation: string): void { * @throws AbortError if the operation has invalid GraphQL syntax. */ export function isMutation(graphqlOperation: string): boolean { - let document - try { - document = parse(graphqlOperation) - } catch (error) { - if (error instanceof Error) { - throw new AbortError(`Invalid GraphQL syntax: ${error.message}`) - } - throw error - } + const document = parseGraphQLDocument(graphqlOperation) const operationDefinition = document.definitions.find((def) => def.kind === 'OperationDefinition')