dotansimha / dotansimha/graphql-code-generator

Typescript fragments that only include other fragments cause "Types" to be imported when they shouldn't be

Open
#2,996 4 comments 5 reactions 0 assignees View on GitHub
core presets
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.