import-types preset does not add import when documents is not set
#120 创建于 2021年3月31日
仓库指标
- Star
- (136 star)
- PR 合并指标
- (PR 指标待抓取)
描述
Following scenario:
- types are generated by
typescriptplugin intotypes.ts - resolvers are generated
typescript-resolverintoresolvers.ts- referencing
types.tsusing theimport-typespreset - 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'
- referencing
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.tshttps://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