37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
import { writeFileSync } from "fs";
|
|
import {
|
|
getIntrospectionQuery,
|
|
graphqlSync,
|
|
lexicographicSortSchema,
|
|
printSchema,
|
|
} from "graphql";
|
|
import { createPostGraphileSchema } from "postgraphile";
|
|
import { Pool } from "pg";
|
|
import { loadConfig } from "config";
|
|
import { getPostGraphileOptions } from "@digiresilience/metamigo-db";
|
|
|
|
export const exportGraphqlSchema = async (): Promise<void> => {
|
|
const config = await loadConfig();
|
|
|
|
const rootPgPool = new Pool({
|
|
connectionString: config.db.connection,
|
|
});
|
|
const exportSchema = `../../data/schema.graphql`;
|
|
const exportJson = `../../frontend/lib/graphql-schema.json`;
|
|
try {
|
|
const schema = await createPostGraphileSchema(
|
|
config.postgraphile.authConnection,
|
|
"app_public",
|
|
getPostGraphileOptions()
|
|
);
|
|
const sorted = lexicographicSortSchema(schema);
|
|
const json = graphqlSync(schema, getIntrospectionQuery());
|
|
writeFileSync(exportSchema, printSchema(sorted));
|
|
writeFileSync(exportJson, JSON.stringify(json));
|
|
|
|
console.log(`GraphQL schema exported to ${exportSchema}`);
|
|
console.log(`GraphQL schema json exported to ${exportJson}`);
|
|
} finally {
|
|
rootPgPool.end();
|
|
}
|
|
};
|