diff --git a/.changeset/cache-store-execute-api-versions.md b/.changeset/cache-store-execute-api-versions.md new file mode 100644 index 00000000000..f03dc5d7582 --- /dev/null +++ b/.changeset/cache-store-execute-api-versions.md @@ -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 diff --git a/packages/store/src/cli/services/store/execute/admin-context.test.ts b/packages/store/src/cli/services/store/execute/admin-context.test.ts index 0d6d3d42597..a001f0c628f 100644 --- a/packages/store/src/cli/services/store/execute/admin-context.test.ts +++ b/packages/store/src/cli/services/store/execute/admin-context.test.ts @@ -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', diff --git a/packages/store/src/cli/services/store/execute/admin-context.ts b/packages/store/src/cli/services/store/execute/admin-context.ts index 475ef51bcdd..635d5ff3e60 100644 --- a/packages/store/src/cli/services/store/execute/admin-context.ts +++ b/packages/store/src/cli/services/store/execute/admin-context.ts @@ -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(', ')}`) diff --git a/packages/store/src/cli/services/store/execute/admin-transport.test.ts b/packages/store/src/cli/services/store/execute/admin-transport.test.ts index 4990a9085ff..4ad75a25de0 100644 --- a/packages/store/src/cli/services/store/execute/admin-transport.test.ts +++ b/packages/store/src/cli/services/store/execute/admin-transport.test.ts @@ -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')) diff --git a/packages/store/src/cli/services/store/execute/admin-transport.ts b/packages/store/src/cli/services/store/execute/admin-transport.ts index df976d0f5f3..37e76272ca1 100644 --- a/packages/store/src/cli/services/store/execute/admin-transport.ts +++ b/packages/store/src/cli/services/store/execute/admin-transport.ts @@ -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 { try { const response = await graphqlRequest({ @@ -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) {