refactor(api): replace inline SQL with modular loader registry#1217
Merged
Conversation
Wire api.ts to use the express-context loader registry for all per-database module resolution. This removes ~430 lines of inline SQL queries, row types, query functions, and transform helpers that are now encapsulated in individual loader modules. Changes: - api.ts: resolveApiNameHeader + resolveDomainLookup now call resolveModuleSettings() which delegates to the loader registry - api.ts: removed 17 SQL constants, 9 row/module interfaces, 12 query/transform functions — all now live in express-context loaders - graphql/server/src/types.ts: re-exports from express-context instead of duplicating type definitions - express-context README: added logo + CI badge + npm version badge to match other packages in the monorepo Zero breaking changes: ApiStructure fields (rlsModule, authSettings, corsOrigins, etc.) are still populated identically. req.api.rlsModule continues to work. svcCache still caches the full ApiStructure by domain key. api.ts: 1035 → 603 lines types.ts: 131 → 20 lines
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 LRU cache was keyed only by databaseId, causing incorrect cache hits when multiple APIs share the same database but have different api_settings overrides (e.g. per-API feature flag gating). Now the cache key includes apiId when present: databaseId:apiId (when apiId is available) databaseId (when apiId is not available) Invalidation by databaseId clears all entries for that database regardless of apiId.
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
Wires
graphql/server/src/middleware/api.tsto use the loader registry from PR #1215 (@constructive-io/express-context) for all per-database module resolution.What changed
api.ts(1035 → 603 lines, -430 lines):resolveApiNameHeaderandresolveDomainLookupnow callresolveModuleSettings()which delegates to the loader registry'sresolve()method for each moduleRLS_SETTINGS_SQL,CORS_SETTINGS_SQL,DATABASE_SETTINGS_SQL,AUTH_SETTINGS_DISCOVERY_SQL,AUTH_SETTINGS_SQL, etc.)RlsModuleData,AuthSettingsRow,CorsSettingsRow,PubkeySettingsRow,WebauthnSettingsRow,DatabaseSettingsRow, etc.)queryRlsModule,queryCorsOrigins,queryDatabaseSettings,queryAuthSettings,toRlsModule,toRlsModuleFromSettings,toAuthSettings,toPubkeyChallengeSettings, etc.)express-context/src/loaders/graphql/server/src/types.ts(131 → 20 lines):@constructive-io/express-contextinstead of duplicating definitionsApiOptions(local to graphql-server) remains defined hereexpress-context/src/loaders/create-loader.ts(cache key fix):databaseId:apiId(composite) instead of justdatabaseIddatabaseSettingsandcorsOriginswhere per-API overrides (api_settings) can differ for the same databasedatabaseIdclears all entries for that database regardless of apiIdexpress-context/README.md:Zero breaking changes
req.api.rlsModulereq.api.rlsModulereq.api.authSettingsreq.api.authSettingsreq.api.corsOriginsreq.api.corsOriginsreq.api.databaseSettingsreq.api.databaseSettingsreq.api.pubkeyChallengeSettingsreq.api.pubkeyChallengeSettingsreq.api.webauthnSettingsreq.api.webauthnSettingssvcCacheper-domain cachingsvcCacheper-domain cachingimport { RlsModule } from '../types'import { RlsModule } from '../types'ApiStructurefields are still populated identically. Every downstream consumer (auth.ts,cookie.ts,cors.ts,graphile.ts,upload.ts) continues to import from../typesand gets the same types.Architecture
Each loader has its own per-
databaseId:apiIdLRU cache. WhensvcCachemisses but loaders have cached data, no SQL is re-executed.Review & Testing Checklist for Human
express-context/src/loaders/*.ts) against the removed SQL fromapi.tsto confirm they're functionally identical. The loaders were written to mirrorapi.tsexactly, but a review is warranted.import { RlsModule } from '../types'inauth.ts,upload.ts,cookie.ts, etc. still resolves correctly through the re-export chain (graphql/server/types.ts→express-context/types.ts)api.ts → auth.ts (reads req.api.rlsModule) → pgSettings. Confirm auth still works end-to-end with a real database.Notes
databaseId, which caused stale hits when two APIs on the same database had differentapi_settingsoverrides. Fixed by using compositedatabaseId:apiIdkeys. The upload integration test ("Bob restricted API: uploadAppFile NOT exposed") now passes.graphql/serverhas 6 pre-existing build errors ingraphile.ts,llm-api.ts, andupload.tsrelated to unresolved workspace deps (graphile-settings,graphile-llm). These exist identically onmain.isDevhelper was removed fromapi.tsas it was already unused —getNodeEnv()is called directly inbuildDevFallbackError.Link to Devin session: https://app.devin.ai/sessions/151753dc357b48528652af31a63decc0
Requested by: @pyramation