1+ import { parseGraphQLOperation } from '../../services/graphql-parser.js'
2+ import { runBulkQuery } from '../../services/bulk-operations.js'
13import { Command , Flags } from '@oclif/core'
24import { globalFlags } from '@shopify/cli-kit/node/cli'
3- import { readFile } from '@shopify/cli-kit/node/fs'
5+ import { readFile , writeFile } from '@shopify/cli-kit/node/fs'
46import { ensureAuthenticatedAdmin } from '@shopify/cli-kit/node/session'
57import { adminRequest } from '@shopify/cli-kit/node/api/admin'
6- import { parseGraphQLOperation } from '../../services/graphql-parser.js'
7- import { runBulkQuery } from '../../services/bulk-operations.js'
8+ import { outputInfo , outputSuccess } from '@shopify/cli-kit/node/output'
89
910export default class Execute extends Command {
1011 static summary = 'execute a graphql query or mutation on a store'
1112
12- static description = 'executes a graphql query or mutation on the specified store, and writes the result to stdout or a file. supports bulk operations.'
13+ static description =
14+ 'executes a graphql query or mutation on the specified store, and writes the result to stdout or a file. supports bulk operations.'
1315
1416 static flags = {
1517 ...globalFlags ,
1618 query : Flags . string ( {
1719 char : 'q' ,
1820 description : 'the graphql query or mutation, as a string' ,
1921 exclusive : [ 'query-file' ] ,
22+ env : 'SHOPIFY_FLAG_QUERY' ,
2023 } ) ,
2124 'query-file' : Flags . string ( {
2225 description : 'a file containing the graphql query or mutation' ,
2326 exclusive : [ 'query' ] ,
27+ env : 'SHOPIFY_FLAG_QUERY_FILE' ,
2428 } ) ,
2529 store : Flags . string ( {
2630 char : 's' ,
2731 description : 'the myshopify.com domain of the store' ,
32+ env : 'SHOPIFY_FLAG_STORE' ,
2833 } ) ,
2934 variables : Flags . string ( {
3035 char : 'v' ,
3136 description : 'the values for graphql variables, in json format' ,
3237 multiple : true ,
3338 exclusive : [ 'variable-file' ] ,
39+ env : 'SHOPIFY_FLAG_VARIABLES' ,
3440 } ) ,
3541 'variable-file' : Flags . string ( {
3642 description : 'a file containing graphql variables, in jsonl format' ,
3743 exclusive : [ 'variables' ] ,
44+ env : 'SHOPIFY_FLAG_VARIABLE_FILE' ,
3845 } ) ,
3946 'output-file' : Flags . string ( {
4047 description : 'the file name where results should be written' ,
48+ env : 'SHOPIFY_FLAG_OUTPUT_FILE' ,
4149 } ) ,
4250 'bulk-operation' : Flags . boolean ( {
4351 description : 'execute as a bulk operation' ,
4452 default : false ,
53+ env : 'SHOPIFY_FLAG_BULK_OPERATION' ,
4554 } ) ,
4655 }
4756
@@ -63,15 +72,17 @@ export default class Execute extends Command {
6372 this . error ( '--store is required' )
6473 }
6574
66- let variables : Record < string , unknown > | undefined
75+ let variables : { [ key : string ] : unknown } | undefined
6776
6877 if ( flags . variables && flags . variables . length > 0 ) {
6978 const variableString = flags . variables [ 0 ]
7079 if ( variableString ) {
7180 try {
7281 variables = JSON . parse ( variableString )
82+ /* eslint-disable-next-line no-catch-all/no-catch-all */
7383 } catch ( error ) {
74- this . error ( `invalid json in --variables: ${ error } ` )
84+ const message = error instanceof Error ? error . message : String ( error )
85+ this . error ( `invalid json in --variables: ${ message } ` )
7586 }
7687 }
7788 } else if ( flags [ 'variable-file' ] ) {
@@ -80,8 +91,10 @@ export default class Execute extends Command {
8091 if ( firstLine ) {
8192 try {
8293 variables = JSON . parse ( firstLine )
94+ /* eslint-disable-next-line no-catch-all/no-catch-all */
8395 } catch ( error ) {
84- this . error ( `invalid json in --variable-file: ${ error } ` )
96+ const message = error instanceof Error ? error . message : String ( error )
97+ this . error ( `invalid json in --variable-file: ${ message } ` )
8598 }
8699 }
87100 }
@@ -92,9 +105,19 @@ export default class Execute extends Command {
92105 const operationType = parseGraphQLOperation ( query )
93106
94107 if ( operationType === 'query' ) {
95- this . log ( 'starting bulk query...' )
96- const results = await runBulkQuery ( query , adminSession )
97- this . log ( results )
108+ const result = await runBulkQuery ( query , adminSession , ( status , objectCount , rate , spinner ) => {
109+ const rateStr = rate > 0 ? ` • ${ Math . round ( rate ) } obj/sec` : ''
110+ process . stdout . write ( `\r\x1b[K${ status . toLowerCase ( ) } : ${ objectCount } objects${ rateStr } ${ spinner } ` )
111+ } )
112+
113+ const outputFilePath = 'bulk-operation-results.jsonl'
114+ await writeFile ( outputFilePath , result . content )
115+
116+ this . log ( '\n' )
117+ outputSuccess ( `wrote ${ result . totalObjects } objects to ${ outputFilePath } ` )
118+ outputInfo (
119+ `completed in ${ result . totalTimeSeconds . toFixed ( 1 ) } s (${ Math . round ( result . averageRate ) } obj/sec average)` ,
120+ )
98121 } else {
99122 this . error ( 'bulk mutations not yet implemented' )
100123 }
0 commit comments