Skip to content

fix(react): centralize inline query keys into reusable factories - #442

Open
harishsundar-okta wants to merge 7 commits into
mainfrom
fix/reusable-queries
Open

fix(react): centralize inline query keys into reusable factories#442
harishsundar-okta wants to merge 7 commits into
mainfrom
fix/reusable-queries

Conversation

@harishsundar-okta

@harishsundar-okta harishsundar-okta commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Centralizes inline TanStack Query keys into reusable factory functions and extracts a shared organization details query hook to eliminate duplication and cache fragmentation across services.

Why

  • Several query hooks used hardcoded inline query key arrays instead of the established centralized factory pattern, making cache invalidation fragile and keys harder to refactor or reuse.
  • The organization details query was duplicated across 5 service hooks with inconsistent keys (['organization', 'details'] vs ['organization-details', 'details']), causing the same data to be cached multiple times and updates in one place to leave others stale.

What

  • Added mfaStepUpQueryKeys factory to core for MFA step-up enrollment/authenticator queries
  • Added invitationList(), memberList(), and identityProviders() parameterized factories to memberManagementQueryKeys
  • Updated use-mfa-requirements.ts to use centralized keys instead of raw string arrays
  • Updated use-member-management-service.ts to use factory functions instead of manual key spreading
  • Fixed hardcoded query key in use-domain-table-service.test.ts to use domainQueryKeys.lists()
  • Unified all organization details queries to use canonical organizationDetailsQueryKeys.details() key
  • Removed stale organization keys from memberManagementQueryKeys, memberDetailQueryKeys, and ssoProviderQueryKeys
  • Extracted useOrganizationDetailsQuery() shared hook consumed by all 5 services that need org details, eliminating ~50 lines of duplicated fetch logic

Packages

  • packages/core
  • packages/react
  • examples

References

Testing

How can this be verified? Note anything intentionally not covered by tests and why.

  • This change adds unit test coverage
  • Tested for both SPA and RWA flows, all example apps working
  • All existing and new tests complete without errors
image image

Checklist

  • Breaking change
  • Requires docs update
  • Backward compatible

Contributing

Summary by CodeRabbit

  • Refactor

    • Improved data refresh and caching consistency across organization details, member management, domain, and SSO provider views.
    • Added structured support for paginated, filtered, and sorted member and invitation lists.
    • Improved MFA step-up data handling for enrollment factors and authenticators.
  • Tests

    • Expanded coverage for member-management and MFA data retrieval scenarios, including parameterized queries and token-specific results.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This change centralizes React Query keys for MFA step-up, member management, organization details, and domains. It adds parameterized key builders, migrates service consumers to shared keys, removes obsolete organization helpers, and expands query-key tests.

Changes

MFA query keys

Layer / File(s) Summary
MFA step-up key contract and usage
packages/core/src/services/my-account/user-mfa-management/user-mfa-management-constants.ts, packages/react/src/hooks/shared/use-mfa-requirements.ts, packages/core/src/services/my-account/user-mfa-management/__tests__/mfa-query-keys.test.ts
Adds token-scoped enrollment-factor and authenticator keys. The MFA requirements hook uses these builders, and tests validate key structure and token uniqueness.

Organization query keys

Layer / File(s) Summary
Member-management key contract and list queries
packages/core/src/services/my-organization/member-management/member-management-constants.ts, packages/react/src/hooks/my-organization/shared/services/use-member-management-service.ts, packages/react/src/hooks/my-organization/__tests__/use-member-management-service.test.ts
Adds list parameters and dedicated identity-provider, invitation-list, and member-list builders. The service uses the builders for list queries and organization details. Tests cover the generated keys.
Organization-details key migration
packages/core/src/services/my-organization/idp-management/sso-provider/sso-provider-query-keys.ts, packages/react/src/hooks/my-organization/shared/services/use-member-detail-service.ts, packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts, packages/react/src/hooks/my-organization/shared/services/use-sso-provider-table-service.ts
Removes obsolete SSO-provider and member-management organization helpers. Organization queries, cache preloading, and cache lookups use organizationDetailsQueryKeys.details().

Domain query keys

Layer / File(s) Summary
Domain invalidation key alignment
packages/react/src/hooks/my-organization/shared/__tests__/use-domain-table-service.test.ts
The invalidation test uses domainQueryKeys.lists() instead of a literal query-key array.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested labels: refactor

Suggested reviewers: naveenchand755

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: centralizing inline React Query keys into reusable query-key factories.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/reusable-queries

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

codecov-commenter commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.84848% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.88%. Comparing base (de34cb4) to head (9579bb8).

Files with missing lines Patch % Lines
...n/member-management/member-management-constants.ts 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #442      +/-   ##
==========================================
- Coverage   90.89%   90.88%   -0.01%     
==========================================
  Files         239      239              
  Lines       17696    17703       +7     
  Branches     1995     2595     +600     
