Skip to content

Commit b7caf67

Browse files
committed
PR feedback: Added an options type for resolveApiVersion
1 parent d2e908f commit b7caf67

File tree

8 files changed

+60
-34
lines changed

8 files changed

+60
-34
lines changed

packages/app/src/cli/services/bulk-operations/bulk-operation-status.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,10 @@ describe('getBulkOperationStatus', () => {
184184

185185
await getBulkOperationStatus({organization: mockOrganization, storeFqdn, operationId, remoteApp})
186186

187-
expect(resolveApiVersion).toHaveBeenCalledWith(
188-
{token: 'test-token', storeFqdn},
189-
undefined,
190-
BULK_OPERATIONS_MIN_API_VERSION,
191-
)
187+
expect(resolveApiVersion).toHaveBeenCalledWith({
188+
adminSession: {token: 'test-token', storeFqdn},
189+
minimumDefaultVersion: BULK_OPERATIONS_MIN_API_VERSION,
190+
})
192191
})
193192

194193
test('uses resolved API version in admin request', async () => {
@@ -369,11 +368,10 @@ describe('listBulkOperations', () => {
369368

370369
await listBulkOperations({organization: mockOrganization, storeFqdn, remoteApp})
371370

372-
expect(resolveApiVersion).toHaveBeenCalledWith(
373-
{token: 'test-token', storeFqdn},
374-
undefined,
375-
BULK_OPERATIONS_MIN_API_VERSION,
376-
)
371+
expect(resolveApiVersion).toHaveBeenCalledWith({
372+
adminSession: {token: 'test-token', storeFqdn},
373+
minimumDefaultVersion: BULK_OPERATIONS_MIN_API_VERSION,
374+
})
377375
})
378376

379377
test('uses resolved API version in admin request', async () => {

packages/app/src/cli/services/bulk-operations/bulk-operation-status.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ export async function getBulkOperationStatus(options: GetBulkOperationStatusOpti
7777
query: GetBulkOperationById,
7878
session: adminSession,
7979
variables: {id: operationId},
80-
version: await resolveApiVersion(adminSession, undefined, BULK_OPERATIONS_MIN_API_VERSION),
80+
version: await resolveApiVersion({
81+
adminSession,
82+
minimumDefaultVersion: BULK_OPERATIONS_MIN_API_VERSION,
83+
}),
8184
})
8285

8386
if (response.bulkOperation) {
@@ -120,7 +123,10 @@ export async function listBulkOperations(options: ListBulkOperationsOptions): Pr
120123
sortKey: 'CREATED_AT',
121124
reverse: true,
122125
},
123-
version: await resolveApiVersion(adminSession, undefined, BULK_OPERATIONS_MIN_API_VERSION),
126+
version: await resolveApiVersion({
127+
adminSession,
128+
minimumDefaultVersion: BULK_OPERATIONS_MIN_API_VERSION,
129+
}),
124130
})
125131

126132
const operations = response.bulkOperations.nodes.map((operation) => ({

packages/app/src/cli/services/bulk-operations/execute-bulk-operation.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,11 @@ describe('executeBulkOperation', () => {
692692
query,
693693
})
694694

695-
expect(resolveApiVersion).toHaveBeenCalledWith(mockAdminSession, undefined, BULK_OPERATIONS_MIN_API_VERSION)
695+
expect(resolveApiVersion).toHaveBeenCalledWith({
696+
adminSession: mockAdminSession,
697+
userSpecifiedVersion: undefined,
698+
minimumDefaultVersion: BULK_OPERATIONS_MIN_API_VERSION,
699+
})
696700
})
697701

698702
test('uses resolved API version when running bulk operation', async () => {

packages/app/src/cli/services/bulk-operations/execute-bulk-operation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ export async function executeBulkOperation(input: ExecuteBulkOperationInput): Pr
5858
variableFile,
5959
outputFile,
6060
watch = false,
61-
version: versionFlag,
61+
version: userSpecifiedVersion,
6262
} = input
6363

6464
const adminSession = await createAdminSessionAsApp(remoteApp, storeFqdn)
6565

66-
const version = await resolveApiVersion(adminSession, versionFlag, BULK_OPERATIONS_MIN_API_VERSION)
66+
const version = await resolveApiVersion({
67+
adminSession,
68+
userSpecifiedVersion,
69+
minimumDefaultVersion: BULK_OPERATIONS_MIN_API_VERSION,
70+
})
6771

6872
const variablesJsonl = await parseVariablesToJsonl(variables, variableFile)
6973

packages/app/src/cli/services/execute-operation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('executeOperation', () => {
5555
})
5656

5757
expect(createAdminSessionAsApp).toHaveBeenCalledWith(mockRemoteApp, storeFqdn)
58-
expect(resolveApiVersion).toHaveBeenCalledWith(mockAdminSession, undefined)
58+
expect(resolveApiVersion).toHaveBeenCalledWith({adminSession: mockAdminSession})
5959
expect(adminRequestDoc).toHaveBeenCalledWith({
6060
// parsed GraphQL document
6161
query: expect.any(Object),
@@ -119,7 +119,7 @@ describe('executeOperation', () => {
119119
version,
120120
})
121121

122-
expect(resolveApiVersion).toHaveBeenCalledWith(mockAdminSession, version)
122+
expect(resolveApiVersion).toHaveBeenCalledWith({adminSession: mockAdminSession, userSpecifiedVersion: version})
123123
expect(adminRequestDoc).toHaveBeenCalledWith(
124124
expect.objectContaining({
125125
version,

packages/app/src/cli/services/execute-operation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ async function parseVariables(variables?: string): Promise<{[key: string]: unkno
3838
}
3939

4040
export async function executeOperation(input: ExecuteOperationInput): Promise<void> {
41-
const {organization, remoteApp, storeFqdn, query, variables, version: versionFlag, outputFile} = input
41+
const {organization, remoteApp, storeFqdn, query, variables, version: userSpecifiedVersion, outputFile} = input
4242

4343
const adminSession = await createAdminSessionAsApp(remoteApp, storeFqdn)
4444

45-
const version = await resolveApiVersion(adminSession, versionFlag)
45+
const version = await resolveApiVersion({adminSession, userSpecifiedVersion})
4646

4747
renderInfo({
4848
headline: 'Executing GraphQL operation.',

packages/app/src/cli/services/graphql/common.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('resolveApiVersion', () => {
110110
const mockAdminSession = {token: 'test-token', storeFqdn: 'test-store.myshopify.com'}
111111

112112
test('returns unstable version without validation', async () => {
113-
const result = await resolveApiVersion(mockAdminSession, 'unstable')
113+
const result = await resolveApiVersion({adminSession: mockAdminSession, userSpecifiedVersion: 'unstable'})
114114

115115
expect(result).toBe('unstable')
116116
expect(fetchApiVersions).not.toHaveBeenCalled()
@@ -123,7 +123,11 @@ describe('resolveApiVersion', () => {
123123
{handle: '2024-07', supported: true},
124124
])
125125

126-
const result = await resolveApiVersion(mockAdminSession, '2024-04', '2026-01')
126+
const result = await resolveApiVersion({
127+
adminSession: mockAdminSession,
128+
userSpecifiedVersion: '2024-04',
129+
minimumDefaultVersion: '2026-01',
130+
})
127131

128132
expect(result).toBe('2024-04')
129133
expect(fetchApiVersions).toHaveBeenCalledWith(mockAdminSession)
@@ -136,7 +140,7 @@ describe('resolveApiVersion', () => {
136140
{handle: '2024-07', supported: true},
137141
])
138142

139-
const result = await resolveApiVersion(mockAdminSession, '2024-04')
143+
const result = await resolveApiVersion({adminSession: mockAdminSession, userSpecifiedVersion: '2024-04'})
140144

141145
expect(result).toBe('2024-04')
142146
expect(fetchApiVersions).toHaveBeenCalledWith(mockAdminSession)
@@ -149,7 +153,9 @@ describe('resolveApiVersion', () => {
149153
{handle: '2024-07', supported: true},
150154
])
151155

152-
await expect(resolveApiVersion(mockAdminSession, '2023-01')).rejects.toThrow('Invalid API version: 2023-01')
156+
await expect(resolveApiVersion({adminSession: mockAdminSession, userSpecifiedVersion: '2023-01'})).rejects.toThrow(
157+
'Invalid API version: 2023-01',
158+
)
153159
expect(fetchApiVersions).toHaveBeenCalledWith(mockAdminSession)
154160
})
155161

@@ -161,7 +167,7 @@ describe('resolveApiVersion', () => {
161167
{handle: '2025-01', supported: false},
162168
])
163169

164-
const result = await resolveApiVersion(mockAdminSession)
170+
const result = await resolveApiVersion({adminSession: mockAdminSession})
165171

166172
expect(result).toBe('2024-07')
167173
expect(fetchApiVersions).toHaveBeenCalledWith(mockAdminSession)
@@ -175,7 +181,7 @@ describe('resolveApiVersion', () => {
175181
{handle: '2025-10', supported: true},
176182
])
177183

178-
const result = await resolveApiVersion(mockAdminSession, undefined, '2026-01')
184+
const result = await resolveApiVersion({adminSession: mockAdminSession, minimumDefaultVersion: '2026-01'})
179185

180186
expect(result).toBe('2026-01')
181187
expect(fetchApiVersions).toHaveBeenCalledWith(mockAdminSession)
@@ -189,7 +195,7 @@ describe('resolveApiVersion', () => {
189195
{handle: '2027-01', supported: false},
190196
])
191197

192-
const result = await resolveApiVersion(mockAdminSession, undefined, '2026-01')
198+
const result = await resolveApiVersion({adminSession: mockAdminSession, minimumDefaultVersion: '2026-01'})
193199

194200
expect(result).toBe('2026-07')
195201
expect(fetchApiVersions).toHaveBeenCalledWith(mockAdminSession)

packages/app/src/cli/services/graphql/common.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,28 @@ export function validateSingleOperation(graphqlOperation: string): void {
4545
}
4646
}
4747

48+
/**
49+
* Options for resolving an API version.
50+
*/
51+
export interface ResolveApiVersionOptions {
52+
/** Admin session containing store credentials. */
53+
adminSession: {token: string; storeFqdn: string}
54+
/** The API version specified by the user. */
55+
userSpecifiedVersion?: string
56+
/** Optional minimum version to use as a fallback when no version is specified. */
57+
minimumDefaultVersion?: string
58+
}
59+
4860
/**
4961
* Determines the API version to use based on the user provided version and the available versions.
5062
* The 'unstable' version is always allowed without validation.
5163
*
52-
* @param adminSession - Admin session containing store credentials.
53-
* @param userSpecifiedVersion - The API version to validate.
54-
* @param minimumDefaultVersion - Optional minimum version to use as a fallback when no version is specified.
64+
* @param options - Options for resolving the API version.
5565
* @throws AbortError if the provided version is not allowed.
5666
*/
57-
export async function resolveApiVersion(
58-
adminSession: {token: string; storeFqdn: string},
59-
userSpecifiedVersion?: string,
60-
minimumDefaultVersion?: string,
61-
): Promise<string> {
67+
export async function resolveApiVersion(options: ResolveApiVersionOptions): Promise<string> {
68+
const {adminSession, userSpecifiedVersion, minimumDefaultVersion} = options
69+
6270
if (userSpecifiedVersion === 'unstable') return userSpecifiedVersion
6371

6472
const availableVersions = await fetchApiVersions(adminSession)

0 commit comments

Comments
 (0)