apollographql / apollographql/apollo-tooling
Swift: Query containing fragment with same name as outer query field generates variables with wrong type
- Dominant language
- TypeScript
- Stars
- 3k
- Forks
- 460
- PR merge metrics
- No merged PRs in 30d
Description
When generating Swift code for a query containing a fragment with the same field name as a field in the query.
```gql
fragment Gameplan on Gameplan {
a b c
}
query TheThing {
gameplan {
... Gameplan
}
}
```
This is generated with `--namespace Backend`.
(Without a specified namespace, I don't believe there's a way that codegen would be able to generate code without this issue. This is because there's no "root namespace" in Swift.)
**Intended outcome:**
`Backend.TheThingQuery.Data.Gameplan.Fragments` type should contain a var of type `Backend.Gameplan`
```swift
public extension Backend {
final class TheThingQuery: GraphQLQuery {
public struct Data: GraphQLSelectionSet {
public struct Gameplan: GraphQLSelectionSet {
public struct Fragments {
public var gameplan: Backend.Gameplan {
get {
return Backend.Gameplan(unsafeResultMap: self.resultMap)
}
//snip
```
**Actual outcome:**
`Backend.TheThingQuery.Data.Gameplan.Fragments` type contains a var of type "Gameplan" which Swift interprets as a reference to `Backend.TheThingQuery.Data.Gameplan`.
Referencing the data in this fragment will result in compile-time errors because it contains the fields of the query's `gameplan` field, and not those of the fragment.
```swift
public extension Backend {
final class TheThingQuery: GraphQLQuery {
public struct Data: GraphQLSelectionSet {
public struct Gameplan: GraphQLSelectionSet {
public struct Fragments {
public var gameplan: Gameplan {
get {
return Gameplan(unsafeResultMap: self.resultMap)
}
//snip
```
**How to reproduce the issue:**
Use a fragment with the same name as a field that contains the field implementing the fragment.
**Versions**
0.40.3
Contributor guide
Research direction
No source file or test is named. Reproduce the Swift generation case from the issue with --namespace Backend and inspect the code-generation path that resolves the fragment type name. Done means the generated Fragments.gameplan property uses Backend.Gameplan rather than the enclosing query's Gameplan type, and the generated code compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, swift, typescript
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100