Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/cache-store-execute-api-versions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/store': patch
---

Cache the store's Admin API version list for a day, so `store execute` no longer sends a version lookup request on every call
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ describe('prepareAdminStoreGraphQLContext', () => {
expect(fetchPublicApiVersions).not.toHaveBeenCalled()
})

test('checks a fresh version list before rejecting a version missing from the cached one', async () => {
vi.mocked(fetchPublicApiVersions)
.mockResolvedValueOnce([{handle: '2025-10', supported: true}])
.mockResolvedValueOnce([
{handle: '2026-01', supported: true},
{handle: '2025-10', supported: true},
])

const result = await prepareAdminStoreGraphQLContext({store, userSpecifiedVersion: '2026-01'})

expect(result.version).toBe('2026-01')
expect(fetchPublicApiVersions).toHaveBeenLastCalledWith({
adminSession: {token: 'token', storeFqdn: store},
session: storedSession,
useCache: false,
})
})

test('throws when the requested API version is invalid', async () => {
await expect(prepareAdminStoreGraphQLContext({store, userSpecifiedVersion: '1999-01'})).rejects.toThrow(
'Invalid API version',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ async function resolveApiVersion(options: {
return supportedVersions.sort().reverse()[0]!
}

const versionList = availableVersions.map((version) => version.handle)
if (availableVersions.some((version) => version.handle === userSpecifiedVersion)) return userSpecifiedVersion

// The list may come from the cache, so check a fresh copy before rejecting a newly released version.
const freshVersions = await fetchPublicApiVersions({adminSession, session, useCache: false})
const versionList = freshVersions.map((version) => version.handle)
if (versionList.includes(userSpecifiedVersion)) return userSpecifiedVersion

throw new AbortError(`Invalid API version: ${userSpecifiedVersion}`, `Allowed versions: ${versionList.join(', ')}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ describe('fetchPublicApiVersions', () => {
)
})

test('caches the version list for a day', async () => {
vi.mocked(graphqlRequest).mockResolvedValue({publicApiVersions: [{handle: '2025-10', supported: true}]})

await fetchPublicApiVersions({adminSession, session})

expect(graphqlRequest).toHaveBeenCalledWith(expect.objectContaining({cacheOptions: {cacheTTL: {hours: 24}}}))
})

test('skips the cache when useCache is false', async () => {
vi.mocked(graphqlRequest).mockResolvedValue({publicApiVersions: [{handle: '2025-10', supported: true}]})

await fetchPublicApiVersions({adminSession, session, useCache: false})

expect(vi.mocked(graphqlRequest).mock.lastCall?.[0]).not.toHaveProperty('cacheOptions')
})

test('clears stored auth and prompts re-auth when the version request returns 401', async () => {
vi.mocked(graphqlRequest).mockRejectedValue(makeClientErrorLike(401, 'Unauthorized'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ const PUBLIC_API_VERSIONS_QUERY = `
* Runs the version-discovery GraphQL query against the Admin API. Errors are classified
* the same way as the execute-phase request: 401/404 trigger a stored-auth re-auth flow,
* 402 / fetch-aborts surface as user-facing `AbortError`s.
*
* The version list only changes when Shopify releases a new API version, so a successful
* response is cached per store and token for a day. Pass `useCache: false` to skip the cache.
*/
export async function fetchPublicApiVersions(input: {
adminSession: AdminSession
session: StoredStoreAppSession
useCache?: boolean
}): Promise<ApiVersion[]> {
try {
const response = await graphqlRequest<PublicApiVersionsResponse>({
Expand All @@ -51,6 +55,7 @@ export async function fetchPublicApiVersions(input: {
url: adminUrl(input.adminSession.storeFqdn, 'unstable', input.adminSession),
token: input.adminSession.token,
responseOptions: {handleErrors: false},
...(input.useCache === false ? {} : {cacheOptions: {cacheTTL: {hours: 24}}}),
})
return response.publicApiVersions
} catch (error) {
Expand Down
Loading