Graphql-code-generator: Unneccessarily importing unused FragmentDoc if near-operation-file and noGraphQLTag are used

Created on 14 Jul 2019  路  3Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
FragmentDoc are imported without being used if used with near-operation-file preset and noGraphQLTag enabled. This happens when the fragment of a different file is being imported to be used in an operation.

Certain apps such as create-react-app enforces noUnusedLocals by default which causes TS compilation to fail.

To Reproduce

Go to this template and run yarn gg. GetProject.operation.generated.tsx is generated with 2 unused FragmentDoc: GetProject_IdFragmentDoc and GetProject_StatusFragmentDoc. These 2 fragments are separate files and are being used in the main GetProject query

https://codesandbox.io/s/gcg-14-experiments-c8l15

  1. My GraphQL schema:
type Project {
   id: ID!
   status: Status!
}

enum Status {
    INITIAL
    PENDING
    COMPLETED
}

type Query {
    project: Project!
}

schema {
    query: Query
}
  1. My GraphQL operations:
// GetProject_Id.graphql
fragment GetProject_Id on Project {
    id
}
// GetProject_Status.graphql
fragment GetProject_Status on Project {
    status
}
// GetProject.graphql
fragment GetProject_All on Project {
  id
  status
}

query GetProject {
    project {
      ...GetProject_Id
      ...GetProject_Status
      ...GetProject_All
    }
}
  1. My codegen.yml config file:
overwrite: true
schema: 'schema.graphql'
documents: './src/**/*.graphql'
generates:
  src/__generated__/types.ts:
    plugins:
      - typescript

  src/:
    preset: near-operation-file
    presetConfig:
      baseTypesPath: __generated__/types.ts
      extension: .generated.tsx
    plugins:
      - typescript-operations
      - typescript-react-apollo
    config:
      noGraphQLTag: true

Expected behavior
It should not include FragmentDoc if it's not used

Additional context
This is probably a side effect of https://github.com/dotansimha/graphql-code-generator/pull/2106?

bug plugins waiting-for-release

All 3 comments

Thanks @eddeee888 , and sorry for the delay. Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2260 , you can try it with the alpha 1.4.1-alpha-6ee432d2.57.

@dotansimha Woo hoo! It's working as expected! Cheers mate!

Fixed in 1.5.0 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zenVentzi picture zenVentzi  路  3Comments

fvisticot picture fvisticot  路  3Comments

dotansimha picture dotansimha  路  3Comments

steebchen picture steebchen  路  3Comments

rogerg93 picture rogerg93  路  3Comments