import { CodegenConfig } from "@graphql-codegen/cli";

import { loadEnvConfig } from "@next/env";
loadEnvConfig(process.cwd());

const wagtailBaseUrl = process.env.WAGTAIL_BASE_URL;
if (!wagtailBaseUrl) {
  throw new Error("WAGTAIL_BASE_URL is not set");
}
const graphqlEndpoint = `${wagtailBaseUrl.replace(/\/$/, "")}/api/graphql/`;

const config: CodegenConfig = {
  schema: graphqlEndpoint,
  documents: ["src/**/*.tsx", "src/**/*.ts"],
  ignoreNoDocuments: true, // for better experience with the watcher
  generates: {
    "./src/gql/": {
      preset: "client",
      presetConfig: {
        fragmentMasking: { unmaskFunctionName: "unmaskFragment" },
      },
      config: {
        scalars: {
          DateTime: "string",
          JSONString: "string",
          PositiveInt: "number",
          RichText: "string",
          UUID: "string",
        },
      },
    },
  },
};

export default config;
