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-modules-preset] Import declarations in a namespace cannot reference a module #7671

Open
Tracked by #8296 ...
vladshcherbin opened this issue Mar 21, 2022 · 2 comments

Comments

@vladshcherbin
Copy link

vladshcherbin commented Mar 21, 2022

Describe the bug
I'm trying to use modular types and save them in *.d.ts files so they are not compiled:

schema: 'src/**/graphql/definitions.ts'
generates:
  src:
    preset: graphql-modules
    presetConfig:
      filename: types.d.ts
      baseTypesPath: types/graphql-types.d.ts
      importBaseTypesFrom: '../types/graphql-types'
      useGraphQLModules: false
    plugins:
      - typescript
      - typescript-resolvers

The result file is:

Result File
declare namespace UserModule {
  import * as Types from '../types/graphql-types'

  interface DefinedFields {
    CurrentUser: 'id';
    Query: 'currentUser';
  }

  export type CurrentUser = Pick<Types.CurrentUser, DefinedFields['CurrentUser']>
  export type Query = Pick<Types.Query, DefinedFields['Query']>

  export type CurrentUserResolvers = Pick<Types.CurrentUserResolvers, DefinedFields['CurrentUser'] | '__isTypeOf'>
  export type QueryResolvers = Pick<Types.QueryResolvers, DefinedFields['Query']>

  export interface Resolvers {
    CurrentUser?: CurrentUserResolvers;
    Query?: QueryResolvers;
  }
}

Building an app gives errors for this file declaration:

image

Expected behavior

I'm not a TS expert, but it somehow should work. I've tried to manually move import at the top:

import * as Types from '../types/graphql-types'

declare namespace UserModule {
  // ...
}

It fixes the issue with imports in declaration file.
I lose ability to use UserModule namespace without importing it but at least it's working and importing a type is okay for me.

Environment:

  • OS: macOS 10.14
  • @graphql-codegen/cli: 2.6.2
  • @graphql-codegen/graphql-modules-preset: 2.3.8
  • @graphql-codegen/typescript: 2.4.8
  • @graphql-codegen/typescript-resolvers: 2.6.1
  • NodeJS: v16.14.0

Additional context

I've also tried to skip using namespaces with encapsulateModuleTypes set to none:

schema: 'src/**/graphql/definitions.ts'
generates:
  src:
    preset: graphql-modules
    presetConfig:
      filename: types.d.ts
      baseTypesPath: types/graphql-types.d.ts
      importBaseTypesFrom: '../types/graphql-types'
      useGraphQLModules: false
      encapsulateModuleTypes: none
    plugins:
      - typescript
      - typescript-resolvers

After generation, import is gone for some reason:

interface DefinedFields {
  CurrentUser: 'id';
  Query: 'currentUser';
}

export type CurrentUser = Pick<Types.CurrentUser, DefinedFields['CurrentUser']>
export type Query = Pick<Types.Query, DefinedFields['Query']>

export type CurrentUserResolvers = Pick<Types.CurrentUserResolvers, DefinedFields['CurrentUser'] | '__isTypeOf'>
export type QueryResolvers = Pick<Types.QueryResolvers, DefinedFields['Query']>

export interface Resolvers {
  CurrentUser?: CurrentUserResolvers;
  Query?: QueryResolvers;
}

If I set filename and baseTypesPath file types to just .ts instead of .d.ts I can make it work.
However, .ts files will be compiled so I'd love to use .d.ts file types.

Related #5260

@anubhav-pandey1
Copy link

Facing similar issues

@vladshcherbin
Copy link
Author

Finding my own half year issue after another battle with graphql-code-generator is golden 😭 🔫

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