Skip to content

Commit

Permalink
Fix yarn server schema:export command via makeSchema (#357)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjie Gillam <benjie@jemjie.com>
  • Loading branch information
jamesallain and benjie committed Mar 17, 2023
1 parent 0545050 commit 61febc6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions @app/server/scripts/schema-export.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { writeFileSync } from "fs";
import { lexicographicSortSchema, printSchema } from "graphql";
import { Pool } from "pg";
import { createPostGraphileSchema } from "postgraphile";
import { makeSchema } from "postgraphile";

import { getPostGraphileOptions } from "../src/graphile.config";
import { getPreset } from "../src/graphile.config";

async function main() {
const rootPgPool = new Pool({
connectionString: process.env.DATABASE_URL!,
const authPgPool = new Pool({
connectionString: process.env.AUTH_DATABASE_URL!,
});
const preset = {
extends: [getPreset({ authPgPool })],
schema: {
// Turn off built-in schema exporting
exportSchemaSDLPath: undefined,
exportSchemaIntrospectionResultPath: undefined,
},
};

try {
const schema = await createPostGraphileSchema(
process.env.AUTH_DATABASE_URL!,
"app_public",
getPostGraphileOptions({ rootPgPool })
);
const { schema } = await makeSchema(preset);
const sorted = lexicographicSortSchema(schema);
writeFileSync(
`${__dirname}/../../../data/schema.graphql`,
printSchema(sorted)
printSchema(sorted) + "\n"
);
console.log("GraphQL schema exported");
} finally {
rootPgPool.end();
authPgPool.end();
}
}

Expand Down
4 changes: 2 additions & 2 deletions @app/server/src/graphile.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const isDev = process.env.NODE_ENV === "development";

interface IPostGraphileOptionsOptions {
authPgPool: Pool;
rootPgPool: Pool;
rootPgPool?: Pool;
}

export function getPreset({
Expand Down Expand Up @@ -224,7 +224,7 @@ export function getPreset({
const sessionId = uuidOrNull(req.user?.session_id);
if (sessionId) {
// Update the last_active timestamp (but only do it at most once every 15 seconds to avoid too much churn).
await rootPgPool.query(
await rootPgPool?.query(
"UPDATE app_private.sessions SET last_active = NOW() WHERE uuid = $1 AND last_active < NOW() - INTERVAL '15 seconds'",
[sessionId]
);
Expand Down

0 comments on commit 61febc6

Please sign in to comment.