dotansimha / dotansimha/graphql-code-generator
[graphql-modules-preset] Import declarations in a namespace cannot reference a module
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
**Describe the bug**
I'm trying to use modular types and save them in `*.d.ts` files so they are not compiled:
```yaml
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
```ts
declare namespace UserModule {
import * as Types from '../types/graphql-types'
interface DefinedFields {
CurrentUser: 'id';
Query: 'currentUser';
}
export type CurrentUser = Pick
export type Query = Pick
export type CurrentUserResolvers = Pick
export type QueryResolvers = Pick
export interface Resolvers {
CurrentUser?: CurrentUserResolvers;
Query?: QueryResolvers;
}
}
```
Building an app gives errors for this file declaration:

**Expected behavior**
I'm not a TS expert, but it somehow should work. I've tried to manually move import at the top:
```ts
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`:
```yaml
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:
```ts
interface DefinedFields {
CurrentUser: 'id';
Query: 'currentUser';
}
export type CurrentUser = Pick
export type Query = Pick
export type CurrentUserResolvers = Pick
export type QueryResolvers = Pick
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 https://github.com/dotansimha/graphql-code-generator/issues/5260
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.