graphile / graphile/crystal

Document how to export the registry as executable and then build a schema from that in prod

Open
#2,017 0 comments 0 reactions 0 assignees View on GitHub
✨ feature 🤩 good-first-bug
Dominant language
TypeScript
Stars
12.9k
Forks
625
Avg merge
5h 23m
Merged PRs (30d)
24

Description

Doing so is slightly safer than exporting the actual schema as executable because far fewer plugins interact with the `gather` phase, and thus fewer plugins need to explicitly support `graphile-export`. This pattern can thus be used as a stop-gap until all of the plugins you are using are graphile-export compatible.

Step 1: export the registry using something like [this script](https://github.com/benjie/pgselect-join-example/blob/main/scripts/generateRegistry.mjs):

```js
import { writeFile } from "node:fs/promises";
import { buildInflection, gather } from "graphile-build";
import { resolvePresets } from "graphile-config";
import { exportValueAsString } from "graphile-export";
import preset from "./graphile.config.mjs";

const resolvedPreset = resolvePresets([preset]);
const inflection = buildInflection(resolvedPreset);
const input = await gather(resolvedPreset, { inflection });
const exportResult = await exportValueAsString("pgRegistry", input.pgRegistry, {
mode: "graphql-js",
prettier: true,
});

await writeFile(`${import.meta.dirname}/registry.mjs`, code);

await preset.pgServices[0].release();
console.log(`Registry written`);
```

Step 2: use the registry to build your schema, like in [this (undocumented) example](https://github.com/graphile/crystal/blob/1581bf2036a5b22d3eb4db43b0e6e2368634411c/graphile-build/graphile-build-pg/src/examples/NO_DATA_GATHERING.ts#L396-L400):

```js
import { buildSchema } from "graphile-build";
import preset from "./graphile.config.mjs";
import { pgRegistry } from "./registry.mjs";

/** @type {GraphileBuild.BuildInput} */
const input = { pgRegistry };
export const schema = buildSchema(config, input);
```

Step 3: serve the built schema via grafserv, e.g. using something like [this example](https://github.com/graphile/crystal/blob/1581bf2036a5b22d3eb4db43b0e6e2368634411c/grafast/dataplan-pg/serve-example-schema.js#L7-L16):

```js
import { createServer } from "node:http";
import { grafserv } from "grafserv/node";
import { createWithPgClient } from "@dataplan/pg/adaptors/pg";
import preset from "./graphile.config.mjs";
import { schema } from "./schema";

const serv = grafserv({ preset, schema });
const server = createServer();
serv.addTo(server);
server.listen(5555);
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.