Skip to content
Draft
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions packages/app/src/cli/api/graphql/bulk_operation_run_query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {gql} from 'graphql-request'

// eslint-disable-next-line @shopify/cli/no-inline-graphql
export const BulkOperationRunQuery = gql`
mutation BulkOperationRunQuery($query: String!) {
bulkOperationRunQuery(query: $query) {
bulkOperation {
id
status
errorCode
createdAt
objectCount
fileSize
url
}
userErrors {
field
message
}
}
}
`

export interface BulkOperationRunQuerySchema {
bulkOperationRunQuery: {
bulkOperation: {
id: string
status: string
errorCode: string | null
createdAt: string
objectCount: string
fileSize: string
url: string | null
} | null
userErrors: {
field: string[] | null
message: string
}[]
}
}
94 changes: 82 additions & 12 deletions packages/app/src/cli/commands/app/execute.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,99 @@
import {appFlags} from '../../flags.js'
import AppUnlinkedCommand, {AppUnlinkedCommandOutput} from '../../utilities/app-unlinked-command.js'
import {AppInterface} from '../../models/app/app.js'
import AppLinkedCommand, {AppLinkedCommandOutput} from '../../utilities/app-linked-command.js'
import {linkedAppContext} from '../../services/app-context.js'
import {storeContext} from '../../services/store-context.js'
import {runBulkOperationQuery} from '../../services/bulk-operations.js'
import {Flags} from '@oclif/core'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import {renderSuccess} from '@shopify/cli-kit/node/ui'
import {renderSuccess, renderInfo, renderWarning} from '@shopify/cli-kit/node/ui'
import {normalizeStoreFqdn} from '@shopify/cli-kit/node/context/fqdn'
import {outputContent, outputToken} from '@shopify/cli-kit/node/output'

export default class Execute extends AppUnlinkedCommand {
static summary = 'Execute app operations.'
export default class Execute extends AppLinkedCommand {
static summary = 'Execute bulk operations.'

static description = 'Execute app operations.'
static description = 'Execute bulk operations against the Shopify Admin API.'

static hidden = true

static flags = {
...globalFlags,
...appFlags,
query: Flags.string({
char: 'q',
description: 'The GraphQL query, as a string.',
env: 'SHOPIFY_FLAG_QUERY',
required: true,
}),
store: Flags.string({
char: 's',
description: 'Store URL. Must be an existing development or Shopify Plus sandbox store.',
env: 'SHOPIFY_FLAG_STORE',
parse: async (input) => normalizeStoreFqdn(input),
}),
}

async run(): Promise<AppUnlinkedCommandOutput> {
await this.parse(Execute)
async run(): Promise<AppLinkedCommandOutput> {
const {flags} = await this.parse(Execute)

renderSuccess({
headline: 'Execute command ran successfully!',
body: 'Placeholder command. Add execution logic here.',
const appContextResult = await linkedAppContext({
directory: flags.path,
clientId: flags['client-id'],
forceRelink: flags.reset,
userProvidedConfigName: flags.config,
})

return {app: undefined as unknown as AppInterface}
const store = await storeContext({
appContextResult,
storeFqdn: flags.store,
forceReselectStore: flags.reset,
})

renderInfo({
headline: 'Starting bulk operation.',
body: `App: ${appContextResult.app.name}\nStore: ${store.shopDomain}`,
})

const {result, errors} = await runBulkOperationQuery({
storeFqdn: store.shopDomain,
query: flags.query,
})

if (errors && errors.length > 0) {
const errorMessages = errors.map((error) => `${error.field?.join('.') ?? 'unknown'}: ${error.message}`).join('\n')
renderWarning({
headline: 'Bulk operation errors.',
body: errorMessages,
})
return {app: appContextResult.app}
}

if (result) {
const infoSections = [
{
title: 'Bulk Operation Created',
body: [
{
list: {
items: [
outputContent`ID: ${outputToken.cyan(result.id)}`.value,
outputContent`Status: ${outputToken.yellow(result.status)}`.value,
outputContent`Created: ${outputToken.gray(result.createdAt)}`.value,
],
},
},
],
},
]

renderInfo({customSections: infoSections})

renderSuccess({
headline: 'Bulk operation started successfully!',
body: 'Congrats!',
})
}

return {app: appContextResult.app}
}
}
38 changes: 38 additions & 0 deletions packages/app/src/cli/services/bulk-operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {BulkOperationRunQuery, BulkOperationRunQuerySchema} from '../api/graphql/bulk_operation_run_query.js'
import {adminRequest} from '@shopify/cli-kit/node/api/admin'
import {AdminSession, ensureAuthenticatedAdmin} from '@shopify/cli-kit/node/session'

export interface BulkOperationRunQueryOptions {

Check failure on line 5 in packages/app/src/cli/services/bulk-operations.ts

View workflow job for this annotation

GitHub Actions / knip-reporter-annotations-check

packages/app/src/cli/services/bulk-operations.ts#L5

'BulkOperationRunQueryOptions' is an unused type
storeFqdn: string
query: string
}

export type BulkOperationResult = NonNullable<BulkOperationRunQuerySchema['bulkOperationRunQuery']['bulkOperation']>

Check failure on line 10 in packages/app/src/cli/services/bulk-operations.ts

View workflow job for this annotation

GitHub Actions / knip-reporter-annotations-check

packages/app/src/cli/services/bulk-operations.ts#L10

'BulkOperationResult' is an unused type
export type BulkOperationError = BulkOperationRunQuerySchema['bulkOperationRunQuery']['userErrors'][number]

Check failure on line 11 in packages/app/src/cli/services/bulk-operations.ts

View workflow job for this annotation

GitHub Actions / knip-reporter-annotations-check

packages/app/src/cli/services/bulk-operations.ts#L11

'BulkOperationError' is an unused type

/**
* Executes a bulk operation query against the Shopify Admin API.
* The operation runs asynchronously in the background.
*/
export async function runBulkOperationQuery(
options: BulkOperationRunQueryOptions,
): Promise<{result?: BulkOperationResult; errors?: BulkOperationError[]}> {
const {storeFqdn, query} = options
const adminSession: AdminSession = await ensureAuthenticatedAdmin(storeFqdn)
const response: BulkOperationRunQuerySchema = await adminRequest(BulkOperationRunQuery, adminSession, {query})

if (response.bulkOperationRunQuery.userErrors.length > 0) {
return {
errors: response.bulkOperationRunQuery.userErrors,
}
}

const bulkOperation = response.bulkOperationRunQuery.bulkOperation
if (bulkOperation) {
return {result: bulkOperation}
}

return {
errors: [{field: null, message: 'No bulk operation was created'}],
}
}
Loading
Loading