Skip to content

feat(datastore): add subscription variables for multi-tenant filtering#14564

Open
anivar wants to merge 1 commit intoaws-amplify:mainfrom
anivar:feat/datastore-subscription-variables
Open

feat(datastore): add subscription variables for multi-tenant filtering#14564
anivar wants to merge 1 commit intoaws-amplify:mainfrom
anivar:feat/datastore-subscription-variables

Conversation

@anivar
Copy link
Copy Markdown
Contributor

@anivar anivar commented Sep 25, 2025

Description of changes

Adds a subscriptionVariables config option to DataStore.configure() that passes custom GraphQL variables to subscription operations. This enables server-side subscription filtering for multi-tenant apps, avoiding the current approach of receiving all updates and filtering client-side.

Usage:

DataStore.configure({
  subscriptionVariables: {
    Todo: { storeId: 'store-123' },
    Order: (op) => ({ storeId: op === 'DELETE' ? undefined : 'store-123' }),
  },
});

What changed:

  • New subscriptionVariables field in DataStoreConfig type
  • Config plumbed through SyncEngineSubscriptionProcessor
  • processSubscriptionVariables() in sync/utils.ts — validates input, filters reserved names, supports static objects or per-operation functions
  • buildSubscriptionGraphQLOperation() accepts custom variables for query building
  • Variables injected into subscription variables before GraphQL call

What was simplified vs original:

  • Removed WeakMap+Map cache (unnecessary complexity — functions are called once per subscription setup)
  • Removed redundant try/catch in sanitizeSubscriptionVariables and filterReservedSubscriptionVariableKeys
  • Merged into single processSubscriptionVariables function
  • Trimmed reserved names list to actual GraphQL-conflicting names

Issue #, if available

Fixes #9413

Description of how you validated changes

  • 12 new tests across 2 test files (subscription-variables, edge-cases)
  • All 19 existing subscription processor tests pass
  • ESLint and TypeScript compilation pass

Checklist

  • PR description included
  • yarn test passes
  • Unit Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 7, 2026

🦋 Changeset detected

Latest commit: f6ba207

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@aws-amplify/datastore Minor
aws-amplify Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Add subscriptionVariables config option to DataStore.configure() that
passes custom GraphQL variables to subscription operations. Enables
server-side filtering for multi-tenant apps instead of client-side.

Usage:
  DataStore.configure({
    subscriptionVariables: {
      Todo: { storeId: 'store-123' },
      Order: (op) => ({ storeId: op === 'DELETE' ? undefined : 'store-123' }),
    },
  });

Variables are validated (must be plain objects), reserved GraphQL names
(filter, owner, input, etc.) are filtered out with warnings, and
function-form variables receive the operation type.

Fixes aws-amplify#9413
@anivar anivar force-pushed the feat/datastore-subscription-variables branch from 972faa4 to f6ba207 Compare March 7, 2026 14:08
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.

DataStore Subscriptions by Argument

2 participants