Skip to content

Swift: Query containing fragment with same name as outer query field generates variables with wrong type #2396

@RobinDaugherty

Description

@RobinDaugherty

When generating Swift code for a query containing a fragment with the same field name as a field in the query.

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

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions