dotansimha / dotansimha/graphql-code-generator-community
import-types preset does not add import when documents is not set
- Dominant language
- TypeScript
- Stars
- 137
- Forks
- 195
- Avg merge
- 6h 20m
- Merged PRs (30d)
- 16
Description
Following scenario:
- types are generated by `typescript` plugin into `types.ts`
- resolvers are generated `typescript-resolver` into `resolvers.ts`
- referencing `types.ts` using the `import-types` preset
- the all type references re properly prefixed with `Types`
- but the import statement is not added to the top of the file
- `import * as Types from './types'
After reviewing the code of the `import-types` preset I think the issue is that the import is only added when the documents key is set:
https://github.com/dotansimha/graphql-code-generator/blob/22c1d235fb983f6b44059b8e9445ccdb85ad6bcf/packages/presets/import-types/src/index.ts#L67
**To Reproduce**
I have created a code sandbox to reproduce this issue:
- When uncommenting the documents section the import statement is properly added t `resolvers.ts`
- If its commented out no import statement is added to `resolvers.ts`
https://codesandbox.io/s/practical-sun-w9zps?file=/codegen.yml
**Additional Info**
Why separate types and resolvers:
The types in `types.ts` are shared between client and server. Hence resolver definitions shouldn't be contained in this file
Why not include `document` key in root:
Server and client execute different operations. Hence documents are provided on generator level individually.
Note:
For anyone finding this there is a workaround to get this working. One needs to use the `add` plugin to manually prepend the import statement. Please note that is in addition to using the `import-types` preset which ensures the types are all type usage is prefixed with `Types.`:
```
preset: import-types
presetConfig:
typesPath: '@shared/types'
plugins:
- add:
content: "import type * as Types from '@shared/types';"
- typescript-resolvers
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.