Skip to content

Commit

Permalink
Check organization beta flags for gated template specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
amcaplan committed Aug 6, 2024
1 parent c0762fc commit ee1f787
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import {
AppVersionByIdQueryVariables,
AppModule as AppModuleReturnType,
} from './app-management-client/graphql/app-version-by-id.js'
import {
OrganizationBetaFlagsQuerySchema,
OrganizationBetaFlagsQueryVariables,
organizationBetaFlagsQuery,
} from './app-management-client/graphql/organization_beta_flags.js'
import {RemoteSpecification} from '../../api/graphql/extension_specifications.js'
import {
DeveloperPlatformClient,
Expand Down Expand Up @@ -127,6 +132,7 @@ import {appManagementRequest} from '@shopify/cli-kit/node/api/app-management'
import {appDevRequest} from '@shopify/cli-kit/node/api/app-dev'
import {
businessPlatformOrganizationsRequest,
businessPlatformOrganizationsRequestDoc,
businessPlatformRequest,
businessPlatformRequestDoc,
} from '@shopify/cli-kit/node/api/business-platform'
Expand Down Expand Up @@ -381,7 +387,7 @@ export class AppManagementClient implements DeveloperPlatformClient {
// partners-client and app-management-client. Since we need transferDisabled and convertableToPartnerTest values
// from the Partners OrganizationStore schema, we will return this type for now
async devStoresForOrg(orgId: string): Promise<OrganizationStore[]> {
const storesResult = await businessPlatformOrganizationsRequest<ListAppDevStoresQuery>(
const storesResult = await businessPlatformOrganizationsRequestDoc<ListAppDevStoresQuery>(
ListAppDevStores,
await this.businessPlatformToken(),
orgId,
Expand Down Expand Up @@ -731,7 +737,7 @@ export class AppManagementClient implements DeveloperPlatformClient {
// from the Partners FindByStoreDomainSchema, we will return this type for now
async storeByDomain(orgId: string, shopDomain: string): Promise<FindStoreByDomainSchema> {
const queryVariables: FetchDevStoreByDomainQueryVariables = {domain: shopDomain}
const storesResult = await businessPlatformOrganizationsRequest(
const storesResult = await businessPlatformOrganizationsRequestDoc(
FetchDevStoreByDomain,
await this.businessPlatformToken(),
orgId,
Expand Down Expand Up @@ -865,15 +871,21 @@ export class AppManagementClient implements DeveloperPlatformClient {
}

private async organizationBetaFlags(
_organizationId: string,
organizationId: string,
allBetaFlags: string[],
): Promise<{[key: string]: boolean}> {
// For now, stub everything as false
const stub: {[flag: string]: boolean} = {}
): Promise<{[flag: (typeof allBetaFlags)[number]]: boolean}> {
const variables: OrganizationBetaFlagsQueryVariables = {organizationId: encodedGidFromId(organizationId)}
const flagsResult = await businessPlatformOrganizationsRequest<OrganizationBetaFlagsQuerySchema>(
organizationBetaFlagsQuery(allBetaFlags),
await this.businessPlatformToken(),
organizationId,
variables,
)
const result: {[flag: (typeof allBetaFlags)[number]]: boolean} = {}
allBetaFlags.forEach((flag) => {
stub[flag] = false
result[flag] = Boolean(flagsResult.organization[`flag_${flag}`])
})
return stub
return result
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {gql} from 'graphql-request'

export function organizationBetaFlagsQuery(flags: string[]): string {
return gql`
query OrganizationBetaFlags($organizationId: OrganizationID!) {
organization(organizationId: $organizationId) {
id
${flags.map((flag) => `flag_${flag}: hasFeatureFlag(handle: "${flag}")`).join('\n ')}
}
}
`
}

export interface OrganizationBetaFlagsQueryVariables {
organizationId: string
}

export interface OrganizationBetaFlagsQuerySchema {
organization: {
id: string
[flag: `flag_${string}`]: boolean
}
}
50 changes: 43 additions & 7 deletions packages/cli-kit/src/public/node/api/business-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,65 @@ export async function businessPlatformRequestDoc<TResult, TVariables extends Var
})
}

/**
* Sets up the request to the Business Platform Organizations API.
*
* @param token - Business Platform token.
* @param organizationId - Organization ID as a numeric (non-GID) value.
*/
async function setupOrganizationsRequest(token: string, organizationId: string) {
const api = 'BusinessPlatform'
const fqdn = await businessPlatformFqdn()
const url = `https://${fqdn}/organizations/api/unstable/organization/${organizationId}/graphql`
return {
token,
api,
url,
responseOptions: {onResponse: handleDeprecations},
}
}

/**
* Executes a GraphQL query against the Business Platform Organizations API.
*
* @param query - GraphQL query to execute.
* @param token - Business Platform token.
* @param organizationId - Organization ID as a numeric (non-GID) value.
* @param variables - GraphQL variables to pass to the query.
* @returns The response of the query of generic type <T>.
*/
export async function businessPlatformOrganizationsRequest<T>(
query: string,
token: string,
organizationId: string,
variables?: GraphQLVariables,
): Promise<T> {
return graphqlRequest<T>({
query,
...(await setupOrganizationsRequest(token, organizationId)),
variables,
responseOptions: {onResponse: handleDeprecations},
})
}

/**
* Executes a GraphQL query against the Business Platform Organizations API. Uses typed documents.
*
* @param query - GraphQL query to execute.
* @param token - Business Platform token.
* @param organizationId - Organization ID as a numeric value.
* @param variables - GraphQL variables to pass to the query.
* @returns The response of the query of generic type <T>.
*/
export async function businessPlatformOrganizationsRequest<TResult>(
export async function businessPlatformOrganizationsRequestDoc<TResult>(
query: TypedDocumentNode<TResult, GraphQLVariables> | TypedDocumentNode<TResult, Exact<{[key: string]: never}>>,
token: string,
organizationId: string,
variables?: GraphQLVariables,
): Promise<TResult> {
const api = 'BusinessPlatform'
const fqdn = await businessPlatformFqdn()
const url = `https://${fqdn}/organizations/api/unstable/organization/${organizationId}/graphql`
return graphqlRequestDoc({
query,
api,
url,
token,
...(await setupOrganizationsRequest(token, organizationId)),
variables,
responseOptions: {onResponse: handleDeprecations},
})
Expand Down

0 comments on commit ee1f787

Please sign in to comment.