feat: add ORM-integrated realtime subscription support (Variation F)#1099
Merged
pyramation merged 3 commits intomainfrom May 10, 2026
Merged
feat: add ORM-integrated realtime subscription support (Variation F)#1099pyramation merged 3 commits intomainfrom
pyramation merged 3 commits intomainfrom
Conversation
- 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.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This comment has been minimized.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 arealtimeconfig get zero WebSocket overhead.graphql-wsis loaded lazily inside theRealtimeManagerconstructor (viarequire()with a compile-time-erasedimport()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
@realtimesmart tag — only tables annotated with@realtimein their PostgreSQL COMMENT getuseXxxSubscriptionhooks. If no tables have the tag, the entiresubscriptions/directory (includinguseConnectionStateand the barrel) is omitted. The companion PR constructive-io/constructive-db#1088 makes theDataRealtimegenerator automatically append@realtimeto source tables.New files:
templates/orm-realtime.ts— RuntimeRealtimeManagerclass wrappinggraphql-ws(connection lifecycle, multiplexing, exponential backoff, connection state listeners)subscriptions.ts— Babel AST-based generators for per-tableuseXxxSubscriptionhooks anduseConnectionStatehooksubscription-hooks.test.ts— 30 tests, 4 snapshotsparseSmartTags()utility in@constructive-io/graphql-query— parses@-prefixed smart comment lines from descriptionsModified files:
templates/orm-client.ts—OrmClientgainssubscribe(),getConnectionState(),onConnectionStateChange(),dispose(),isRealtimeEnabledorm/client.ts— Type stubs for all realtime typesorm/client-generator.ts/orm/index.ts— Emitrealtime.tsalongsideclient.tsutils.ts— Subscription naming helpers (getSubscriptionHookName,getSubscriptionFieldName, etc.)barrel.ts/index.ts— Wiresubscriptions/directory into codegen orchestrator and barrel exports; gate on@realtimesmart taggraphql/query/src/types/schema.tsandgraphql/codegen/src/types/schema.ts— AddsmartTagsfield toTableinterfacegraphql/query/src/introspect/infer-tables.ts— Parse smart tags from entity description viaparseSmartTags()Review & Testing Checklist for Human
onErrorhandler: The snapshot showsoptionsRef.current?.onError(err)— optional chaining is on.current(which is a ref and never null) instead of on.onError(which IS optional). Should beoptionsRef.current.onError?.(err)to avoid a runtime crash whenonErroris not provided. Checksubscriptions.tsgenerator.useCallbackfrom React andQueryClienttype from@tanstack/react-query, but neither is used in the hook body. These should be removed from the generator.@realtimeappearing as a@-prefixed line in the GraphQL type'sdescriptionfield 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, theparseSmartTags()call ininfer-tables.tswill never see@realtimeand no subscription hooks will be generated. This should be tested end-to-end against a real schema withDataRealtimeapplied.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
@constructive-io/realtime+@constructive-io/react-realtimepackages from feat: prototype Variation E realtime client (standalone RealtimeClient + React hooks) #1097.append_table_smart_tags,append_field_smart_tags) and makesDataRealtimeappend@realtimeto the source table.Link to Devin session: https://app.devin.ai/sessions/19485cf5cc58416a9f86068563d512f5
Requested by: @pyramation