Skip to content

Add support for enumsAsConst to api-codegen-preset #2056

Open
@ryanolf

Description

@ryanolf

Overview/summary

The codgen typescript plugin has an option to generate "as const" enums, which are nice in that they don't need to be imported. So e.g. you can pass "PRODUCT" as a MetafieldOwnerType without having to import a run-time enum (but you still get type checking in modern Typescript). It would be nice if shopifyApiProject and shopifyApiTypes would support this as an option. I think this is a pretty easy change -- happy to submit a PR if it's helpful.

Right now I have to roll my own config using the preset and replicate much of what is in those function:

import { ApiType, pluckConfig, preset } from "@shopify/api-codegen-preset";
import fs from "fs";

const schemaFile = "./src/shopify/types/admin-2025-01.schema.json";
const schema = "https://shopify.dev/admin-graphql-direct-proxy/2025-01";
const schemaFileExists = fs.existsSync(schemaFile);

export default {
  // For syntax highlighting / auto-complete when writing operations
  schema,
  documents: ["./src/**/*.{js,ts,jsx,tsx}", "!dist", "!node_modules/"],
  projects: {
    default: {
      // For type extraction
      schema,
      documents: ["./src/**/*.{js,ts,jsx,tsx}", "!dist", "!node_modules"],
      extensions: {
        codegen: {
          // Enables support for `#graphql` tags, as well as `/* GraphQL */`
          pluckConfig,
          generates: {
            ...(schemaFileExists
              ? {}
              : {
                  [schemaFile]: {
                    schema,
                    plugins: ["introspection"],
                    config: { minify: true },
                  },
                }),
            "./src/shopify/types/admin.types.d.ts": {
              plugins: ["typescript"],
              config: {
                enumsAsConst: true,
              },
            },
            "./src/shopify/types/admin.generated.d.ts": {
              preset,
              presetConfig: {
                apiType: ApiType.Admin,
              },
            },
          },
        },
      },
    },
  },
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions