pothos-schema-exporter
Version:
Plugin to output 'schema.graphql' from pothos to file
27 lines (26 loc) • 1.02 kB
JavaScript
import SchemaBuilder, { BasePlugin } from "@pothos/core";
import "./global-types.js";
import { printSchema } from "graphql";
import { promises as fs } from "fs";
import path from "path";
export class PothosSchemaExporterPlugin extends BasePlugin {
afterBuild(schema) {
const builder = this.builder;
const output = builder.options.pothosSchemaExporter?.output;
if (output) {
const targetDir = path.dirname(output);
fs.mkdir(targetDir, { recursive: true })
.catch(() => undefined)
.then(() => {
fs.writeFile(output, printSchema(schema));
});
}
return schema;
}
}
const pluginName = "pothosSchemaExporter";
const allowPluginReRegistration = SchemaBuilder.allowPluginReRegistration;
SchemaBuilder.allowPluginReRegistration = true;
SchemaBuilder.registerPlugin(pluginName, PothosSchemaExporterPlugin);
SchemaBuilder.allowPluginReRegistration = allowPluginReRegistration;
export default pluginName;