Apollo-ios: GraphQL enum type with values that are reserved Swift keywords

Created on 7 Feb 2017  路  7Comments  路  Source: apollographql/apollo-ios

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.

Most helpful comment

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-codegen#33 doesn't take enum cases into account. But that should be a single line fix because the escapeIdentifierIfNeeded function has already been written.

All 7 comments

There's a closed issue on this here: https://github.com/apollographql/apollo-codegen/issues/33

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

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

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-codegen#33 doesn't take enum cases into account. But that should be a single line fix because the escapeIdentifierIfNeeded function has already been written.

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

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

@martijnwalraven you Sir are awesome! Thanks!

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeromeDms picture jeromeDms  路  5Comments

ermik picture ermik  路  4Comments

hiteshborse12 picture hiteshborse12  路  4Comments

marcoreni picture marcoreni  路  3Comments

StanislavCekunov picture StanislavCekunov  路  3Comments