Skip to content

Commit

Permalink
fix: schema.graphql codegen (#1257)
Browse files Browse the repository at this point in the history
* fix: move database connection to graphql context

* changeset
  • Loading branch information
typedarray authored Nov 19, 2024
1 parent 7b156ea commit dc768bd
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 165 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-pigs-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ponder/core": patch
---

Fixed a bug where the `codegen` command did not generate `generated/schema.graphql`.
3 changes: 2 additions & 1 deletion packages/core/src/bin/commands/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export async function codegen({ cliOptions }: { cliOptions: CliOptions }) {
properties: { cli_command: "codegen" },
});

runCodegen({ common });
const graphqlSchema = buildResult.indexingBuild.graphqlSchema;
runCodegen({ common, graphqlSchema });

logger.info({ service: "codegen", msg: "Wrote ponder-env.d.ts" });
logger.info({ service: "codegen", msg: "Wrote schema.graphql" });
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/bin/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export async function serve({ cliOptions }: { cliOptions: CliOptions }) {
});

const server = await createServer({
common,
app: buildResult.apiBuild.app,
routes: buildResult.apiBuild.routes,
common,
schema,
graphqlSchema: buildResult.indexingBuild.graphqlSchema,
database,
instanceId:
process.env.PONDER_EXPERIMENTAL_INSTANCE_ID === undefined
Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/bin/utils/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { IndexingBuild } from "@/build/index.js";
import { buildSchema } from "@/build/schema.js";
import { createDatabase } from "@/database/index.js";
import { onchainTable } from "@/drizzle/index.js";
import { buildGraphQLSchema } from "@/graphql/index.js";
import { promiseWithResolvers } from "@ponder/common";
import { beforeEach, expect, test, vi } from "vitest";
import { run } from "./run.js";
Expand All @@ -20,22 +21,24 @@ const account = onchainTable("account", (p) => ({
balance: p.bigint().notNull(),
}));

// const graphqlSchema = buildGraphQLSchema({ schema: { account } });
const schema = { account };
const graphqlSchema = buildGraphQLSchema(schema);

test("run() setup", async (context) => {
const indexingFunctions = {
"Erc20:setup": vi.fn(),
};

const { statements, namespace } = buildSchema({
schema: { account },
schema,
instanceId: "1234",
});

const build: IndexingBuild = {
buildId: "buildId",
instanceId: "1234",
schema: { account },
schema,
graphqlSchema,
databaseConfig: context.databaseConfig,
networks: context.networks,
sources: context.sources,
Expand All @@ -46,7 +49,7 @@ test("run() setup", async (context) => {

const database = createDatabase({
common: context.common,
schema: { account },
schema,
databaseConfig: context.databaseConfig,
instanceId: "1234",
buildId: "buildId",
Expand Down Expand Up @@ -76,14 +79,15 @@ test("run() setup error", async (context) => {
const onReloadableErrorPromiseResolver = promiseWithResolvers<void>();

const { statements, namespace } = buildSchema({
schema: { account },
schema,
instanceId: "1234",
});

const build: IndexingBuild = {
buildId: "buildId",
instanceId: "1234",
schema: { account },
schema,
graphqlSchema,
databaseConfig: context.databaseConfig,
networks: context.networks,
sources: context.sources,
Expand All @@ -94,7 +98,7 @@ test("run() setup error", async (context) => {

const database = createDatabase({
common: context.common,
schema: { account },
schema,
databaseConfig: context.databaseConfig,
instanceId: "1234",
buildId: "buildId",
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/bin/utils/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ export async function run({
onFatalError: (error: Error) => void;
onReloadableError: (error: Error) => void;
}) {
const { instanceId, networks, sources, schema, indexingFunctions } = build;
const {
instanceId,
networks,
sources,
schema,
indexingFunctions,
graphqlSchema,
} = build;

let isKilled = false;

Expand All @@ -52,7 +59,7 @@ export async function run({
// starting the server so the app can become responsive more quickly.
await database.migrateSync();

runCodegen({ common });
runCodegen({ common, graphqlSchema });

// Note: can throw
const sync = await createSync({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/bin/utils/runServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export async function runServer({
build: ApiBuild;
database: Database;
}) {
const { schema, instanceId } = build;
const { instanceId, graphqlSchema } = build;

const server = await createServer({
app: build.app,
routes: build.routes,
common,
schema,
graphqlSchema,
database,
instanceId,
});
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/build/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BuildError } from "@/common/errors.js";
import { type Schema, isPgEnumSym } from "@/drizzle/index.js";
import { getSql } from "@/drizzle/kit/index.js";
import { buildGraphQLSchema } from "@/graphql/index.js";
import { SQL, getTableColumns, is } from "drizzle-orm";
import {
PgBigSerial53,
Expand Down Expand Up @@ -192,10 +193,12 @@ export const safeBuildSchema = ({
}: { schema: Schema; instanceId: string }) => {
try {
const result = buildSchema({ schema, instanceId });
const graphqlSchema = buildGraphQLSchema(schema);

return {
status: "success",
...result,
graphqlSchema,
} as const;
} catch (_error) {
const buildError = new BuildError((_error as Error).message);
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/build/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { PonderRoutes } from "@/hono/index.js";
import type { Source } from "@/sync/source.js";
import { serialize } from "@/utils/serialize.js";
import { glob } from "glob";
import type { GraphQLSchema } from "graphql";
import type { Hono } from "hono";
import { type ViteDevServer, createServer } from "vite";
import { ViteNodeRunner } from "vite-node/client";
Expand Down Expand Up @@ -57,6 +58,7 @@ type BaseBuild = {
schema: Schema;
statements: SqlStatements;
namespace: string;
graphqlSchema: GraphQLSchema;
};

export type IndexingBuild = BaseBuild & {
Expand Down Expand Up @@ -748,6 +750,7 @@ const validateAndBuild = async (
schema: schema.schema,
statements: buildSchemaResult.statements,
namespace: buildSchemaResult.namespace,
graphqlSchema: buildSchemaResult.graphqlSchema,
},
};
};
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/common/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writeFileSync } from "node:fs";
import { mkdirSync, writeFileSync } from "node:fs";
import path from "node:path";
import type { Common } from "@/common/common.js";
import { type GraphQLSchema, printSchema } from "graphql";

export const ponderEnv = `// This file enables type checking and editor autocomplete for this Ponder project.
// After upgrading, you may find that changes have been made to this file.
Expand Down Expand Up @@ -31,7 +32,10 @@ declare module "@/generated" {
}
`;

export function runCodegen({ common }: { common: Common }) {
export function runCodegen({
common,
graphqlSchema,
}: { common: Common; graphqlSchema: GraphQLSchema }) {
writeFileSync(
path.join(common.options.rootDir, "ponder-env.d.ts"),
ponderEnv,
Expand All @@ -42,4 +46,16 @@ export function runCodegen({ common }: { common: Common }) {
service: "codegen",
msg: "Wrote new file at ponder-env.d.ts",
});

mkdirSync(common.options.generatedDir, { recursive: true });
writeFileSync(
path.join(common.options.generatedDir, "schema.graphql"),
printSchema(graphqlSchema),
"utf-8",
);

common.logger.debug({
service: "codegen",
msg: "Wrote new file at generated/schema.graphql",
});
}
Loading

0 comments on commit dc768bd

Please sign in to comment.