-
Notifications
You must be signed in to change notification settings - Fork 722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't add CaseIterable on Apollo-generated enum #399
Comments
I implemented a temporary workaround based on some other suggestions for similar issues. extension Gender: CaseIterable {
public static var allCases: [Gender] {
@available(*, unavailable)
func _assertExhaustive(for gender: Gender) {
switch gender {
case .male, .female, .other, .notProvided, .unknown:
break
}
}
return [.male, .female, .other, .notProvided]
}
} The nested function is marked as unavailable so you can't call it, but it will break your build if the schema changes and messes with the enum cases. As long as you keep the array of cases updated, this allows you to have your CaseIterable and ignore the unknown case. The problem with the codegen goes deeper than CaseIterable, however, and I think a proper fix would be to add an option to skip generation of unknown cases for enums altogether. Another issue I came across is that I had been initializing enum cases with the rawValue init, but if the string value isn't an exact match, you end up with an unknown("value") instead of what you expected. It should be a failable initializer and produce nil, but because of the unknown case and generated initializer, you will never get nil. In my opinion, this is bad implementation and should be fixed. |
I also believe adding caseiterable to the generated enums would be a good idea, I used the same workaround as @zigfreid5 |
I'm fairly new so I have to make sure that we're not doing anything too nuts that would make non-automatic |
Hi! Tooling PR 1441 adds generated |
* Add `CaseIterable` conformance to swift Enums Addresses apollographql/apollo-ios#399 * Don't use `as!` when the thing being unwrapped is a double-optional Addresses apollographql/apollo-ios#530 * Use multi-line string literals for operations and fragments Addresses apollographql/apollo-ios#179 * update changelog * fix swift tests failing on CI (why don't these fail locally?) * back out multiline codeblock changes * add comments with original graphql statement to operation definitions * add comments with graphql statements to fragment definitions * update operation definition in the general tests * trim out additional whitespace * update changelog * don't add newlines back in when trimming whitespace * add comments to added test cases * do not attempt to strip whitespace from things which include multi-line string literals * fix alignment sillies * slightly javascriptier code * yep, guess that needs the return after all too * remove the brackets and you can also remove the return! * nil-coalesce to `none` when pulling optional out of GraphQL map
Built-in |
Do note that if you used @zigfreid5's workaround, you'll need to remove it since it'd be a redeclaration of |
* Add `CaseIterable` conformance to swift Enums Addresses apollographql/apollo-ios#399 * Don't use `as!` when the thing being unwrapped is a double-optional Addresses apollographql/apollo-ios#530 * Use multi-line string literals for operations and fragments Addresses apollographql/apollo-ios#179 * update changelog * fix swift tests failing on CI (why don't these fail locally?) * back out multiline codeblock changes * add comments with original graphql statement to operation definitions * add comments with graphql statements to fragment definitions * update operation definition in the general tests * trim out additional whitespace * update changelog * don't add newlines back in when trimming whitespace * add comments to added test cases * do not attempt to strip whitespace from things which include multi-line string literals * fix alignment sillies * slightly javascriptier code * yep, guess that needs the return after all too * remove the brackets and you can also remove the return! * nil-coalesce to `none` when pulling optional out of GraphQL map
Hello,
We're trying to iterate on the
Enum
generated from the schema, so we've tried to addCaseIterable
to it but Swift won't allow that due to the__unknown()
case in theEnum
.Is there a way to iterate on the enums values apart from parsing the schema.json again?
Could Apollo maybe generate an AllCases constant holding all-bar-unknown value cases so we can get that rather than resort to
CaseIterable
?The text was updated successfully, but these errors were encountered: