Skip to content

Commit 449785a

Browse files
authored
Merge pull request #912 from constructive-io/devin/1774572350-introspection-json-export
feat(graphile-schema): add buildIntrospectionJSON() for database metadata export
2 parents 41c6b7e + c725c7e commit 449785a

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { TableMeta } from 'graphile-settings'
2+
import { _cachedTablesMeta } from 'graphile-settings'
3+
import { buildSchemaSDL } from './build-schema'
4+
import type { BuildSchemaOptions } from './build-schema'
5+
6+
export type { BuildSchemaOptions as BuildIntrospectionOptions }
7+
8+
/**
9+
* Build introspection metadata for all tables visible in the given schemas.
10+
*
11+
* Internally calls `buildSchemaSDL()` which triggers the MetaSchemaPlugin
12+
* gather hook, populating `_cachedTablesMeta` as a side-effect. The cached
13+
* metadata is then returned as a plain array of `TableMeta` objects.
14+
*
15+
* The result includes every table's fields, types, constraints, indexes,
16+
* relations, inflection names, and query entry-points — the same data
17+
* exposed by the `_meta` GraphQL query at runtime.
18+
*
19+
* @example
20+
* ```ts
21+
* import { buildIntrospectionJSON } from 'graphile-schema';
22+
* import fs from 'fs';
23+
*
24+
* const tables = await buildIntrospectionJSON({
25+
* database: 'my_db',
26+
* schemas: ['public', 'app_public'],
27+
* });
28+
* fs.writeFileSync('introspection.json', JSON.stringify(tables, null, 2));
29+
* ```
30+
*/
31+
export async function buildIntrospectionJSON(
32+
opts: BuildSchemaOptions
33+
): Promise<TableMeta[]> {
34+
await buildSchemaSDL(opts)
35+
return [..._cachedTablesMeta]
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
export { buildSchemaSDL } from './build-schema';
22
export type { BuildSchemaOptions } from './build-schema';
3+
export { buildIntrospectionJSON } from './build-introspection';
34
export { _cachedTablesMeta } from 'graphile-settings';
5+
export type {
6+
TableMeta,
7+
FieldMeta,
8+
TypeMeta,
9+
IndexMeta,
10+
ConstraintsMeta,
11+
PrimaryKeyConstraintMeta,
12+
UniqueConstraintMeta,
13+
ForeignKeyConstraintMeta,
14+
RelationsMeta,
15+
BelongsToRelation,
16+
HasRelation,
17+
ManyToManyRelation,
18+
InflectionMeta,
19+
QueryMeta,
20+
} from 'graphile-settings';
421
export { fetchEndpointSchemaSDL } from './fetch-endpoint-schema';
522
export type { FetchEndpointSchemaOptions } from './fetch-endpoint-schema';

graphile/graphile-settings/src/plugins/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ export {
5454
MetaSchemaPlugin,
5555
MetaSchemaPreset,
5656
} from './meta-schema';
57+
export type {
58+
TableMeta,
59+
FieldMeta,
60+
TypeMeta,
61+
IndexMeta,
62+
ConstraintsMeta,
63+
PrimaryKeyConstraintMeta,
64+
UniqueConstraintMeta,
65+
ForeignKeyConstraintMeta,
66+
RelationsMeta,
67+
BelongsToRelation,
68+
HasRelation,
69+
ManyToManyRelation,
70+
InflectionMeta,
71+
QueryMeta,
72+
} from './meta-schema/types';
5773

5874
// PG type mappings for custom PostgreSQL types (email, url, etc.)
5975
export {

0 commit comments

Comments
 (0)