Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
benjie opened this issue Apr 11, 2024 · 0 comments

Comments

@benjie
Copy link
Member

benjie commented Apr 11, 2024

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:

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:

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:

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); 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📝 Docs Improvements
Development

No branches or pull requests

1 participant