dotansimha/graphql-code-generator-community

import-types preset does not add import when documents is not set

Open

#120 创建于 2021年3月31日

在 GitHub 查看
 (0 评论) (5 反应) (0 负责人)TypeScript (195 fork)github user discovery
help wanted

仓库指标

Star
 (136 star)
PR 合并指标
 (PR 指标待抓取)

描述

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:

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

贡献者指南