@graphql-cli/init
Version:
Creates a GraphQL project using a template or GraphQL Config file for your existing project.
40 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromExistingOpenAPI = void 0;
const tslib_1 = require("tslib");
const ora_1 = tslib_1.__importDefault(require("ora"));
const js_yaml_1 = require("js-yaml");
const fs_extra_1 = require("fs-extra");
const inquirer_1 = require("inquirer");
async function fromExistingOpenAPI(context) {
const { openApiPath } = await inquirer_1.prompt([
{
type: 'input',
name: 'openApiPath',
message: 'Enter your OpenAPI schema path',
default: './swagger.json',
},
]);
const processingOpenAPISpinner = ora_1.default(`Processing OpenAPI definition: ${openApiPath}`).start();
const schemaText = fs_extra_1.readFileSync(`${openApiPath}`, 'utf8');
const parsedObject = openApiPath.endsWith('yaml') || openApiPath.endsWith('yml') ? js_yaml_1.safeLoad(schemaText) : JSON.parse(schemaText);
const datamodelPath = `${context.graphqlConfig.extensions.graphback.model}/datamodel.graphql`;
try {
const { createGraphQLSchema } = await Promise.resolve().then(() => tslib_1.__importStar(require('openapi-to-graphql')));
const { schema } = await createGraphQLSchema(parsedObject, {
strict: true,
fillEmptyResponses: true,
equivalentToMessages: false,
});
const { printSchema } = await Promise.resolve().then(() => tslib_1.__importStar(require('graphql')));
const schemaString = printSchema(schema);
await fs_extra_1.ensureFile(datamodelPath);
fs_extra_1.writeFileSync(datamodelPath, schemaString);
processingOpenAPISpinner.succeed();
}
catch (err) {
processingOpenAPISpinner.fail(`Failed to process OpenAPI definition: ${datamodelPath}. Error: ${err}`);
}
}
exports.fromExistingOpenAPI = fromExistingOpenAPI;
//# sourceMappingURL=from-open-api.js.map