feat(datastore): add subscription variables for multi-tenant filtering #14564
+785
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds subscription variables support to DataStore, addressing the long-standing need for server-side subscription filtering in multi-tenant applications. Fixes #9413.
The implementation adds a new
subscriptionVariables
configuration option that allows passing custom variables to GraphQL subscriptions. This enables filtering at the subscription level rather than receiving all updates and filtering client-side - a critical requirement for multi-tenant SaaS applications where each tenant should only receive their own real-time updates.The solution integrates cleanly with the existing DataStore sync engine by extending
DataStoreConfig
with an optionalsubscriptionVariables
field. Variables are processed inSubscriptionProcessor
and passed through tobuildSubscriptionGraphQLOperation
. The implementation supports both static variables (objects) and dynamic variables (functions that receive the operation type).The implementation includes comprehensive production safeguards: deep cloning to prevent mutation side effects on cached values, filtering of reserved GraphQL keywords (filter, owner, limit, etc.), memoization to avoid redundant function calls, and proper error handling for edge cases like circular references and Object.create(null).
This directly addresses the use cases discussed in the issue thread where developers need to filter subscriptions by tenant/store IDs at the server level. Without this feature, multi-tenant apps must receive all updates across all tenants and filter client-side, which is inefficient and potentially insecure.
The changes are fully backward compatible - existing DataStore usage continues to work unchanged. Added 12 comprehensive unit tests covering basic functionality, edge cases, mutation protection, and caching behavior. All existing DataStore tests continue to pass.