dotansimha/graphql-code-generator-community

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

Open

#120 aperta il 31 mar 2021

Vedi su GitHub
 (0 commenti) (5 reazioni) (0 assegnatari)TypeScript (195 fork)github user discovery
help wanted

Metriche repository

Star
 (136 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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

Guida contributor