@fragment-dev/node-client
Version:
[Fragment](https://fragment.dev) is the Ledger API for engineers that move money. Stop wrangling payment tables, debugging balance errors and hacking together data pipelines. Start shipping the features that make a difference.
73 lines (72 loc) • 2.39 kB
JavaScript
#!/usr/bin/env node
import { writeFileSync } from "fs";
import yargs from "yargs";
import * as path from "path";
import { generate, cliError } from "@graphql-codegen/cli";
const argv = yargs(process.argv.slice(2))
.options({
outputFilename: {
type: "string",
alias: "o",
description: "Output filename",
default: "generated.ts",
},
input: {
type: "string",
alias: "i",
description: "Input filename",
demandOption: true,
},
})
.help()
.parseSync();
if (path.extname(argv.outputFilename) !== ".ts") {
throw new Error(`Output filename must have a .ts extension. You provided ${argv.outputFilename}`);
}
const allowedInputExtensions = [".graphql", ".gql", ".ts"];
if (!allowedInputExtensions.includes(path.extname(argv.input))) {
throw new Error(`Input filename must have a .graphql, .gql, or .ts extension. You provided ${path.extname(argv.input) || "<none>"}`);
}
generate({
schema: "https://api.us-west-2.fragment.dev/schema.graphql",
documents: argv.input,
config: {
scalars: {
AlphaNumericString: "string",
Date: "string",
DateTime: "string",
Int64: "string",
Int96: "string",
JSON: "Record<string, unknown>",
JSONObject: "Record<string, unknown>",
LastMoment: "string",
ParameterizedString: "string",
Period: "string",
SafeString: "string",
UTCOffset: "string",
},
gqlImport: "@fragment-dev/node-client#gql",
},
generates: {
[path.join(process.cwd(), argv.outputFilename)]: {
plugins: [
"typescript",
"typescript-operations",
"typescript-graphql-request",
{
add: {
content: "/* This file is auto-generated by @fragment-dev/node-client. Don't edit it directly. */",
},
},
],
},
},
}, false)
.then(([fileOutput]) => {
const output = fileOutput.content.replace(/import .* from 'graphql-request'/g, "import { GraphQLClient, RequestOptions } from '@fragment-dev/node-client'");
writeFileSync(fileOutput.filename, output, "utf-8");
process.exit(0);
})
.catch((error) => {
cliError(error);
});