@fragment-dev/cli
Version:
164 lines (161 loc) • 4.98 kB
JavaScript
import {
generateQueriesFileContent,
generateQueryFiles,
schemaToEntryDefinitions,
validateOutputName
} from "./chunk-N62SFGWS.js";
import {
Schema,
parseWithError
} from "./chunk-66MREU45.js";
import {
formatValidationErrors,
logValidationErrors
} from "./chunk-AY76YKC7.js";
import {
BadRequestError
} from "./chunk-CFMJRPDM.js";
import {
FragmentCommand,
require_lib
} from "./chunk-UD22EIAP.js";
import {
getParsedSchema
} from "./chunk-UDU5PBTV.js";
import {
require_source
} from "./chunk-M5OAS5QZ.js";
import {
__toESM,
init_cjs_shims
} from "./chunk-7GH3YGSC.js";
// src/commands/gen-graphql.ts
init_cjs_shims();
var import_chalk = __toESM(require_source(), 1);
var import_core = __toESM(require_lib(), 1);
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
var GenGraphQL = class _GenGraphQL extends FragmentCommand {
static {
this.summary = "Generate GraphQL queries from your Schema for your SDK.";
}
static {
this.description = `This command generates a GraphQL query for each entry type in your ledger Schema.
Use this command with Fragment SDKs to create type-safe code to update your ledger.`;
}
static {
this.examples = [
{
description: `Output the GraphQL queries from the schema file at ${import_chalk.default.yellowBright(
"<path>"
)}.`,
command: "<%= config.bin %> <%= command.id %> --path <path>"
},
{
description: "Output the GraphQL queries to a specific output path.",
command: "<%= config.bin %> <%= command.id %> --path <path> --output <output path>"
},
{
description: "Output each GraphQL query in a separate file.",
command: "<%= config.bin %> <%= command.id %> --path <path> --output <output path> --output-file-per-query"
},
{
description: "Include the standard GraphQL queries and output each in a separate file.",
command: "<%= config.bin %> <%= command.id %> --path <path> --output <output path> --output-file-per-query --include-standard-queries"
}
];
}
static {
this.help = "Generate GraphQL queries from your Schema for your SDK.";
}
static {
this.flags = {
path: import_core.Flags.string({
char: "p",
description: "The path to the schema file.",
required: true
}),
output: import_core.Flags.string({
char: "o",
required: true,
description: "The output path for the generated file(s). Must end with .graphql or .gql, unless --output-file-per-query is set. If set, the output path must be a directory."
}),
"output-file-per-query": import_core.Flags.boolean({
required: false,
default: false,
description: "Output each query in a separate file instead of a single file."
}),
"include-standard-queries": import_core.Flags.boolean({
required: false,
default: false,
description: "Include the set of standard queries in the output. This is required only if you are not using the Fragment SDK."
})
};
}
async run() {
const { flags } = await this.parse(_GenGraphQL);
const { path, output } = flags;
try {
validateOutputName({
output,
outputFilePerQuery: flags["output-file-per-query"]
});
} catch (e) {
const err = e;
this.logToStderr(err.message);
this.exit(1);
}
const schema = getParsedSchema({ command: this, schemaPath: path });
let validatedSchema;
try {
validatedSchema = parseWithError(
Schema,
schema,
BadRequestError,
{
message: `Invalid schema provided`
}
);
} catch (e) {
this.logToStderr(import_chalk.default.red("Failed to validate schema:"));
const { errors } = formatValidationErrors(e);
if (errors.length > 0) {
logValidationErrors(this, errors);
}
this.logToStderr(`
${import_chalk.default.gray("Schema file:")} ${path}`);
this.exit(1);
}
try {
const entryDefinitions = schemaToEntryDefinitions({
schema: validatedSchema
});
const generateQueryParams = {
definitions: entryDefinitions,
includeStandardQueries: flags["include-standard-queries"]
};
if (flags["output-file-per-query"]) {
const files = generateQueryFiles(generateQueryParams);
await Promise.all(
files.map(
(file) => writeFile(join(output, file.fileName), file.content)
)
);
} else {
const content = generateQueriesFileContent(generateQueryParams);
await writeFile(output, content);
}
} catch (e) {
this.logToStderr(`Failed to generate GraphQL queries:`);
if (e instanceof Error) {
this.logToStderr(` ${e.message}`);
} else {
this.logToStderr(e);
}
this.exit(1);
}
}
};
export {
GenGraphQL
};