==========================================
+ Hits        16084    16089       +5     
- Misses       1612     1614       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/core/src/services/my-account/mfa/mfa-constants.ts (1)

27-33: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add unit tests for the new query-key factory.

Cover the exact outputs of all, enrollmentFactors(mfaToken), and authenticators(mfaToken), including distinct tokens producing distinct keys. As per coding guidelines, add unit tests for new hooks/utilities.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/services/my-account/mfa/mfa-constants.ts` around lines 27 -
33, Add unit tests for the mfaStepUpQueryKeys factory, asserting the exact array
values returned by all, enrollmentFactors, and authenticators. Include cases
with different mfaToken values to verify each token produces a distinct key.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/core/src/services/my-organization/member-management/member-management-constants.ts`:
- Around line 14-25: Add direct unit tests for the memberManagementQueryKeys
factories, covering invitationList, memberList, identityProviders, and the roles
factory referenced as roles (memberRoles in the review). Assert each factory
returns the exact expected readonly key array, including representative
invitation/member parameters and a roles search-independent key.

In
`@packages/react/src/hooks/my-organization/shared/services/use-member-management-service.ts`:
- Line 89: Remove the locally declared memberManagementQueryKeys from the
service and import the shared memberManagementQueryKeys export from
`@auth0/universal-components-core`. Update the query and cache-management calls at
the referenced locations to use that core factory consistently, preserving the
existing key method usage.

---

Nitpick comments:
In `@packages/core/src/services/my-account/mfa/mfa-constants.ts`:
- Around line 27-33: Add unit tests for the mfaStepUpQueryKeys factory,
asserting the exact array values returned by all, enrollmentFactors, and
authenticators. Include cases with different mfaToken values to verify each
token produces a distinct key.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3f11e868-449e-45c6-b4a4-8cd3d2b0cc6f

📥 Commits

Reviewing files that changed from the base of the PR and between 66cdddc and c4e392d.

📒 Files selected for processing (5)
  • packages/core/src/services/my-account/mfa/mfa-constants.ts
  • packages/core/src/services/my-organization/member-management/member-management-constants.ts
  • packages/react/src/hooks/my-organization/shared/__tests__/use-domain-table-service.test.ts
  • packages/react/src/hooks/my-organization/shared/services/use-member-management-service.ts
  • packages/react/src/hooks/shared/use-mfa-requirements.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
packages/core/src/services/my-account/mfa/__tests__/mfa-query-keys.test.ts (1)

5-6: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use when... descriptions for test conditions.

Rename the top-level describe blocks to condition-oriented names, such as when accessing MFA query keys and when accessing MFA step-up query keys; keep the it names action-oriented.

As per coding guidelines, Vitest tests must describe conditions with when... and describe actions in the it name.

Also applies to: 16-17

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/services/my-account/mfa/__tests__/mfa-query-keys.test.ts`
around lines 5 - 6, Rename the top-level describe blocks in mfaQueryKeys tests
to condition-oriented when... descriptions, including the MFA step-up query keys
block, while keeping the existing it descriptions action-oriented.

Source: Coding guidelines

packages/react/src/hooks/my-organization/__tests__/use-member-management-service.test.ts (1)

74-106: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use action-oriented names for the Vitest cases.

Rename these it descriptions from “should have...” to action-oriented names such as “returns the expected invitation list key” and “returns the expected identity provider key.”

As per coding guidelines, Vitest tests should describe conditions with when... and describe actions in the it name.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/react/src/hooks/my-organization/__tests__/use-member-management-service.test.ts`
around lines 74 - 106, Rename the affected Vitest descriptions in the
memberManagementQueryKeys tests from “should have...” to action-oriented wording
that states the returned result, such as “returns the expected invitation list
key,” “returns the expected members key,” “returns the expected member list
key,” and “returns the expected identity provider key.” Keep the test behavior
and assertions unchanged.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/core/src/services/my-account/mfa/__tests__/mfa-query-keys.test.ts`:
- Around line 5-6: Rename the top-level describe blocks in mfaQueryKeys tests to
condition-oriented when... descriptions, including the MFA step-up query keys
block, while keeping the existing it descriptions action-oriented.

In
`@packages/react/src/hooks/my-organization/__tests__/use-member-management-service.test.ts`:
- Around line 74-106: Rename the affected Vitest descriptions in the
memberManagementQueryKeys tests from “should have...” to action-oriented wording
that states the returned result, such as “returns the expected invitation list
key,” “returns the expected members key,” “returns the expected member list
key,” and “returns the expected identity provider key.” Keep the test behavior
and assertions unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b28ffd95-1659-459e-b120-907621c32f83

📥 Commits

Reviewing files that changed from the base of the PR and between c4e392d and d47cdfe.

📒 Files selected for processing (2)
  • packages/core/src/services/my-account/mfa/__tests__/mfa-query-keys.test.ts
  • packages/react/src/hooks/my-organization/__tests__/use-member-management-service.test.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/react/src/hooks/my-organization/shared/services/use-organization-details-query.ts`:
