Skip to content

Allow generating .ts files with enums #9

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

Merged
merged 5 commits into from
May 27, 2024
Merged

Allow generating .ts files with enums #9

merged 5 commits into from
May 27, 2024

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented May 16, 2024

This should enable using enums in the generated file when its extension is .ts instead of .d.ts.

@frandiox frandiox marked this pull request as ready for review May 24, 2024 00:33

This comment has been minimized.

@frandiox frandiox requested a review from paulomarg May 24, 2024 00:33
Copy link

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied the changes to an existing app, and was able to use .ts as expected!

@frandiox frandiox merged commit 16e8817 into main May 27, 2024
6 checks passed
@frandiox frandiox deleted the fd-enums branch May 27, 2024 03:28
@bakura10
Copy link

bakura10 commented Feb 3, 2025

Hello,

I am experiencing this issue as enum can't be imported for d.ts files. Is there a way to force this tool to generate a .ts file directly, instead of d.ts ? Renaming is an option but you have to do it all the time the file is re-generated.

@frandiox
Copy link
Contributor Author

frandiox commented Feb 3, 2025

I am experiencing this issue as enum can't be imported for d.ts files. Is there a way to force this tool to generate a .ts file directly, instead of d.ts ? Renaming is an option but you have to do it all the time the file is re-generated.

When you configure this plugin, you can specify a name for the generated file. You just need to do it once and it should be always generated with that name:

export default {
    overwrite: true,
    pluckConfig,
    generates: {
        'my-api.generated.ts': {
           preset, schema, documents: [...]
        }
    }
}

@bakura10
Copy link

bakura10 commented Feb 7, 2025

Hello @frandiox ,

Sorry to be a mess here, but for the hell of me I can't figure out how to configure that with Shopify recommendation here: https://shopify.dev/docs/api/shopify-app-remix/v2/guide-graphql-types

My .graphqlrc.ts file works correctly, but I can't figure out how to put the generates properly to override the default:

import {shopifyApiProject, ApiType} from '@shopify/api-codegen-preset';

export default {
  // For syntax highlighting / auto-complete when writing operations
  schema: 'https://shopify.dev/admin-graphql-direct-proxy/unstable',
  documents: ['./app/**/*.{js,ts,jsx,tsx}'],
  projects: {
    // To produce variable / return types for Admin API operations
    default: shopifyApiProject({
      apiType: ApiType.Admin,
      apiVersion: 'unstable',
      documents: ['./app/**/*.{js,ts,jsx,tsx}'],
      outputDir: './app/types',
    }),
  },
};

It should definitely be documented somewhere in the official Shopify documentation.

@frandiox
Copy link
Contributor Author

frandiox commented Feb 7, 2025

@bakura10 Checking the source code (1, 2, 3), I believe you need to pass declarations: false to shopifyApiProject. Let me know if that works.

@bakura10
Copy link

bakura10 commented Feb 7, 2025

Great catch! This is working! Thanks a lot.

@frandiox
Copy link
Contributor Author

@bakura10 Hey! I’m curious about what led you to bring this up in this repo instead of the preset's repo, which has some documentation on declarations. I’m just trying to get a better understanding of how you got here. Did the mention of the "graphql-codegen" script in the template docs influence your choice, or was it something else? Thanks for clarifying!

@bakura10
Copy link

Hi @frandiox ,

When I searched "Shopify graphql codegen" on Google, this repo came first :D.

@bakura10
Copy link

Helo @frandiox ,

I am currently creating a library to interact with metafields/metaobjects, and I am still having quite a lot of issues with enums. I've found that there is a enumsAsTypes which converts enums as types, but it seems to not be usable in the context of the Shopify preset. Is there a way to add this options? The issue i'm facing with enums is that it forces to download the whole admin.types file whenever I am using them.

@bakura10
Copy link

For reference, I've been able to do it by not using the preset, with this config:

export default {
  // For syntax highlighting / auto-complete when writing operations
  schema: 'https://shopify.dev/admin-graphql-direct-proxy/2025-04',
  documents: ['./src/**/*.{js,ts,jsx,tsx}'],
  projects: {
    default: {
      // For type extraction
      schema: 'https://shopify.dev/admin-graphql-direct-proxy/2025-04',
      extensions: {
        codegen: {
          generates: {
            './src/types/admin.types.d.ts': {
              plugins: ['typescript'],
              config: { enumsAsTypes: true }
            }
          },
        },
      },
    },
  },
};

@frandiox
Copy link
Contributor Author

We pass your config to the tool here: https://github.com/Shopify/graphql-codegen/blob/main/src/preset.ts#L134
But unsure if that option is compatible with the rest of the default options 🤔

I'd suggest opening a new issue so that the new maintainers can check 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

Successfully merging this pull request may close these issues.

3 participants