fix(billing): fix analytics GraphQL query and support billing-disabled mode

- Add argument name to @Args decorator for BillingAnalyticsInput
- Wrap query variables in 'input' object to match GraphQL schema
- Fall back to 30-day default period when billing is disabled
- Update generated GraphQL types for BillingAnalyticsInput

https://claude.ai/code/session_01Y1EqrX6PFq3EJxJq89h7DF
This commit is contained in:
Claude
2026-03-13 08:28:43 +00:00
parent 2b61fa5717
commit 664e37717c
4 changed files with 35 additions and 27 deletions
@@ -6241,10 +6241,14 @@ export type GetMeteredProductsUsageQueryVariables = Exact<{ [key: string]: never
export type GetMeteredProductsUsageQuery = { __typename?: 'Query', getMeteredProductsUsage: Array<{ __typename?: 'BillingMeteredProductUsage', productKey: BillingProductKey, usedCredits: number, grantedCredits: number, rolloverCredits: number, totalGrantedCredits: number, unitPriceCents: number }> };
export type GetBillingAnalyticsQueryVariables = Exact<{
export type BillingAnalyticsInput = {
periodStart?: InputMaybe<Scalars['DateTime']>;
periodEnd?: InputMaybe<Scalars['DateTime']>;
userWorkspaceId?: InputMaybe<Scalars['String']>;
};
export type GetBillingAnalyticsQueryVariables = Exact<{
input?: InputMaybe<BillingAnalyticsInput>;
}>;
@@ -11031,8 +11035,8 @@ export type GetMeteredProductsUsageQueryHookResult = ReturnType<typeof useGetMet
export type GetMeteredProductsUsageLazyQueryHookResult = ReturnType<typeof useGetMeteredProductsUsageLazyQuery>;
export type GetMeteredProductsUsageQueryResult = Apollo.QueryResult<GetMeteredProductsUsageQuery, GetMeteredProductsUsageQueryVariables>;
export const GetBillingAnalyticsDocument = gql`
query GetBillingAnalytics($periodStart: DateTime, $periodEnd: DateTime, $userWorkspaceId: String) {
getBillingAnalytics(periodStart: $periodStart, periodEnd: $periodEnd, userWorkspaceId: $userWorkspaceId) {
query GetBillingAnalytics($input: BillingAnalyticsInput) {
getBillingAnalytics(input: $input) {
usageByUser {
key
creditsUsed
@@ -185,11 +185,13 @@ export const SettingsBillingAnalyticsSection = () => {
const { data, loading } = useGetBillingAnalyticsQuery({
variables: {
...(periodDates.periodStart && {
periodStart: periodDates.periodStart,
}),
...(periodDates.periodEnd && { periodEnd: periodDates.periodEnd }),
...(selectedUserId && { userWorkspaceId: selectedUserId }),
input: {
...(periodDates.periodStart && {
periodStart: periodDates.periodStart,
}),
...(periodDates.periodEnd && { periodEnd: periodDates.periodEnd }),
...(selectedUserId && { userWorkspaceId: selectedUserId }),
},
},
});
@@ -1,16 +1,8 @@
import { gql } from '@apollo/client';
export const GET_BILLING_ANALYTICS = gql`
query GetBillingAnalytics(
$periodStart: DateTime
$periodEnd: DateTime
$userWorkspaceId: String
) {
getBillingAnalytics(
periodStart: $periodStart
periodEnd: $periodEnd
userWorkspaceId: $userWorkspaceId
) {
query GetBillingAnalytics($input: BillingAnalyticsInput) {
getBillingAnalytics(input: $input) {
usageByUser {
key
creditsUsed
@@ -336,17 +336,27 @@ export class BillingResolver {
@RequireFeatureFlag(FeatureFlagKey.IS_USAGE_ANALYTICS_ENABLED)
async getBillingAnalytics(
@AuthWorkspace() workspace: WorkspaceEntity,
@Args({ nullable: true }) input?: BillingAnalyticsInput,
@Args('input', { nullable: true }) input?: BillingAnalyticsInput,
): Promise<BillingAnalyticsDTO> {
const subscription =
await this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow(
{ workspaceId: workspace.id },
);
let defaultPeriodStart: Date;
let defaultPeriodEnd: Date;
const periodStart =
input?.periodStart ?? subscription.currentPeriodStart;
const periodEnd =
input?.periodEnd ?? subscription.currentPeriodEnd;
if (this.billingService.isBillingEnabled()) {
const subscription =
await this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow(
{ workspaceId: workspace.id },
);
defaultPeriodStart = subscription.currentPeriodStart;
defaultPeriodEnd = subscription.currentPeriodEnd;
} else {
defaultPeriodEnd = new Date();
defaultPeriodStart = new Date();
defaultPeriodStart.setDate(defaultPeriodStart.getDate() - 30);
}
const periodStart = input?.periodStart ?? defaultPeriodStart;
const periodEnd = input?.periodEnd ?? defaultPeriodEnd;
const [usageByUser, usageByResource, usageByExecutionType, timeSeries] =
await Promise.all([