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

Using reserved keyword Type as in selection fields does not compile #3006

Closed
Nielssg opened this issue May 10, 2023 · 4 comments · Fixed by #3058
Closed

Using reserved keyword Type as in selection fields does not compile #3006

Nielssg opened this issue May 10, 2023 · 4 comments · Fixed by #3058
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

@Nielssg
Copy link

Nielssg commented May 10, 2023

Summary

Using reserved keywords for fields in a type results in the failure to compile the project. I have a type called QueueDto which has several fields, one of which is called Type. When using version 1.0.5 the Type will be generated as GraphQLEnum<Type> which is fine, however, when using any version higher than 1.0.5 (even the latest, 1.1.3), it will append it with the namespace (in my case MainApi). It will try to use GraphQLEnum<MainType.Type>, where .Type is probably some reserved Swift keyword or property. Is there any way to get around this problem, I would not like to rename the field at the server-side, is there any way to transform it for code generation or any other possible option I could try?

Part of the generated query with version 1.0.5

public var trackId: Long { __data["trackId"] }
public var type: GraphQLEnum<Type> { __data["type"] }

Part of the generated query with version 1.1.3

public var trackId: MainApi.Long { __data["trackId"] }
public var type: GraphQLEnum<MainApi.Type> { __data["type"] }

Version

1.1.3

Steps to reproduce the behavior

In order to reproduce, you need to have a schema which has a type that has a field called type. When generating the code, a swift file will be generated (in this case GetQueueQuery.graphql.swift) which will contain a reference to YourNamespace.Type. I have attached all possible files I could think of to reproduce, please let me know if there is something missing.

Schema.graphqls file

"""
A directive used by the Apollo iOS client to annotate operations or fragments that should be used exclusively for generating local cache mutations instead of as standard operations.
"""
directive @apollo_client_ios_localCacheMutation on QUERY | MUTATION | SUBSCRIPTION | FRAGMENT_DEFINITION

"""A type representing a DateTime"""
scalar DateTime

"""Long type"""
scalar Long

input PagingDtoInput {
  limit: Int!
  offset: Int!
}

type Query {
  getQueue(paging: PagingDtoInput!): [QueueDto!]!
}

type QueueDto {
  trackId: Long!
  type: Type!
}

enum Type {
  FALLBACK
  ADDED_BY_VOTE
  VOTED_FALLBACK
}

GetQueue.graphql

query GetQueue($offset: Int!, $limit: Int!) {
    getQueue(paging: { offset: $offset, limit: $limit }) {
        trackId
        type
    }
}

apollo-codegen-config-main.json

{
  "schemaName": "MainApi",
  "options": {
    "deprecatedEnumCases": "include",
    "schemaDocumentation": "include",
    "conversionStrategies": {
      "enumCases": "camelCase"
    }
  },
  "input": {
    "operationSearchPaths": [
      "graphql/main/**/*.graphql"
    ],
    "schemaSearchPaths": [
      "graphql/main/**/*.graphqls"
    ]
  },
  "output": {
    "testMocks": {
      "none": {
      }
    },
    "schemaTypes": {
      "path": "./graphql/main/graphql-main-api",
      "moduleType": {
        "swiftPackageManager": {
        }
      }
    },
    "operations": {
      "inSchemaModule": {
      }
    }
  }
}

Logs

No response

Anything else?

No response

@Nielssg Nielssg added bug Generally incorrect behavior needs investigation labels May 10, 2023
@AnthonyMDev AnthonyMDev added this to the Patch Releases (1.1.x) milestone May 11, 2023
@AnthonyMDev
Copy link
Contributor

This isn't due to a field called type, it's due to the enum in your schema being named Type.

Definitely something we need to look into a fix for.

@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 May 11, 2023
@TizianoCoroneo
Copy link
Contributor

TizianoCoroneo commented May 12, 2023

@AnthonyMDev We just run into a very similar error: our schema has an interface type called Error that causes ambiguity with the Error type in the Swift standard library when generating mocks.

@AnthonyMDev
Copy link
Contributor

Thanks for the heads up on Error being an issue too @TizianoCoroneo

@BobaFetters
Copy link
Member

The fix for this will go out with our next minor release (1.3)

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.

5 participants