Skip to content

feat: add ORM-integrated realtime subscription support (Variation F)#1099

Merged
pyramation merged 3 commits intomainfrom
feat/orm-realtime-integrated
May 10, 2026
Merged

feat: add ORM-integrated realtime subscription support (Variation F)#1099
pyramation merged 3 commits intomainfrom
feat/orm-realtime-integrated

Conversation

@pyramation
Copy link
Copy Markdown
Contributor

@pyramation pyramation commented May 10, 2026

Summary

Adds WebSocket subscription support directly into the ORM codegen pipeline ("Variation F"), replacing the standalone package approach from #1097. Realtime is an optional mixin on OrmClient — apps that don't pass a realtime config get zero WebSocket overhead.

graphql-ws is loaded lazily inside the RealtimeManager constructor (via require() with a compile-time-erased import() type), so consumers that never enable realtime (e.g. the generated CLI) don't need the package installed at all.

Subscription hooks are gated on the @realtime smart tag — only tables annotated with @realtime in their PostgreSQL COMMENT get useXxxSubscription hooks. If no tables have the tag, the entire subscriptions/ directory (including useConnectionState and the barrel) is omitted. The companion PR constructive-io/constructive-db#1088 makes the DataRealtime generator automatically append @realtime to source tables.

New files:

  • templates/orm-realtime.ts — Runtime RealtimeManager class wrapping graphql-ws (connection lifecycle, multiplexing, exponential backoff, connection state listeners)
  • subscriptions.ts — Babel AST-based generators for per-table useXxxSubscription hooks and useConnectionState hook
  • subscription-hooks.test.ts — 30 tests, 4 snapshots
  • parseSmartTags() utility in @constructive-io/graphql-query — parses @-prefixed smart comment lines from descriptions

Modified files:

  • templates/orm-client.tsOrmClient gains subscribe(), getConnectionState(), onConnectionStateChange(), dispose(), isRealtimeEnabled
  • orm/client.ts — Type stubs for all realtime types
  • orm/client-generator.ts / orm/index.ts — Emit realtime.ts alongside client.ts
  • utils.ts — Subscription naming helpers (getSubscriptionHookName, getSubscriptionFieldName, etc.)
  • barrel.ts / index.ts — Wire subscriptions/ directory into codegen orchestrator and barrel exports; gate on @realtime smart tag
  • graphql/query/src/types/schema.ts and graphql/codegen/src/types/schema.ts — Add smartTags field to Table interface
  • graphql/query/src/introspect/infer-tables.ts — Parse smart tags from entity description via parseSmartTags()

Review & Testing Checklist for Human

  • Bug in generated onError handler: The snapshot shows optionsRef.current?.onError(err) — optional chaining is on .current (which is a ref and never null) instead of on .onError (which IS optional). Should be optionsRef.current.onError?.(err) to avoid a runtime crash when onError is not provided. Check subscriptions.ts generator.
  • Unused imports in generated hooks: Generated subscription hooks import useCallback from React and QueryClient type from @tanstack/react-query, but neither is used in the hook body. These should be removed from the generator.
  • Smart tag propagation through PostGraphile introspection: The gating depends on @realtime appearing as a @-prefixed line in the GraphQL type's description field from introspection. Verify that PostGraphile v5 actually passes through smart tags in entity descriptions (rather than stripping them at schema-build time). If it strips them, the parseSmartTags() call in infer-tables.ts will never see @realtime and no subscription hooks will be generated. This should be tested end-to-end against a real schema with DataRealtime applied.
  • Subscription document is a skeleton: The generated GraphQL document uses contact { __typename } — it doesn't select actual row fields. Confirm this is acceptable as a foundation to be fleshed out when the server-side subscription plugin lands.

Notes

  • This is codegen-only — no server-side subscription plugin exists yet, so the generated hooks cannot be tested end-to-end against a real WebSocket server.
  • Supersedes the standalone @constructive-io/realtime + @constructive-io/react-realtime packages from feat: prototype Variation E realtime client (standalone RealtimeClient + React hooks) #1097.
  • Companion PR: constructive-io/constructive-db#1088 scopes smart tag helpers (append_table_smart_tags, append_field_smart_tags) and makes DataRealtime append @realtime to the source table.
  • 344 codegen unit tests + 18 query unit tests passing, including 30 subscription hook tests (5 specifically for smart tag gating). TypeScript typecheck clean on both packages.

Link to Devin session: https://app.devin.ai/sessions/19485cf5cc58416a9f86068563d512f5
Requested by: @pyramation

- Add RealtimeManager runtime template (orm-realtime.ts) with WebSocket
  lifecycle management, subscription multiplexing, and connection state
- Extend OrmClient template with optional realtime config, subscribe(),
  getConnectionState(), onConnectionStateChange(), dispose() methods
- Add type stub for realtime types (SubscriptionEvent, RealtimeConfig, etc.)
- Create subscription hook generator producing per-table useXxxSubscription
  hooks with React Query cache invalidation bridge
- Create useConnectionState hook generator for connection status UI
- Add subscription naming utils (getSubscriptionHookName, etc.)
- Wire realtime file into ORM orchestrator and subscription hooks into
  main codegen orchestrator with barrel exports
- Add 25 tests for subscription generators (4 snapshots)
- Update client-generator snapshot for realtime imports

Zero overhead: apps without realtime config have no WebSocket dependency.
All 339 tests pass across 20 suites.
@devin-ai-integration
Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@blacksmith-sh

This comment has been minimized.

The realtime module is optional — apps that don't use subscriptions
(like the CLI) should not need graphql-ws installed. Changed the
top-level import to a TypeScript import() type (erased at compile
time) and moved the require() call inside the RealtimeManager
constructor so it only executes when realtime is actually used.
- Add smartTags field to Table interface in both query and codegen packages
- Parse smart tags from PostGraphile @-prefixed description lines in inferTablesFromIntrospection
- Export parseSmartTags utility from @constructive-io/graphql-query
- Gate subscription hook generation: only tables with @realtime smart tag get hooks
- Gate useConnectionState + subscriptions barrel: only emitted when at least one table has @realtime
- Add comprehensive Smart Tag Gating test suite (5 new tests, all passing)
- All 344 codegen tests + 18 query tests passing, typecheck clean
@pyramation pyramation merged commit 11b5f54 into main May 10, 2026
55 checks passed
@pyramation pyramation deleted the feat/orm-realtime-integrated branch May 10, 2026 10:28
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.

1 participant