Skip to content
Draft
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
31 changes: 13 additions & 18 deletions packages/cli-kit/src/public/node/api/bulk-operations/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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')

Expand Down
Loading