Graphql-code-generator: Typescript fragments that only include other fragments cause "Types" to be imported when they shouldn't be

Created on 22 Nov 2019  路  1Comment  路  Source: dotansimha/graphql-code-generator

When a fragment is created that only includes other fragments, like the following

export const DetailedFileAttachmentFields = gql`
fragment MyFragment on MyType {
   ...SomeOtherFragment
   aNestedField {
       ...SomeOtherFragment
   }
}
${SomeOtherFragment}`

The generated file includes import * as Types from '...'; when it shouldn't, as the generated fragment does not use Types. This causes an issue with Typescript and noUnusedLocals

Packages I'm using are as follows:

"@graphql-codegen/add": "^1.8.3",
    "@graphql-codegen/cli": "^1.8.3",
    "@graphql-codegen/fragment-matcher": "1.8.3",
    "@graphql-codegen/import-types-preset": "^1.8.3",
    "@graphql-codegen/introspection": "1.8.3",
    "@graphql-codegen/near-operation-file-preset": "1.8.4-alpha-11f86a7d.48+11f86a7d",
    "@graphql-codegen/typescript": "1.8.3",
    "@graphql-codegen/typescript-operations": "^1.8.3",
    "@graphql-codegen/typescript-react-apollo": "1.8.3",

I'm using an alpha version for the fix it includes for the issue here https://github.com/dotansimha/graphql-code-generator/issues/2912

codegen.yml looks like

overwrite: true
schema: './node_modules/my/path/*.graphql'
documents: './graphql/**/*.gql.ts'
generates:
  graphql/:
    preset: near-operation-file
    presetConfig:
      extension: .generated.tsx
      baseTypesPath: '~my/package'
      folder: generated
    plugins:
      - add: '/* THIS FILE WAS GENERATED BY GRAPHQL CODE GENERATOR. DO NOT EDIT. */'
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      documentMode: 'external'
      importDocumentNodeExternallyFrom: 'near-operation-file'
      withHooks: true
      withHOC: true
      reactApolloVersion: 3
hooks:
  afterOneFileWrite:
    - prettier --write
bug presets

Most helpful comment

we're facing the same issue with fragments 馃槥

>All comments

we're facing the same issue with fragments 馃槥

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SimenB picture SimenB  路  3Comments

zenVentzi picture zenVentzi  路  3Comments

NickClark picture NickClark  路  3Comments

iamdanthedev picture iamdanthedev  路  3Comments

mszczepanczyk picture mszczepanczyk  路  3Comments