Skip to content

Commit

Permalink
Cache latest API versions by fqdn
Browse files Browse the repository at this point in the history
  • Loading branch information
catlee committed Oct 23, 2024
1 parent 564255b commit a067333
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/cli-kit/src/public/node/api/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {normalizeStoreFqdn} from '../context/fqdn.js'
import {ClientError, Variables} from 'graphql-request'
import {TypedDocumentNode} from '@graphql-typed-document-node/core'

const LatestApiVersionByFQDN = new Map<string, string>()

/**
* Executes a GraphQL query against the Admin API.
*
Expand Down Expand Up @@ -42,8 +44,8 @@ export async function adminRequestDoc<TResult, TVariables extends Variables>(
version?: string,
responseOptions?: GraphQLResponseOptions<TResult>,
): Promise<TResult> {
let apiVersion = version
if (!version) {
let apiVersion = version || LatestApiVersionByFQDN.get(session.storeFqdn)
if (!apiVersion) {
apiVersion = await fetchLatestSupportedApiVersion(session)
}
const store = await normalizeStoreFqdn(session.storeFqdn)
Expand All @@ -64,8 +66,9 @@ export async function adminRequestDoc<TResult, TVariables extends Variables>(
*/
async function fetchLatestSupportedApiVersion(session: AdminSession): Promise<string> {
const apiVersions = await supportedApiVersions(session)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return apiVersions.reverse()[0]!
const latest = apiVersions.reverse()[0]!
LatestApiVersionByFQDN.set(session.storeFqdn, latest)
return latest
}

/**
Expand Down

0 comments on commit a067333

Please sign in to comment.