Skip to content

fix(graphile-schema): correlate SDL and _meta metadata to one schema build - #1561

Merged
pyramation merged 3 commits into
mainfrom
feat/correlated-schema-artifacts
Jul 31, 2026
Merged

fix(graphile-schema): correlate SDL and _meta metadata to one schema build#1561
pyramation merged 3 commits into
mainfrom
feat/correlated-schema-artifacts

Conversation

@pyramation

@pyramation pyramation commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes constructive-io/constructive-planning#1331 — offline schema builds could return one schema's GraphQL artifacts paired with another schema's _meta metadata, because callers read the process-global _cachedTablesMeta as a separate operation after an awaited build (A write → B write → A read).

The result now comes from one correlated build boundary keyed on the exact final executable GraphQLSchema:

// graphile-meta: schema-keyed accessor over the existing WeakMap
export function getTablesMetaForSchema(schema: GraphQLSchema): TableMeta[] | undefined

// graphile-schema
export async function buildSchemaArtifacts(opts): Promise<{ sdl: string; tablesMeta: TableMeta[] }> {
  const { schema } = await makeSchema(preset);
  // execute shallow _meta against this schema, then:
  tablesMeta = getTablesMetaForSchema(schema) ?? [];   // same GraphQLSchema identity
  return { sdl: printSchema(lexicographicSortSchema(schema)), tablesMeta };
}

// existing APIs become projections
buildSchemaSDL(opts)        // => artifacts.sdl
buildIntrospectionJSON(opts) // => artifacts.tablesMeta
DatabaseSchemaSource.fetch() // consumes { sdl, tablesMeta } from the same call
  • _cachedTablesMeta and its plumbing (cache.ts, the finalize mirror hook, all re-exports) are deleted outright — no deprecated compatibility export. Live _meta behavior is unchanged and stays WeakMap-scoped.
  • If the meta plugin is disabled, tablesMeta is an explicit [] (never stale metadata from an unrelated build).
  • An internal test-only _onMetaCollected hook lets regression tests deterministically force the unsafe A write → B write → A read schedule (ordinary Promise.all stress was shown to be too weak/flaky in the issue).

Tests

  • graphile/graphile-schema/__tests__/build-schema-artifacts.test.ts (live PG): sequential, forced-interleave, uncoordinated-concurrent, and plugin-disabled cases — each caller gets only its own metadata.
  • graphql/codegen/src/__tests__/introspect/database-source.test.ts (unit): DatabaseSchemaSource.fetch() returns introspection and tablesMeta belonging to the same schema even when another build completes mid-fetch.
  • CI: graphile/graphile-meta and graphile/graphile-schema are now run in the pg-graphql batch (previously only built).

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

…ld boundary

Offline schema builds could pair one schema's SDL/introspection with another
schema's _meta metadata because callers read the process-global
_cachedTablesMeta after an awaited build. buildSchemaArtifacts() now returns
{ sdl, tablesMeta } derived from the same final executable GraphQLSchema
(via a schema-keyed WeakMap accessor), and buildSchemaSDL /
buildIntrospectionJSON / DatabaseSchemaSource.fetch are projections of it.

Fixes constructive-io/constructive-planning#1331
@pyramation pyramation self-assigned this Jul 31, 2026
@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, CI, and merge conflict monitoring

The process-global metadata channel has no remaining consumers; remove
cache.ts, the finalize mirror hook, and all re-exports instead of keeping
a deprecated export.
@pyramation
pyramation merged commit 97a18c2 into main Jul 31, 2026
17 checks passed
@pyramation
pyramation deleted the feat/correlated-schema-artifacts branch July 31, 2026 08:32
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