dotansimha/graphql-code-generator-community

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

オープン

#120 opened on 2021/03/31

 (0 件のコメント) (5 件のリアクション) (0 人の担当者)TypeScript (195 件のフォーク)github user discovery
help wanted

Repository metrics

Stars
 (136 個のスター)
PR merge metrics
 (平均マージ 1d 11h) (30d で 28 merged PRs)

説明

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

コントリビューターガイド