11import { appFlags } from '../../flags.js'
22import AppLinkedCommand , { AppLinkedCommandOutput } from '../../utilities/app-linked-command.js'
33import { linkedAppContext } from '../../services/app-context.js'
4+ import { storeContext } from '../../services/store-context.js'
5+ import { runBulkOperationQuery } from '../../services/bulk-operations.js'
6+ import { Flags } from '@oclif/core'
47import { globalFlags } from '@shopify/cli-kit/node/cli'
5- import { renderSuccess } from '@shopify/cli-kit/node/ui'
8+ import { renderSuccess , renderInfo , renderWarning } from '@shopify/cli-kit/node/ui'
9+ import { normalizeStoreFqdn } from '@shopify/cli-kit/node/context/fqdn'
10+ import { outputContent , outputToken } from '@shopify/cli-kit/node/output'
611
712export default class Execute extends AppLinkedCommand {
813 static summary = 'Execute bulk operations.'
@@ -14,6 +19,18 @@ export default class Execute extends AppLinkedCommand {
1419 static flags = {
1520 ...globalFlags ,
1621 ...appFlags ,
22+ query : Flags . string ( {
23+ char : 'q' ,
24+ description : 'The GraphQL query, as a string.' ,
25+ env : 'SHOPIFY_FLAG_QUERY' ,
26+ required : true ,
27+ } ) ,
28+ store : Flags . string ( {
29+ char : 's' ,
30+ description : 'Store URL. Must be an existing development or Shopify Plus sandbox store.' ,
31+ env : 'SHOPIFY_FLAG_STORE' ,
32+ parse : async ( input ) => normalizeStoreFqdn ( input ) ,
33+ } ) ,
1734 }
1835
1936 async run ( ) : Promise < AppLinkedCommandOutput > {
@@ -26,11 +43,57 @@ export default class Execute extends AppLinkedCommand {
2643 userProvidedConfigName : flags . config ,
2744 } )
2845
29- renderSuccess ( {
30- headline : 'App context loaded successfully!' ,
31- body : `App: ${ appContextResult . app . name } \nOrganization: ${ appContextResult . organization . businessName } ` ,
46+ const store = await storeContext ( {
47+ appContextResult,
48+ storeFqdn : flags . store ,
49+ forceReselectStore : flags . reset ,
3250 } )
3351
52+ renderInfo ( {
53+ headline : 'Starting bulk operation.' ,
54+ body : `App: ${ appContextResult . app . name } \nStore: ${ store . shopDomain } ` ,
55+ } )
56+
57+ const { result, errors} = await runBulkOperationQuery ( {
58+ storeFqdn : store . shopDomain ,
59+ query : flags . query ,
60+ } )
61+
62+ if ( errors && errors . length > 0 ) {
63+ const errorMessages = errors . map ( ( error ) => `${ error . field ?. join ( '.' ) ?? 'unknown' } : ${ error . message } ` ) . join ( '\n' )
64+ renderWarning ( {
65+ headline : 'Bulk operation errors.' ,
66+ body : errorMessages ,
67+ } )
68+ return { app : appContextResult . app }
69+ }
70+
71+ if ( result ) {
72+ const infoSections = [
73+ {
74+ title : 'Bulk Operation Created' ,
75+ body : [
76+ {
77+ list : {
78+ items : [
79+ outputContent `ID: ${ outputToken . cyan ( result . id ) } ` . value ,
80+ outputContent `Status: ${ outputToken . yellow ( result . status ) } ` . value ,
81+ outputContent `Created: ${ outputToken . gray ( result . createdAt ) } ` . value ,
82+ ] ,
83+ } ,
84+ } ,
85+ ] ,
86+ } ,
87+ ]
88+
89+ renderInfo ( { customSections : infoSections } )
90+
91+ renderSuccess ( {
92+ headline : 'Bulk operation started successfully!' ,
93+ body : 'Congrats!' ,
94+ } )
95+ }
96+
3497 return { app : appContextResult . app }
3598 }
3699}
0 commit comments