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

Couldn't build the generated graphql project when using some reserved words in the graphql file #2765

Closed
torycons opened this issue Jan 10, 2023 · 1 comment · Fixed by #2772
Assignees
Labels
bug Generally incorrect behavior codegen Issues related to or arising from code generation planned-next Slated to be included in the next release

Comments

@torycons
Copy link
Contributor

torycons commented Jan 10, 2023

Summary

Hello there, Thank you for hard work in the new major version of apollo-ios
But I have an issue with the new code generation.

I couldn't build the generated graphql project that there are some reserved words in the graphql files because the variable name is one of the reserved words in swift language.

For example If I create a schema with the swift reserved words and use it in my .graphql file like this

Schema File

interface Animal {
  id: ID!
  for: String!
  if: String!
}

GraphQL File

query Animal {
  animal {
    id
    for
    if
  }
}

After I run ./apollo-ios-cli generate and build my xcode project there will be an error in my generated graphql project that shown Keyword 'for' cannot be used as an identifier here.

In AnimalQuery.graphql.swift You will see in the line public var for: String { __data["for"] } that we couldn't use for as a variable name in swift. But for if variable it works correctly because it is escaped by the code generation.

AnimalQuery.graphql.swift

public struct Animal: TestApolloAlphaGraphQLGenerated.SelectionSet {
  public let __data: DataDict
  public init(data: DataDict) { __data = data }

  public static var __parentType: ParentType { TestApolloAlphaGraphQLGenerated.Interfaces.Animal }
  public static var __selections: [Selection] { [
    .field("id", ID.self),
    .field("if", String.self),
    .field("for", String.self),
  ] }

  public var id: ID { __data["id"] }
  public var `if`: String { __data["if"] }
  public var for: String { __data["for"] } // There is a problem from this line
}

Version

v1.0.5

Steps to reproduce the behavior

  1. Create A new schema with some reserved words
  2. Create A query file with reserved word fields
  3. Run cli to generate .swift files
  4. Build the project
  5. There will be an error that we can't use a reserved word as a variable name

Logs

Keyword 'for' cannot be used as an identifier here

Anything else?

I have looked into the reserved words list in the apollo-tooling project and the new one in apollo-ios. I found that list of the reserved words are not the same (there is no for keyword in the new code generation list too). I think if the reserved words list is the same, it should works correctly.
Ref:
Reserved words in the apollo-tooling project
Reserved words in the apollo-ios project (v1.0.5)

The workaround for this problem if I use the alias name for the reserved word variable. it still works really fine.
For example: Aninal.graphql

query Animal {
  animal {
    id
    if
    f: for # It works correctly if we use the alias instead of the for keyword
  }
}
@torycons torycons added bug Generally incorrect behavior needs investigation labels Jan 10, 2023
@calvincestari
Copy link
Member

Thanks for the great detailed bug report @torycons. We're planning to try have 1.0.7 out by the end of the week and should be able to have a fix for this in there.

@calvincestari calvincestari added codegen Issues related to or arising from code generation planned-next Slated to be included in the next release and removed needs investigation labels Jan 10, 2023
@calvincestari calvincestari added this to the Patch Releases (1.0.x) milestone Jan 10, 2023
@calvincestari calvincestari self-assigned this Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Generally incorrect behavior codegen Issues related to or arising from code generation planned-next Slated to be included in the next release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants