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

GraphQL Admin Codegen import types throwing TS error #798

Closed
5 tasks done
daweido opened this issue Apr 15, 2024 · 5 comments · Fixed by Shopify/graphql-codegen#7
Closed
5 tasks done

GraphQL Admin Codegen import types throwing TS error #798

daweido opened this issue Apr 15, 2024 · 5 comments · Fixed by Shopify/graphql-codegen#7

Comments

@daweido
Copy link

daweido commented Apr 15, 2024

Issue summary

Using @shopify/admin-api-client with codegen. However, I have this error on my admin.generated.d.ts:

A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './admin.types' instead?ts(2846)

Before opening this issue, I have:

  • Upgraded to the latest version of the relevant packages
    • @shopify/* package and version:
    • Node version:
    • Operating system:
  • Set { logger: { level: LogSeverity.Debug } } in my configuration, when applicable
  • Found a reliable way to reproduce the problem that indicates it's a problem with the package
  • Looked for similar issues in this repository
  • Checked that this isn't an issue with a Shopify API

I have followed the example given from the @shopify/admin-api-client GraphQL codegen section.

When opening my the file admin.generated.d.ts I have this error:

A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './admin.types' instead?ts(2846)

I have tried changing the config to this:

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

const config = {
  // For syntax highlighting / auto-complete when writing operations
  schema: 'https://shopify.dev/admin-graphql-direct-proxy',
  documents: ['./src/**/*.ts', '!./node_modules/**'],
  projects: {
    default: {
      // For type extraction
      schema: 'https://shopify.dev/admin-graphql-direct-proxy',
      documents: ['./src/**/*.ts', '!./node_modules/**'],
      extensions: {
        codegen: {
          // Enables support for `#graphql` tags, as well as `/* GraphQL */`
          pluckConfig,
          generates: {
            './__generated__/admin.schema.json': {
              plugins: ['introspection'],
              config: { minify: true },
            },
            './__generated__/admin.types.d.ts': {
              plugins: ['typescript', 'typescript-resolvers'],
            },
            './__generated__/admin.generated.d.ts': {
              preset: {
                ...preset,
              },
              plugins: ['typescript-operations', 'typed-document-node'],
              presetConfig: {
                apiType: ApiType.Admin,
              },
              config: {
                useTypeImports: true,
              },
            },
          },
        },
      },
    },
  },
};

export default config;

Which should work, but I still have the same problem. This does not allow me to run my application. I can change it manually but I would have to do so every time I run codegen.

Expected behavior

What do you think should happen?

Having generated files which work out of the box.

Actual behavior

What actually happens?

A Typescript error on an import.

Steps to reproduce the problem

  1. Create a GraphQL Query
  2. Copy the codegen config using the shopifyApiProject function
  3. Generate codegen
  4. Open the admin.generated.d.ts.
  5. Witness the TS error
@matteodepalo
Copy link
Contributor

Hi @daweido, thank you for opening this issue, we'll take a look 👍

@pauldraper
Copy link

Has anybody been able to use this?

I don't get how they have...but I'm open to suggestions!

@jpaylor
Copy link

jpaylor commented Apr 22, 2024

Yeah, you can temporarily get past this by manually editing the generated admin.generated.d.ts file and changing

import * as AdminTypes from './admin.types.d.ts';
to
import type * as AdminTypes from './admin.types.d.ts';

But obviously this has to be done every time the file is re-generated so its not a suitable fix.

I believe the permanent fix would be to set the config useTypeImports to true in the typescript plugin config (see https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#usetypeimports)

I could be wrong but I've had a quick look and I think this needs to be applied around here where the plugin is specified:

[`${outputDir}/${config.typesFile}`]: {
schema: schemaFileExists ? schemaFile : schema,
plugins: ['typescript'],
},

@frandiox
Copy link
Contributor

frandiox commented May 7, 2024

This probably fixes it: Shopify/graphql-codegen#7

@frandiox
Copy link
Contributor

frandiox commented May 8, 2024

Bumping a dependency to get the fix in this monorepo: #863

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 a pull request may close this issue.

5 participants