Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ryanolf opened this issue Feb 3, 2025 · 1 comment
Open

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

ryanolf opened this issue Feb 3, 2025 · 1 comment

Comments

@ryanolf
Copy link

ryanolf commented Feb 3, 2025

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,
              },
            },
          },
        },
      },
    },
  },
};
@lizkenyon
Copy link
Contributor

Hi there 👋

Thanks for the flag!

Feel free to submit a PR and the team will prioritize reviewing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants