Skip to content
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

GraphQL enum type with values that are reserved Swift keywords #53

Closed
attheodo opened this issue Feb 7, 2017 · 7 comments
Closed

GraphQL enum type with values that are reserved Swift keywords #53

attheodo opened this issue Feb 7, 2017 · 7 comments

Comments

@attheodo
Copy link

attheodo commented Feb 7, 2017

That's an interesting case. Our GraphQL backend has an AlbumPrivacies enum with values PRIVATE and PUBLIC.

Reasonably, Apollo tries to generate the respective Swift enum in the following way:

/// The privacy modes for an album.
public enum AlbumPrivacies: String {
  case public = "PUBLIC" /// Public albums can be joined and viewed by anyone.
  case private = "PRIVATE" /// Private albums can be joined and viewed only with an invitation.
}

Unfortunately, public and private are of course reserved Swift keywords so unfortunately the generated code is broken.

I am not sure what's the most appropriate way to mitigate this on your side. Maybe check whether an enum value is a reserved keyword and prefix/suffix something to its name declaration? So in that case it could be publicCase and privateCase or something.

Just thinking out loud here.

@MrAlek
Copy link
Contributor

MrAlek commented Feb 7, 2017

There's a closed issue on this here: apollographql/apollo-tooling#33

Edit: Seems to work for property names but maybe was missed for enum cases

@attheodo
Copy link
Author

attheodo commented Feb 7, 2017

@MrAlek I guess it's missed. I am on apollo-codegen 0.10.4 and doesn't seem to work.

@martijnwalraven
Copy link
Contributor

Ah, that should be pretty easy to fix actually! Swift allows you to escape identifiers by using backticks. So in this case, you'd want the output to be:

/// The privacy modes for an album.
public enum AlbumPrivacies: String {
  case `public` = "PUBLIC" /// Public albums can be joined and viewed by anyone.
  case `private` = "PRIVATE" /// Private albums can be joined and viewed only with an invitation.
}

Unfortunately, it seems apollographql/apollo-tooling#33 doesn't take enum cases into account. But that should be a single line fix because the escapeIdentifierIfNeeded function has already been written.

@attheodo
Copy link
Author

attheodo commented Feb 7, 2017

@martijnwalraven any estimates on when the fix could make it to apollo-codegen ?

@martijnwalraven
Copy link
Contributor

@attheodo: Working on it right now, give me 15 min :)

@attheodo
Copy link
Author

attheodo commented Feb 7, 2017

@martijnwalraven you Sir are awesome! Thanks!

@martijnwalraven
Copy link
Contributor

@attheodo: Should be fixed in apollo-codegen 0.10.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants