import { execSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import { buildSchema, lexicographicSortSchema, printSchema } from "graphql";

const url = process.env.VITE_TAILOR_APP_URL;
const schemaPath = "src/graphql/generated/schema.graphql";

execSync(`pnpm gql-tada generate schema "${url}/query"`);

const schema = buildSchema(readFileSync(schemaPath, "utf-8"));
const sorted = lexicographicSortSchema(schema);
writeFileSync(schemaPath, printSchema(sorted) + "\n");

execSync("pnpm gql-tada generate output");
