Skip to content

Commit 2ef312a

Browse files
committed
refactor: extract bulk operation logic into service class; create bulk-operations directory in services directory
1 parent e0c10b0 commit 2ef312a

File tree

5 files changed

+75
-54
lines changed

5 files changed

+75
-54
lines changed

graphql.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default {
8181
appDev: projectFactory('app-dev', 'app_dev_schema.graphql'),
8282
appManagement: projectFactory('app-management', 'app_management_schema.graphql'),
8383
admin: projectFactory('admin', 'admin_schema.graphql', 'cli-kit'),
84-
bulkOperations: projectFactory('bulk-operations', 'admin_schema.graphql'),
84+
bulkOperations: projectFactory('bulk-operations', '../../../cli-kit/src/cli/api/graphql/admin/admin_schema.graphql'),
8585
webhooks: projectFactory('webhooks', 'webhooks_schema.graphql'),
8686
functions: projectFactory('functions', 'functions_cli_schema.graphql', 'app'),
8787
},

packages/app/src/cli/commands/app/execute.ts

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import {appFlags, bulkOperationFlags} from '../../flags.js'
22
import AppLinkedCommand, {AppLinkedCommandOutput} from '../../utilities/app-linked-command.js'
33
import {linkedAppContext} from '../../services/app-context.js'
44
import {storeContext} from '../../services/store-context.js'
5-
import {runBulkOperationQuery} from '../../services/bulk-operation-run-query.js'
5+
import {executeBulkOperation} from '../../services/bulk-operations/execute-bulk-operation.js'
66
import {globalFlags} from '@shopify/cli-kit/node/cli'
7-
import {renderSuccess, renderInfo, renderWarning} from '@shopify/cli-kit/node/ui'
8-
import {outputContent, outputToken} from '@shopify/cli-kit/node/output'
97

108
export default class Execute extends AppLinkedCommand {
119
static summary = 'Execute bulk operations.'
@@ -36,54 +34,12 @@ export default class Execute extends AppLinkedCommand {
3634
forceReselectStore: flags.reset,
3735
})
3836

39-
renderInfo({
40-
headline: 'Starting bulk operation.',
41-
body: `App: ${appContextResult.app.name}\nStore: ${store.shopDomain}`,
42-
})
43-
44-
const bulkOperationResponse = await runBulkOperationQuery({
37+
await executeBulkOperation({
38+
app: appContextResult.app,
4539
storeFqdn: store.shopDomain,
4640
query: flags.query,
4741
})
4842

49-
if (bulkOperationResponse?.userErrors?.length) {
50-
const errorMessages = bulkOperationResponse.userErrors
51-
.map((error) => `${error.field?.join('.') ?? 'unknown'}: ${error.message}`)
52-
.join('\n')
53-
renderWarning({
54-
headline: 'Bulk operation errors.',
55-
body: errorMessages,
56-
})
57-
return {app: appContextResult.app}
58-
}
59-
60-
const result = bulkOperationResponse?.bulkOperation
61-
if (result) {
62-
const infoSections = [
63-
{
64-
title: 'Bulk Operation Created',
65-
body: [
66-
{
67-
list: {
68-
items: [
69-
outputContent`ID: ${outputToken.cyan(result.id)}`.value,
70-
outputContent`Status: ${outputToken.yellow(result.status)}`.value,
71-
outputContent`Created: ${outputToken.gray(String(result.createdAt))}`.value,
72-
],
73-
},
74-
},
75-
],
76-
},
77-
]
78-
79-
renderInfo({customSections: infoSections})
80-
81-
renderSuccess({
82-
headline: 'Bulk operation started successfully!',
83-
body: 'Congrats!',
84-
})
85-
}
86-
8743
return {app: appContextResult.app}
8844
}
8945
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {runBulkOperationQuery} from './run-query.js'
2+
import {AppLinkedInterface} from '../../models/app/app.js'
3+
import {renderSuccess, renderInfo, renderWarning} from '@shopify/cli-kit/node/ui'
4+
import {outputContent, outputToken} from '@shopify/cli-kit/node/output'
5+
6+
export interface ExecuteBulkOperationInput {
7+
app: AppLinkedInterface
8+
storeFqdn: string
9+
query: string
10+
}
11+
12+
/**
13+
* Orchestrates the execution of a bulk operation query.
14+
* Handles the API call, error handling, and user feedback.
15+
*/
16+
export async function executeBulkOperation(input: ExecuteBulkOperationInput): Promise<void> {
17+
const {app, storeFqdn, query} = input
18+
19+
renderInfo({
20+
headline: 'Starting bulk operation.',
21+
body: `App: ${app.name}\nStore: ${storeFqdn}`,
22+
})
23+
24+
const bulkOperationResponse = await runBulkOperationQuery({
25+
storeFqdn,
26+
query,
27+
})
28+
29+
if (bulkOperationResponse?.userErrors?.length) {
30+
const errorMessages = bulkOperationResponse.userErrors
31+
.map(
32+
(error: {field?: string[] | null; message: string}) =>
33+
`${error.field?.join('.') ?? 'unknown'}: ${error.message}`,
34+
)
35+
.join('\n')
36+
renderWarning({
37+
headline: 'Bulk operation errors.',
38+
body: errorMessages,
39+
})
40+
return
41+
}
42+
43+
const result = bulkOperationResponse?.bulkOperation
44+
if (result) {
45+
const infoSections = [
46+
{
47+
title: 'Bulk Operation Created',
48+
body: [
49+
{
50+
list: {
51+
items: [
52+
outputContent`ID: ${outputToken.cyan(result.id)}`.value,
53+
outputContent`Status: ${outputToken.yellow(result.status)}`.value,
54+
outputContent`Created: ${outputToken.gray(String(result.createdAt))}`.value,
55+
],
56+
},
57+
},
58+
],
59+
},
60+
]
61+
62+
renderInfo({customSections: infoSections})
63+
64+
renderSuccess({
65+
headline: 'Bulk operation started successfully!',
66+
body: 'Congrats!',
67+
})
68+
}
69+
}

packages/app/src/cli/services/bulk-operations-run-query.test.ts renamed to packages/app/src/cli/services/bulk-operations/run-query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {runBulkOperationQuery} from './bulk-operation-run-query.js'
1+
import {runBulkOperationQuery} from './run-query.js'
22
import {adminRequestDoc} from '@shopify/cli-kit/node/api/admin'
33
import {ensureAuthenticatedAdmin} from '@shopify/cli-kit/node/session'
44
import {describe, test, expect, vi, beforeEach} from 'vitest'

packages/app/src/cli/services/bulk-operation-run-query.ts renamed to packages/app/src/cli/services/bulk-operations/run-query.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
BulkOperationRunQuery,
33
BulkOperationRunQueryMutation,
4-
} from '../api/graphql/bulk-operations/generated/bulk-operation-run-query.js'
4+
} from '../../api/graphql/bulk-operations/generated/bulk-operation-run-query.js'
55
import {adminRequestDoc} from '@shopify/cli-kit/node/api/admin'
66
import {ensureAuthenticatedAdmin} from '@shopify/cli-kit/node/session'
77

@@ -10,10 +10,6 @@ interface BulkOperationRunQueryOptions {
1010
query: string
1111
}
1212

13-
/**
14-
* Executes a bulk operation query against the Shopify Admin API.
15-
* The operation runs asynchronously in the background.
16-
*/
1713
export async function runBulkOperationQuery(
1814
options: BulkOperationRunQueryOptions,
1915
): Promise<BulkOperationRunQueryMutation['bulkOperationRunQuery']> {

0 commit comments

Comments
 (0)