Repository: apollographql/apollo-tooling
Pull request #1950 (2020-05-15 19:21:34)

Swift: Make selections a var instead of a let
A workaround for [iOS #230](https://github.com/apollographql/apollo-ios/issues/230) - the way that Swift allocates `static lets` was basically eating through memory that it shouldn't been. This transitions to using a computed `static var` instead, which should alleviate the problem. 

~I did look into just making it a computed var but there were some...complications... with the formatting that I wasted far too much time on trying to make work, and this achieves the same effect with a more minimal change.~ Got it working as a computed `var` after stepping away from the code for a weekend. 

designatednerd: Validated that the .zip produced by CI works with the current iOS and all tests still pass 🎉- will have to bump the iOS version once we have a new release for tooling but hey, at least it works in a vacuum 😆
designatednerd: @gsabran updated to computed var in manner I can't believe I didn't think of the other day. LMK what you think here.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 404c974d..5ea0d03c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,7 +9,7 @@
 - `apollo-codegen-scala`
   - <First `apollo-codegen-scala` related entry goes here>
 - `apollo-codegen-swift`
-  - <First `apollo-codegen-swift` related entry goes here>
+  - Change selection sets to computed vars to remove unnecessary  memory use [#1950](https://github.com/apollographql/apollo-tooling/pull/1950)
 - `apollo-codegen-typescript`
   - <First `apollo-codegen-typescript` related entry goes here>
 - `apollo-codegen-core`
diff --git a/packages/apollo-codegen-swift/src/codeGeneration.ts b/packages/apollo-codegen-swift/src/codeGeneration.ts
index 19998847..503cd715 100644
--- a/packages/apollo-codegen-swift/src/codeGeneration.ts
+++ b/packages/apollo-codegen-swift/src/codeGeneration.ts
@@ -471,14 +471,17 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
 
         this.printNewlineIfNeeded();
         this.printOnNewline(
-          swift`public static let selections: [GraphQLSelection] = `
+          swift`public static var selections: [GraphQLSelection] {`
         );
-        if (typeCase) {
-          this.typeCaseInitialization(typeCase);
-        } else {
-          this.selectionSetInitialization(variant);
-        }
-
+        this.withIndent(() => {
+          this.printOnNewline(swift`return `);
+          if (typeCase) {
+            this.typeCaseInitialization(typeCase);
+          } else {
+            this.selectionSetInitialization(variant);
+          }
+        });
+        this.printOnNewline(swift`}`);
         this.printNewlineIfNeeded();
 
         this.printOnNewline(


Test patch:
diff --git a/packages/apollo-codegen-swift/src/__tests__/__snapshots__/codeGeneration.ts.snap b/packages/apollo-codegen-swift/src/__tests__/__snapshots__/codeGeneration.ts.snap
index 133081b2..f03f99d7 100644
--- a/packages/apollo-codegen-swift/src/__tests__/__snapshots__/codeGeneration.ts.snap
+++ b/packages/apollo-codegen-swift/src/__tests__/__snapshots__/codeGeneration.ts.snap
@@ -32,9 +32,11 @@ exports[`Swift code generation #classDeclarationForOperation() should correctly
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Mutation\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\\\n I thought\\\\n  This movie ROCKED!\\"]], type: .object(CreateReview.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\\\n I thought\\\\n  This movie ROCKED!\\"]], type: .object(CreateReview.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -58,10 +60,12 @@ exports[`Swift code generation #classDeclarationForOperation() should correctly
     public struct CreateReview: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Review\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
-        GraphQLField(\\"commentary\\", type: .scalar(String.self)),
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
+          GraphQLField(\\"commentary\\", type: .scalar(String.self)),
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -129,9 +133,11 @@ exports[`Swift code generation #classDeclarationForOperation() should correctly
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Mutation\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\\\n I thought\\\\n  This movie \\\\\\\\ ROCKED!\\"]], type: .object(CreateReview.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\\\n I thought\\\\n  This movie \\\\\\\\ ROCKED!\\"]], type: .object(CreateReview.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -155,10 +161,12 @@ exports[`Swift code generation #classDeclarationForOperation() should correctly
     public struct CreateReview: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Review\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
-        GraphQLField(\\"commentary\\", type: .scalar(String.self)),
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
+          GraphQLField(\\"commentary\\", type: .scalar(String.self)),
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -222,9 +230,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Mutation\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\"]], type: .object(CreateReview.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\"]], type: .object(CreateReview.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -248,10 +258,12 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
     public struct CreateReview: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Review\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
-        GraphQLField(\\"commentary\\", type: .scalar(String.self)),
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
+          GraphQLField(\\"commentary\\", type: .scalar(String.self)),
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -311,9 +323,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hero\\", type: .object(Hero.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hero\\", type: .object(Hero.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -337,13 +351,15 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
     public struct Hero: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLTypeCase(
-          variants: [\\"Droid\\": AsDroid.selections],
-          default: [
-          ]
-        )
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLTypeCase(
+            variants: [\\"Droid\\": AsDroid.selections],
+            default: [
+            ]
+          )
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -373,9 +389,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
       public struct AsDroid: GraphQLSelectionSet {
         public static let possibleTypes: [String] = [\\"Droid\\"]
 
-        public static let selections: [GraphQLSelection] = [
-          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-        ]
+        public static var selections: [GraphQLSelection] {
+          return [
+            GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+          ]
+        }
 
         public private(set) var resultMap: ResultMap
 
@@ -450,9 +468,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hero\\", type: .object(Hero.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hero\\", type: .object(Hero.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -476,13 +496,15 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
     public struct Hero: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLTypeCase(
-          variants: [\\"Droid\\": AsDroid.selections],
-          default: [
-          ]
-        )
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLTypeCase(
+            variants: [\\"Droid\\": AsDroid.selections],
+            default: [
+            ]
+          )
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -540,9 +562,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
       public struct AsDroid: GraphQLSelectionSet {
         public static let possibleTypes: [String] = [\\"Droid\\"]
 
-        public static let selections: [GraphQLSelection] = [
-          GraphQLField(\\"primaryFunction\\", type: .scalar(String.self)),
-        ]
+        public static var selections: [GraphQLSelection] {
+          return [
+            GraphQLField(\\"primaryFunction\\", type: .scalar(String.self)),
+          ]
+        }
 
         public private(set) var resultMap: ResultMap
 
@@ -617,9 +641,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hero\\", type: .object(Hero.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hero\\", type: .object(Hero.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -643,9 +669,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
     public struct Hero: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -728,9 +756,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hero\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\")], type: .object(Hero.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hero\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\")], type: .object(Hero.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -754,9 +784,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
     public struct Hero: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -810,9 +842,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hero\\", type: .object(Hero.selections)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hero\\", type: .object(Hero.selections)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -836,9 +870,11 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
     public struct Hero: GraphQLSelectionSet {
       public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-      public static let selections: [GraphQLSelection] = [
-        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      ]
+      public static var selections: [GraphQLSelection] {
+        return [
+          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        ]
+      }
 
       public private(set) var resultMap: ResultMap
 
@@ -926,10 +962,12 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
 
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"appearsIn\\", type: .nonNull(.list(.scalar(Episode.self)))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"appearsIn\\", type: .nonNull(.list(.scalar(Episode.self)))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1006,10 +1044,12 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
 
   public static let possibleTypes: [String] = [\\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"primaryFunction\\", type: .scalar(String.self)),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"primaryFunction\\", type: .scalar(String.self)),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1058,10 +1098,12 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
 
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"friends\\", type: .list(.object(Friend.selections))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"friends\\", type: .list(.object(Friend.selections))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1100,9 +1142,11 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
   public struct Friend: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1144,10 +1188,12 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
 
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"appearsIn\\", type: .nonNull(.list(.scalar(Episode.self)))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"appearsIn\\", type: .nonNull(.list(.scalar(Episode.self)))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1189,9 +1235,11 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
 "public struct Human: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1216,9 +1264,11 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
   public struct \`Self\`: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1249,10 +1299,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
 public struct Human: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
-    GraphQLField(\\"name\\", alias: \\"_self\\", type: .nonNull(.scalar(String.self))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
+      GraphQLField(\\"name\\", alias: \\"_self\\", type: .nonNull(.scalar(String.self))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1287,9 +1339,11 @@ public struct Human: GraphQLSelectionSet {
   public struct \`Self\`: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1322,10 +1376,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", alias: \\"private\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", alias: \\"private\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1364,9 +1420,11 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
   public struct \`Self\`: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1399,9 +1457,11 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"friends\\", type: .list(.object(Friend.selections))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"friends\\", type: .list(.object(Friend.selections))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1430,9 +1490,11 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
   public struct Friend: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1465,14 +1527,16 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLTypeCase(
-      variants: [\\"Droid\\": AsDroid.selections],
-      default: [
-        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      ]
-    )
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLTypeCase(
+        variants: [\\"Droid\\": AsDroid.selections],
+        default: [
+          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        ]
+      )
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1512,10 +1576,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
   public struct AsDroid: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1570,9 +1636,11 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1604,11 +1672,13 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLBooleanCondition(variableName: \\"includeName\\", inverted: false, selections: [
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    ]),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLBooleanCondition(variableName: \\"includeName\\", inverted: false, selections: [
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      ]),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1640,10 +1710,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1701,14 +1773,16 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLTypeCase(
-      variants: [\\"Droid\\": AsDroid.selections],
-      default: [
-        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      ]
-    )
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLTypeCase(
+        variants: [\\"Droid\\": AsDroid.selections],
+        default: [
+          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        ]
+      )
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1776,10 +1850,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
   public struct AsDroid: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1834,14 +1910,16 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLTypeCase(
-      variants: [\\"Droid\\": AsDroid.selections],
-      default: [
-        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      ]
-    )
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLTypeCase(
+        variants: [\\"Droid\\": AsDroid.selections],
+        default: [
+          GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        ]
+      )
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
@@ -1881,10 +1959,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
   public struct AsDroid: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Droid\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
-      GraphQLField(\\"primaryFunction\\", type: .scalar(String.self)),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"name\\", type: .nonNull(.scalar(String.self))),
+        GraphQLField(\\"primaryFunction\\", type: .scalar(String.self)),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -1923,10 +2003,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should preser
 "public struct Hero: GraphQLSelectionSet {
   public static let possibleTypes: [String] = [\\"Human\\", \\"Droid\\"]
 
-  public static let selections: [GraphQLSelection] = [
-    GraphQLField(\\"name\\", alias: \\"_name\\", type: .nonNull(.scalar(String.self))),
-    GraphQLField(\\"id\\", alias: \\"_camel_case_id__\\", type: .nonNull(.scalar(GraphQLID.self))),
-  ]
+  public static var selections: [GraphQLSelection] {
+    return [
+      GraphQLField(\\"name\\", alias: \\"_name\\", type: .nonNull(.scalar(String.self))),
+      GraphQLField(\\"id\\", alias: \\"_camel_case_id__\\", type: .nonNull(.scalar(GraphQLID.self))),
+    ]
+  }
 
   public private(set) var resultMap: ResultMap
 
diff --git a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap
index 246158d1..2d39b40b 100644
--- a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap
+++ b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap
@@ -249,9 +249,11 @@ public final class SimpleQueryQuery: GraphQLQuery {
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hello\\", type: .nonNull(.scalar(String.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hello\\", type: .nonNull(.scalar(String.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap
 
@@ -300,9 +302,11 @@ public final class SimpleQueryQuery: GraphQLQuery {
   public struct Data: GraphQLSelectionSet {
     public static let possibleTypes: [String] = [\\"Query\\"]
 
-    public static let selections: [GraphQLSelection] = [
-      GraphQLField(\\"hello\\", type: .nonNull(.scalar(String.self))),
-    ]
+    public static var selections: [GraphQLSelection] {
+      return [
+        GraphQLField(\\"hello\\", type: .nonNull(.scalar(String.self))),
+      ]
+    }
 
     public private(set) var resultMap: ResultMap