dotansimha / dotansimha/graphql-code-generator
Increased query complexity when using fragment colocation
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Which packages are impacted by your issue?
@graphql-codegen/core
### Describe the bug
if I have the following query
```
query post {
id
name
status
description
createdAt
}
```
and extract some fields to a fragment like this
```
query post {
id
...Fragment1
...Fragment2
}
fragment Fragment1 on Post {
name
status
}
fragment Fragment2 on Post {
name
status
}
```
the first query has complexity = 6 (1 for the query, and 5 fields)
but the second query also has complexity = 6 (1 for the query, 1 for id, 2 for fields in fragment1 and 2 for fields fragment 2)
I have my server setup with a maximum query complexity, so the request fails with the following error when I add too many fragments:
```
maximum query complexity exceeded x > y
```
my question is, is there a way to remove fragment definitions from the request, so duplicate fields are deduplicated.
what I want is to transform the second query to be:
```
query post {
id
name
status
}
```
I think Relay with the relay compiler achieves something similar
I'm using graphql-codegen with the client-preset, here is my config
```
const config: CodegenConfig = {
schema: '../schemas/graphql/**/*.graphqls',
documents: [
'./lib/**/*.{ts,tsx}',
'./pages/**/*.{ts,tsx}',
'./components/**/*.{ts,tsx}',
],
generates: {
'./lib/generated/': {
preset: 'client',
plugins: [],
config: {
useTypeImports: true,
inlineFragmentTypes: 'mask',
},
presetConfig: {
fragmentMasking: false,
customDirectives: {
apolloUnmask: true,
},
},
},
},
ignoreNoDocuments: true,
};
```
### Your Example Website or App
-
### Steps to Reproduce the Bug or Issue
1. create a query with no fragments, only fields
2. observer the query complexity
3. extract some fields to fragments
5. notice the query complexity increases
### Expected behavior
I need a way to remove fragment definitions from the request and only include the fields, same way as the relay compiler
### Screenshots or Videos
_No response_
### Platform
- OS: macOS
- NodeJS: [e.g. 18.5.0]
- `graphql` version: 16.9.0
- "@graphql-codegen/cli": "^5.0.3",
- "@graphql-codegen/client-preset": "^4.5.0",
### Codegen Config File
```
import { type CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: '../schemas/graphql/**/*.graphqls',
documents: [
'./lib/**/*.{ts,tsx}',
'./pages/**/*.{ts,tsx}',
'./components/**/*.{ts,tsx}',
],
generates: {
'./lib/generated/': {
preset: 'client',
plugins: [],
config: {
useTypeImports: true,
inlineFragmentTypes: 'mask',
},
presetConfig: {
fragmentMasking: false,
customDirectives: {
apolloUnmask: true,
},
},
},
},
ignoreNoDocuments: true,
};
export default config;
```
### Additional context
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.