-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2025-02-23T17_37_17.804Z#schema_to_text_file.js
54 lines (49 loc) · 1.5 KB
/
2025-02-23T17_37_17.804Z#schema_to_text_file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use strict";
const fs = require("fs");
const path = require("path");
const helper = require("../utils/helper");
const { printSchema } = require("graphql");
/**
* @module - Migration to generate and save GraphQL schema
*/
module.exports = {
/**
* up - Generate and save GraphQL schema to a file
*
* @param {object} zendro initialized zendro object
*/
up: async (zendro) => {
try {
let Schema = helper.mergeSchemaSetScalarTypes(
path.join(__dirname, "../schemas")
);
if (!Schema) {
throw new Error("Schema is empty or undefined.");
}
const schemaString = printSchema(Schema);
const outputPath = path.join(__dirname, "../schema.graphql");
fs.writeFileSync(outputPath, schemaString, "utf8");
console.log(`GraphQL Schema saved to ${outputPath}`);
} catch (error) {
throw new Error(`Failed to generate GraphQL schema: ${error.message}`);
}
},
/**
* down - Remove the generated GraphQL schema file
*
* @param {object} zendro initialized zendro object
*/
down: async (zendro) => {
try {
const outputPath = path.join(__dirname, "../schema.graphql");
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath);
console.log(`GraphQL Schema file ${outputPath} deleted successfully.`);
} else {
console.log(`GraphQL Schema file ${outputPath} does not exist.`);
}
} catch (error) {
throw new Error(`Failed to remove GraphQL schema: ${error.message}`);
}
},
};