- Around line 16-26: The useOrganizationDetailsQuery hook lacks direct unit
coverage for its query configuration and behavior. Add Vitest tests targeting
useOrganizationDetailsQuery that verify the organizationDetailsQueryKeys.details
cache key, API organizationDetails.get call, OrganizationDetailsMappers.fromAPI
mapping, and the disabled/no-client behavior when coreClient is unavailable.

In
`@packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts`:
- Around line 399-401: Update the detach flow around ensureQueryData to reuse
the existing organization query state or shared organization-details query,
including its queryFn, rather than calling ensureQueryData with only the details
key. Ensure organization details are available when the cache is missing or
evicted, and prevent detach from starting while organizationQuery.isLoading.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c4f5adcf-7a05-433e-9a53-13dcfc85edb4

📥 Commits

Reviewing files that changed from the base of the PR and between d47cdfe and eb98cd7.

📒 Files selected for processing (9)
  • packages/core/src/services/my-organization/idp-management/sso-provider/sso-provider-query-keys.ts
  • packages/core/src/services/my-organization/member-management/member-management-constants.ts
  • packages/react/src/hooks/my-organization/shared/__tests__/use-sso-provider-table-service.test.ts
  • packages/react/src/hooks/my-organization/shared/services/use-member-detail-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-member-management-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-organization-details-edit-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-organization-details-query.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-table-service.ts
💤 Files with no reviewable changes (2)
  • packages/core/src/services/my-organization/member-management/member-management-constants.ts
  • packages/core/src/services/my-organization/idp-management/sso-provider/sso-provider-query-keys.ts

…nto fix/reusable-queries

# Conflicts:
#	packages/core/src/services/my-account/user-mfa-management/__tests__/mfa-query-keys.test.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts (1)

83-90: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Do not cache placeholder organization data under the shared key.

initialData: OrganizationDetailsFactory.create() persists the empty organization object to organizationDetailsQueryKeys.details(), and this key is shared by useMemberManagementService, useMemberDetailService, and useSsoProviderTableService. useSsoProviderTableService.fetchOrganizationDetails can then get that cached placeholder from ensureQueryData while the API request is pending. Remove initialData from the shared query and keep the local return fallback at line 722.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts`
around lines 83 - 90, Remove the initialData option using
OrganizationDetailsFactory.create() from the shared organization details query,
so the placeholder is not cached under organizationDetailsQueryKeys.details().
Preserve the local fallback return in
useSsoProviderTableService.fetchOrganizationDetails.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts`:
- Around line 83-90: Remove the initialData option using
OrganizationDetailsFactory.create() from the shared organization details query,
so the placeholder is not cached under organizationDetailsQueryKeys.details().
Preserve the local fallback return in
useSsoProviderTableService.fetchOrganizationDetails.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7e10812a-09dc-4020-87ce-0f52eb93d56e

📥 Commits

Reviewing files that changed from the base of the PR and between eb98cd7 and a807b95.

📒 Files selected for processing (4)
  • packages/react/src/hooks/my-organization/shared/services/use-member-detail-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-member-management-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-table-service.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/core/src/services/my-account/user-mfa-management/user-mfa-management-constants.ts (1)

27-33: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the new exported query-key contract.

mfaStepUpQueryKeys is exported from core and consumed by the React layer. Add JSDoc for the namespace and both builders. Include @param and @returns, or mark the symbol @internal if it is not part of the package public API.

As per coding guidelines, document public APIs with JSDoc and use @module, @internal, @param, and @returns tags where applicable.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/core/src/services/my-account/user-mfa-management/user-mfa-management-constants.ts`
around lines 27 - 33, The exported mfaStepUpQueryKeys contract lacks
documentation. Add JSDoc to mfaStepUpQueryKeys and its enrollmentFactors and
authenticators builders, including `@param` and `@returns` descriptions for each
builder; if this export is not intended as public API, mark the namespace
`@internal` instead.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In
`@packages/core/src/services/my-account/user-mfa-management/user-mfa-management-constants.ts`:
- Around line 27-33: The exported mfaStepUpQueryKeys contract lacks
documentation. Add JSDoc to mfaStepUpQueryKeys and its enrollmentFactors and
authenticators builders, including `@param` and `@returns` descriptions for each
builder; if this export is not intended as public API, mark the namespace
`@internal` instead.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1ac7d178-2eb1-486c-ad2c-9edf6327a39f

📥 Commits

Reviewing files that changed from the base of the PR and between a807b95 and 9579bb8.

📒 Files selected for processing (4)
  • packages/core/src/services/my-account/user-mfa-management/__tests__/mfa-query-keys.test.ts
  • packages/core/src/services/my-account/user-mfa-management/user-mfa-management-constants.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-table-service.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-edit-service.ts
  • packages/react/src/hooks/my-organization/shared/services/use-sso-provider-table-service.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants