graphql-code-generator
graphql-code-generator copied to clipboard
[flutter-freezed] Can't handle multiple line comments
- Wrong part code generated
Expected
part 'app_models.freezed.dart';
Actualpart 'app_models.dart';
- Can't handle multiline comments
-
@JsonKey
in enums missing"
and,
end of the line Expected@JsonKey(name: "ACCOUNT_NOT_CONFIRMED") account_not_confirmed,
Actual@JsonKey(name: ACCOUNT_NOT_CONFIRMED) account_not_confirmed
- Some key words generated in enums (in,is,do,void,default) but it's depends on what graphql server gives https://demo.saleor.io/graphql/
cc @Parables
@charlypoly @endigo, I know of these issues because I am also using the plugin for my project. However, I am currently occupied with other activities that is why I haven't fixed these issues.
@endigo, the plugin is quite simple, I would be very grateful if you could submit a PR for these fixes on the fluter-freezed-v3.0.0 branch.
As a guide, find the related codes to the issues you mentioned here
- addFreezedImportStatements
- Every block generates its own comments, freezed Class block, factory block, parameter block
- @JsonKey
- Not sure I understand what you said here, please elaborate on that for me
Thank you for fishing out these issues. Your contribution is greatly appreciated 🙏
It's been 22 days since this issue was posted and I am not happy I haven't been able to fix it so I will squeeze some few hours in the mornings to attempt fixing the plugin.
@charlypoly, what have I missed while I was away.... any breaking changes I should be aware of?
It's been 22 days since this issue was posted and I am not happy I haven't been able to fix it so I will squeeze some few hours in the mornings to attempt fixing the plugin.
@charlypoly, what have I missed while I was away.... any breaking changes I should be aware of?
@Parables, don't worry, no hurry!
No, we didn't introduce breaking changes in the core packages, you are safe.
Hey @Parables, just ran the plugin but it seems broken. Also cannot directly use the npm dep from github since it's a monorepo.
When do you think the fix version will be released?
Same problem! I'd like to see internal code, where can I find them?
@charlypoly @endigo, I know of these issues because I am also using the plugin for my project. However, I am currently occupied with other activities that is why I haven't fixed these issues.
@endigo, the plugin is quite simple, I would be very grateful if you could submit a PR for these fixes on the fluter-freezed-v3.0.0 branch.
As a guide, find the related codes to the issues you mentioned here
- addFreezedImportStatements
- Every block generates its own comments, freezed Class block, factory block, parameter block
- @JsonKey
- Not sure I understand what you said here, please elaborate on that for me
Thank you for fishing out these issues. Your contribution is greatly appreciated 🙏
@auaicn please find the source of these issues in the comment quoted above.
I'm currently under lots of pressure to meet multiple deadlines so I will be very grateful if you can help me out with a PR for these fixes
I handled some issues on my branch and also updated tests.
- [x] 1. addFreezedImportStatements
- [ ] 2. Every block generates its own comments, freezed Class block, factory block, parameter block
- [x] 3. @JsonKey
- [ ] 4. Not sure I understand what you said here, please elaborate on that for me
Regarding Issue numbered 4 by @endigo
Some key words generated in enums (in,is,do,void,default) but it's depends on what graphql server gives
What @endigo says might be explained with below example
@unfreezed
class NestedIntFilter with _$NestedIntFilter {
const NestedIntFilter._();
const factory NestedIntFilter({
int? equals,
int? gt,
int? gte,
- List<int>? in, // current result. It's as expected, but causes error
int? lt,
int? lte,
NestedIntFilter? not,
List<int>? notIn,
}) = _NestedIntFilter;
factory NestedIntFilter.fromJson(Map<String, Object?> json) => _NestedIntFilterFromJson(json);
}
There's a lot of reserved keyword on dart language. see here dart language keywords
abstract
,else
,import
,show
,as
,enum
,in
,static
,assert
,export
,interface
,super
,async
,extends
,is
,switch
,await
,extension
,late
,sync
,break
,external
,library
,this
,case
,factory
,mixin
,throw
,catch
,false
,new
,true
,class
,final
,null
,try
,const
,finally
,on
,typedef
,continue
,for
,operator
,var
,
Is it okay if I append _(underscore) after like this?
@unfreezed
class NestedIntFilter with _$NestedIntFilter {
const NestedIntFilter._();
const factory NestedIntFilter({
int? equals,
int? gt,
int? gte,
- List<int>? in, // current result. It's as expected, but causes error
+ List<int>? in_,
int? lt,
int? lte,
NestedIntFilter? not,
List<int>? notIn,
}) = _NestedIntFilter;
factory NestedIntFilter.fromJson(Map<String, Object?> json) => _NestedIntFilterFromJson(json);
}
If there's better solution, let me know.
Another handled Issues
@unfreezed
class NestedIntFilter with _$NestedIntFilter {
const NestedIntFilter._();
const factory NestedIntFilter({
int? equals,
int? gt,
int? gte,
List<int>? in,
int? lt,
int? lte,
NestedIntFilter? not,
List<int>? notIn,
}) = _NestedIntFilter;
- factory NestedIntFilter.fromJson(Map<String, Object?> json) => _NestedIntFilterFromJson(json); // current result, but not follows freezed spec
+ factory NestedIntFilter.fromJson(Map<String, Object?> json) => _$NestedIntFilterFromJson(json); // follows current freezed spec
}
@Parables It would be appreciate if you reply to me!
@Parables It would be appreciated if you reply to me!
I saw your PR as soon as you sent it but the power was out for 2 days and my battery drained before I could review your changes. Sincere apologies for not replying earlier.
I value your effort and dedication, most importantly your time which is something I used to have a lot at my disposal but things took a sudden turn and right now, I am torn in between final year school projects, work projects and other in-production side gigs.
It is part of my routine to check my GitHub notifications before I begin the day's work so please give me up to a day to respond.
Once again, I am very grateful to you @auaicn
Regarding Issue numbered 4 by @endigo
Thanks for clarifying this issue for me @auaicn .
Appending or prepending an _
to the property will also affect the JSON serialization, the GraphQL server would expect a in
property only to get a in_
or a _in
property.
@munificent shared this same idea in this open issue.
So we are torn between modifying our GraphQL schema to use keyword-friendly identifiers or waiting for the Dart team to find a way to escape Dart keywords to be used as identifiers.
Using @JsonKey
seems to be the only viable option here:
@unfreezed
class NestedIntFilter with _$NestedIntFilter {
const NestedIntFilter._();
const factory NestedIntFilter({
int? equals,
int? gt,
int? gte,
- List<int>? in, // current result. It's as expected, but causes error
+ @JsonKey(name: "in") List<int>? in_,
+ // OR
+ @JsonKey(name: "in") List<int>? someOtherName,
int? lt,
int? lte,
NestedIntFilter? not,
List<int>? notIn,
}) = _NestedIntFilter;
factory NestedIntFilter.fromJson(Map<String, Object?> json) => _$NestedIntFilterFromJson(json); // follows current freezed spec
}
Using someOtherName
will only affect your Dart code which in my opinion is better than breaking your GraphQL Schema and all the other clients using your GraphQL API just because of a Dart problem that could be fixed in a Dart codebase.
I hope this helps clears out any confusion. I am open to further discussions so please share your thoughts with me.
PS:
You can specify the @JsonKey
annotation/decorator using the customDecorator in your config either globally using the globalFreezedConfig
or for a specific GraphQL Type using the typeSpecificFreezedConfig
@Parables
Thanks for the guidance to avoid dart reserved-words. I'll try the solution
@auaicn What do you have in mind? Implement a way to detect a Dart keyword and automatically wrap it in @JsonKey
?
If so, how would you choose the name for the field/parameter?
Since this is a problem-specific issue, I suggest we can add some notes to the guide something like a FAQ on how to address such problems.
That's why this plugin is configurable so that the user can fine-tune the output as preferred
I think it would be better if we can give option which can be set in codegen_yml
file. Like this.
yes, if parameter name is modified, it seems @JsonKey
should be set with original value
/**
* @name avoidDartKeywords
* @description avoid converting to dart-language reserved keywords such as `void`, `in`. It appends suffix which can be set by changing `dartKeywordEscapeSuffix` value
* @default false
* @see_also [dartKeywordEscapeSuffix]
*
* @exampleMarkdown
* ```yaml
* generates:
* flutter_app/lib/data/models/app_models.dart
* plugins:
* - flutter-freezed
* config:
* avoidDartKeywords: true
*
* ```
*/
avoidDartKeywords?: boolean;
/**
* @name dartKeywordEscapeSuffix
* @description
* @default "_"
* @see_also [avoidDartKeywords]
*
* ```yaml
* generates:
* flutter_app/lib/data/models/app_models.dart
* plugins:
* - flutter-freezed
* config:
* dartKeywordEscapeSuffix: "_"
*
* ```
*/
dartKeywordEscapeSuffix?: string;
@auaicn while that would work and address this problem, I doubt we should implement it.
According to Gregory Young, the software must do 99% of the work, the remaining 1% should be done by the human. Eliminating the human factor and making the software do all the work can result in some pretty bugged software.
This issue can simply be addressed with the current version of the plugin by specifying the following in the config:
Warning: The following snippets might contain errors and may be improperly formatted as it was typed out directly in this comment. Please adjust the code snippet accordingly
typeSpecificFreezedConfig: {
NestedIntFilter: {
fields: {
in: {
customDecorators: { # custom are used just as it given
'@JsonKey' : {
mapsToFreezedAs: 'custom', # custom are used just as it given
applyOn: ['class_factory_parameter', 'union_factory_parameter'],
arguments: ["name: 'in' "],
},
}
}
}
}
}
This config should produce an output as below. Part of the output has been cropped for brevity
@unfreezed
class NestedIntFilter with _$NestedIntFilter {
const NestedIntFilter._();
const factory NestedIntFilter({
...
@JsonKey(name: "in") List<int>? in_,
...
}) = _NestedIntFilter;
}
Okay: I take it back, I missed the fact that this solution doesn't fix the issue. Moreover, it can be quite a boilerplate if you have more GraphQL types using Dart-valid keywords, specifying a config for each field seems unreasonably simple.
@auaicn Please go ahead with your proposal. Let me know what you come up with.
Once again, Sorry for disagreeing with your proposal initially. Your solution is the best simple fix for this issue
@auaicn @endigo @gabsn I would like to notify you that this issue is being addressed in the new community repo found on flutter-freezed-v3.0.0-fix.
If you have any issues, please forward is to the new repo instead.
I am personally disappointed for not being able to manage this plugin but I would like to thank you all for patience.
@endigo thanks for using this plugin and pointing out this issue @auaicn thanks for your help and time. @charlypoly thanks for your dedication
@Parables, please know that we will migrate all community plugin-related issues to the new repo by the end of the week 😊
@charlypoly @endigo @gabsn @auaicn All the issues mentioned here have been fixed.
There was an urgent need to rewrite this plugin again and I just couldn't squeeze in any time until now.
This plugin started when I saw the myzod plugin and I decided to follow his steps to create this plugin. I also borrow a lot of ideas from existing plugins.
This is the fourth iteration of the plugin and there are lots of ~~changes~~ improvements compared to the previous versions so I guess this would be version 3.x or 4.x maybe @charlypoly
The changes will be available on the community-repo so be sure to check it out in the morning.
And please close this issue as completed @charlypoly. Thank you