-
Notifications
You must be signed in to change notification settings - Fork 730
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
Generates conflicting types for fields of singular and plural names #2850
Comments
If the schema + operation passes GraphQL validation then we need to support it and generate correct Swift code, unless in the rare case that the validation is actually wrong. That simple query above should yield types named It would be better if you can you share a standalone sample app demonstrating the issue; schema + operation + generated Swift code that doesn't compile. |
Thanks for the reply. I will try to get you an example next week. To clarify the example above: The types for |
I attached an example of the structure that causes the issue described. However, I does generate the types named the way you said |
I found the root issue. My previous example was not close enough to our internal schema. I updated the original issue post accordingly. The problem is that we have two properties, called From the example project: type Container {
value: Value
values: [Value]
}
type Value {
propertyA: String!
propertyB: String!
propertyC: String!
propertyD: String!
} containers {
value {
propertyA
propertyB
propertyC
propertyD
}
values { # <- this will generate another "Value" type, not a "Values" type.
propertyA
propertyC
}
} The workaround is to assign local aliases. Is this intended behaviour? |
The type being named One of the guiding principles we have for codegen is what-you-see-is-what-you-get where the user is able to influence the shape of the generated code directly through the operation with as little 'magic' as possible. The ability to use an alias for resolving issues like this is intended but not very desirable, and IMO this issue is stretching that principle because you need to be aware of the implementation detail (singularized field names as type names) in order to understand how to fix it. Apollo Kotlin is likely doing some conflict checking and working around it although I don't think |
I agree with that position and until now, I didn't have any problems with the type names generated by Apollo. Using the singular name makes sense in all other cases, so it should be kept. However, this case is especially frustrating because it leaves the developer with a "ambiguous type reference" compiler error and no clear way to resolve the issue. Even then, you need to fix it after every code generation. For folks that have CI generate their code (to be sure not to have forgotten it) this will break their build by overwriting manual changes. Anyway, I have the following solutions as input to the discussion:
|
@AnthonyMDev and I discussed this issue today and we came to the conclusion that using a GraphQL field alias is the preferred way to resolve this kind of issue; it places control with the user in shaping the outcome rather than an arbitrary number tacked onto the end of the conflicting type. That said though, the field alias is really only useful if you know what the problem is and what's required to resolve it. So we'll be changing the codegen engine to detect this kind of collision and throw an error causing codegen to fail with a helpful error message about what to do. |
Summary
When querying different properties of similar name — one being singular, one being plural — with a different subset of properties , Apollo generates conflicting type names for the query response.
We have a query with two properties on a parent type. One is a single instance, the other is an array. Both are of the same type in the schema. When we query different subsets of properties on each, Apollo generated separate types (as expected) but names them the same. This is a conflict because they are defined on the same level.
Version
1.0.7
Steps to reproduce the behavior
Query example:
Both resulting types are called
Value
in the Swift project. This makes any reference ambiguous and fails the build.Logs
No response
Anything else?
Apollo on Android seems to notice the conflicting type names, as it generates one
Value
and oneValue1
type.The text was updated successfully, but these errors were encountered: