dotansimha / dotansimha/graphql-code-generator-community
[@graphql-codegen/flutter-freezed] Generated enums are not used in freezed models, advanced codegen configuration is not possible
- Dominant language
- TypeScript
- Stars
- 137
- Forks
- 195
- Avg merge
- 6h 20m
- Merged PRs (30d)
- 16
Description
### Which packages are impacted by your issue?
@graphql-codegen/flutter-freezed
### Describe the bug
I have the following schema:
```
enum REAL_ESTATE_TYPE {
APARTMENT
DETACHED_HOUSE
APARTMENT_BUILDING
PROPERTY
COMMERCIAL
}
type RealEstate{
type: REAL_ESTATE_TYPE!
monthly_rent: Int!
property_value: Int!
year_of_purchase: Int!
postal_code: String!
}
```
All of our enums are named in all caps and snake case. The enum gets generated correctly and looks like this:
```
enum RealEstateType {
@JsonKey(name: 'APARTMENT')
apartment,
@JsonKey(name: 'APARTMENT_BUILDING')
apartmentBuilding,
@JsonKey(name: 'COMMERCIAL')
commercial,
@JsonKey(name: 'DETACHED_HOUSE')
detachedHouse,
@JsonKey(name: 'PROPERTY')
property,
}
```
Unfortunately this enum is not used and reflected in the resulting class:
```
@Freezed(
copyWith: true,
)
class RealEstate with _$RealEstate {
const RealEstate._();
const factory RealEstate({
@JsonKey(name: 'monthly_rent')
required int monthlyRent,
@JsonKey(name: 'postal_code')
required String postalCode,
@JsonKey(name: 'property_value')
required int propertyValue,
required REAL_ESTATE_TYPE type,
@JsonKey(name: 'year_of_purchase')
required int yearOfPurchase,
}) = _RealEstate;
factory RealEstate.fromJson(Map json) => _$RealEstateFromJson(json);
}
```
### Your Example Website or App
https://stackblitz.com/edit/github-wt29hz?file=schema.graphql
### Steps to Reproduce the Bug or Issue
`npm run generate`
### Expected behavior
I would like to do more customization via the `codegen.ts` file but I'm unable to do that since the types used for advanced configuration options are not exported by the `flutter-freezed` package itself as mentioned in this [issue](https://github.com/dotansimha/graphql-code-generator-community/issues/319). Because of this I'm not able to used the `Config` type that is used in the [guide](https://the-guild.dev/graphql/codegen/docs/guides/flutter-freezed#identifying-the-building-blocks).
How can I customize the naming of enums in my GraphQL schema and how should this be configured in my `codegen.ts`? Should the building block `APPLIES_ON_ENUM` be used?
### Screenshots or Videos
_No response_
### Platform
"dependencies": {
"graphql": "^16.2.0"
},
"devDependencies": {
"@graphql-codegen/cli": "^3.3.0",
"@graphql-codegen/flutter-freezed": "^3.0.2",
"typescript": "^4.8.4"
}
### Codegen Config File
```
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
generates: {
'entities_gen.dart': {
plugins: {
'flutter-freezed': {
copyWith: true,
},
},
},
},
};
export default config;
```
### Additional context
I would really appreciate a small example on how to configure this correctly in my codegen.ts file. Thank you!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with schema.graphql and codegen.ts, then run npm run generate to inspect entities_gen.dart. Trace the @graphql-codegen/flutter-freezed configuration and generated enum/model handling, including the Config type referenced in the guide. Done means advanced configuration types are available and the generated RealEstate model uses the generated RealEstateType enum with the requested naming customization.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter, graphql, typescript
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100