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 16, 2024
1 parent a1a337c commit a274728
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/cli-kit/src/public/node/api/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {PublicApiVersions} from '../../../cli/api/graphql/admin/generated/public
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 @@ -40,8 +42,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 opts = {
Expand All @@ -61,7 +63,9 @@ export async function adminRequestDoc<TResult, TVariables extends Variables>(
*/
async function fetchLatestSupportedApiVersion(session: AdminSession): Promise<string> {
const apiVersions = await supportedApiVersions(session)
return apiVersions.reverse()[0]!
const latest = apiVersions.reverse()[0]!
LatestApiVersionByFQDN.set(session.storeFqdn, latest)
return latest
}

/**
Expand Down

0 comments on commit a274728

Please sign in to comment.