From 632d2d8f9acdb0ab3bdfbff62870036f8c7fb17f Mon Sep 17 00:00:00 2001
From: Anthony Miller <anthonymdev@gmail.com>
Date: Fri, 26 Apr 2024 11:06:56 -0700
Subject: [PATCH] Deprecate localCacheMutation selectionSetInitializer option

---
 ...olloCodegenConfigurationCodableTests.swift | 59 +------------
 .../ApolloCodegenTests.swift                  |  5 +-
 .../Templates/FragmentTemplateTests.swift     |  4 +-
 ...CacheMutationDefinitionTemplateTests.swift | 84 +------------------
 .../ApolloCodegenConfiguration.swift          | 35 ++++----
 5 files changed, 22 insertions(+), 165 deletions(-)

diff --git a/Tests/ApolloCodegenTests/ApolloCodegenConfigurationCodableTests.swift b/Tests/ApolloCodegenTests/ApolloCodegenConfigurationCodableTests.swift
index 03ac59199..3b90a0756 100644
--- a/Tests/ApolloCodegenTests/ApolloCodegenConfigurationCodableTests.swift
+++ b/Tests/ApolloCodegenTests/ApolloCodegenConfigurationCodableTests.swift
@@ -110,7 +110,7 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
           "pruneGeneratedFiles" : false,
           "schemaDocumentation" : "exclude",
           "selectionSetInitializers" : {
-            "localCacheMutations" : true
+
           },
           "warningsOnDeprecatedUsage" : "exclude"
         },
@@ -486,66 +486,12 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
     expect(decoded).to(equal([]))
   }
 
-  func test__encode_selectionSetInitializers__givenLocalCacheMutations_shouldReturnObjectString() throws {
-    // given
-    let subject: ApolloCodegenConfiguration.SelectionSetInitializers = [.localCacheMutations]
-
-    let expected = """
-    {
-      "localCacheMutations" : true
-    }
-    """
-
-    // when
-    let actual = try testJSONEncoder.encode(subject).asString
-
-    // then
-    expect(actual).to(equal(expected))
-  }
-
-  func test__decode_selectionSetInitializers__givenLocalCacheMutations_shouldReturnOptions() throws {
-    // given
-    let subject = """
-    {
-      "localCacheMutations": true
-    }
-    """.asData
-
-    // when
-    let decoded = try JSONDecoder().decode(
-      ApolloCodegenConfiguration.SelectionSetInitializers.self,
-      from: subject
-    )
-
-    // then
-    expect(decoded).to(equal(.localCacheMutations))
-  }
-
-  func test__decode_selectionSetInitializers__givenLocalCacheMutations_false_shouldReturnEmptyOptions() throws {
-    // given
-    let subject = """
-    {
-      "localCacheMutations": false
-    }
-    """.asData
-
-    // when
-    let decoded = try JSONDecoder().decode(
-      ApolloCodegenConfiguration.SelectionSetInitializers.self,
-      from: subject
-    )
-
-    // then
-    expect(decoded).to(equal([]))
-  }
-
   func test__encode_selectionSetInitializers__givenAll_shouldReturnObjectString() throws {
     // given
     let subject: ApolloCodegenConfiguration.SelectionSetInitializers = .all
 
     let expected = """
     {
-      "localCacheMutations" : true,
       "namedFragments" : true,
       "operations" : true
     }
@@ -563,8 +509,7 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
     let subject = """
     {
       "operations" : true,
-      "namedFragments" : true,
-      "localCacheMutations" : true
+      "namedFragments" : true
     }
     """.asData
 
diff --git a/Tests/ApolloCodegenTests/ApolloCodegenTests.swift b/Tests/ApolloCodegenTests/ApolloCodegenTests.swift
index 3475527d4..b7177c00c 100644
--- a/Tests/ApolloCodegenTests/ApolloCodegenTests.swift
+++ b/Tests/ApolloCodegenTests/ApolloCodegenTests.swift
@@ -2386,12 +2386,11 @@ class ApolloCodegenTests: XCTestCase {
       [.siblings, .namedFragments]
     ]
     let initializerOptions: [ApolloCodegenConfiguration.SelectionSetInitializers] = [
-      .all,
-      .localCacheMutations,
+      .all,      
       .operations,
       .namedFragments,
       .fragment(named: "TestFragment"),
-      [.operations, .localCacheMutations]
+      [.operations, .namedFragments]
     ]
 
     for fieldMergingOption in fieldMergingOptions {
diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift
index 7610da920..cf56f4551 100644
--- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift
+++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift
@@ -574,7 +574,7 @@ class FragmentTemplateTests: XCTestCase {
     expect(actual).to(equalLineByLine("}", atLine: 16, ignoringExtraLines: true))
   }
 
-  func test__render_givenNamedFragments_asLocalCacheMutation_configIncludeLocalCacheMutations_rendersInitializer() async throws {
+  func test__render_givenNamedFragments_asLocalCacheMutation_rendersInitializer() async throws {
     // given
     schemaSDL = """
       type Query {
@@ -612,7 +612,7 @@ class FragmentTemplateTests: XCTestCase {
     // when
     try await buildSubjectAndFragment(
       config: .mock(options: .init(
-        selectionSetInitializers: [.localCacheMutations]
+        selectionSetInitializers: []
       )))
 
     let actual = renderSubject()
diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/LocalCacheMutationDefinitionTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/LocalCacheMutationDefinitionTemplateTests.swift
index 7b1cfff0b..db265d758 100644
--- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/LocalCacheMutationDefinitionTemplateTests.swift
+++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/LocalCacheMutationDefinitionTemplateTests.swift
@@ -670,7 +670,7 @@ class LocalCacheMutationDefinitionTemplateTests: XCTestCase {
 
   // MARK: Initializer Rendering Config - Tests
 
-  func test__render_givenLocalCacheMutation_configIncludesLocalCacheMutations_rendersInitializer() async throws {
+  func test__render_givenLocalCacheMutation_rendersInitializer() async throws {
     // given
     schemaSDL = """
     type Query {
@@ -700,7 +700,7 @@ class LocalCacheMutationDefinitionTemplateTests: XCTestCase {
     config = ApolloCodegenConfiguration.mock(
       schemaNamespace: "TestSchema",
       options: .init(
-        selectionSetInitializers: [.localCacheMutations]
+        selectionSetInitializers: []
       )
     )
 
@@ -713,84 +713,4 @@ class LocalCacheMutationDefinitionTemplateTests: XCTestCase {
     expect(actual).to(equalLineByLine(expected, atLine: 18, ignoringExtraLines: true))
   }
 
-  func test__render_givenLocalCacheMutation_configIncludesSpecificLocalCacheMutations_rendersInitializer() async throws {
-    // given
-    schemaSDL = """
-    type Query {
-      allAnimals: [Animal!]
-    }
-
-    type Animal {
-      species: String!
-    }
-    """
-
-    document = """
-    query TestOperation @apollo_client_ios_localCacheMutation {
-      allAnimals {
-        species
-      }
-    }
-    """
-
-    let expected =
-    """
-        }
-
-        init(
-    """
-
-    config = ApolloCodegenConfiguration.mock(
-      schemaNamespace: "TestSchema",
-      options: .init(
-        selectionSetInitializers: [.operation(named: "TestOperation")]
-      )
-    )
-
-    // when
-    try await buildSubjectAndOperation()
-
-    let actual = renderSubject()
-
-    // then
-    expect(actual).to(equalLineByLine(expected, atLine: 18, ignoringExtraLines: true))
-  }
-
-  func test__render_givenLocalCacheMutation_configDoesNotIncludesLocalCacheMutations_doesNotRenderInitializer() async throws
-  {
-    // given
-    schemaSDL = """
-    type Query {
-      allAnimals: [Animal!]
-    }
-
-    type Animal {
-      species: String!
-    }
-    """
-
-    document = """
-    query TestOperation @apollo_client_ios_localCacheMutation {
-      allAnimals {
-        species
-      }
-    }
-    """
-
-    config = ApolloCodegenConfiguration.mock(
-      schemaNamespace: "TestSchema",
-      options: .init(
-        selectionSetInitializers: [.namedFragments]
-      )
-    )
-
-    // when
-    try await buildSubjectAndOperation()
-
-    let actual = renderSubject()
-
-    // then
-    expect(actual).to(equalLineByLine("    /// AllAnimal", atLine: 20, ignoringExtraLines: true))
-  }
-
 }
diff --git a/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift b/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift
index c0b6b171a..580f72f9d 100644
--- a/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift
+++ b/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift
@@ -512,7 +512,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
       public static let additionalInflectionRules: [InflectionRule] = []
       public static let deprecatedEnumCases: Composition = .include
       public static let schemaDocumentation: Composition = .include
-      public static let selectionSetInitializers: SelectionSetInitializers = [.localCacheMutations]
+      public static let selectionSetInitializers: SelectionSetInitializers = []
       public static let operationDocumentFormat: OperationDocumentFormat = .definition
       public static let cocoapodsCompatibleImportStatements: Bool = false
       public static let warningsOnDeprecatedUsage: Composition = .include
@@ -842,12 +842,8 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
   /// The ``SelectionSetInitializers`` configuration is used to determine if you would like
   /// initializers to be generated for your generated selection set models.
   ///
-  /// There are three categories of selection set models that initializers can be generated for:
-  /// - Operations
-  /// - Named fragments
-  /// - Local cache mutations
-  ///
-  /// By default, initializers are only generated for local cache mutations.
+  /// Initializers are always generated for local cache mutations.
+  /// You can additionally configure initializers to be generated for operations and named fragments.
   ///
   /// ``SelectionSetInitializers`` functions like an `OptionSet`, allowing you to combine multiple
   /// different instances together to indicate all the types you would like to generate
@@ -860,13 +856,10 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
     /// that are not local cache mutations.
     public static let operations: SelectionSetInitializers = .init(.operations)
 
-    /// Option to generate initializers for all local cache mutations.
-    public static let localCacheMutations: SelectionSetInitializers = .init(.localCacheMutations)
-
     /// Option to generate initializers for all models.
     /// This includes named fragments, operations, and local cache mutations.
     public static let all: SelectionSetInitializers = [
-      .namedFragments, .operations, .localCacheMutations
+      .namedFragments, .operations
     ]
 
     /// An option to generate initializers for a single operation with a given name.
@@ -1195,14 +1188,13 @@ extension ApolloCodegenConfiguration {
   func shouldGenerateSelectionSetInitializers(for operation: IR.Operation) -> Bool {
     guard experimentalFeatures.fieldMerging == .all else { return false }
 
-    switch operation.definition.isLocalCacheMutation {
-    case true where options.selectionSetInitializers.contains(.localCacheMutations):
+    if operation.definition.isLocalCacheMutation {
       return true
 
-    case false where options.selectionSetInitializers.contains(.operations):
+    } else if options.selectionSetInitializers.contains(.operations) {
       return true
 
-    default:
+    } else {
       return options.selectionSetInitializers.contains(definitionNamed: operation.definition.name)
     }
   }
@@ -1213,8 +1205,7 @@ extension ApolloCodegenConfiguration {
 
     if options.selectionSetInitializers.contains(.namedFragments) { return true }
 
-    if fragment.definition.isLocalCacheMutation &&
-        options.selectionSetInitializers.contains(.localCacheMutations) {
+    if fragment.definition.isLocalCacheMutation {
       return true
     }
 
@@ -1227,7 +1218,6 @@ extension ApolloCodegenConfiguration {
 extension ApolloCodegenConfiguration.SelectionSetInitializers {
   struct Options: OptionSet, Codable, Equatable {
     let rawValue: Int
-    static let localCacheMutations = Options(rawValue: 1 << 0)
     static let namedFragments      = Options(rawValue: 1 << 1)
     static let operations          = Options(rawValue: 1 << 2)
   }
@@ -1255,7 +1245,6 @@ extension ApolloCodegenConfiguration.SelectionSetInitializers {
   enum CodingKeys: CodingKey, CaseIterable {
     case operations
     case namedFragments
-    case localCacheMutations
     case definitionsNamed
   }
 
@@ -1272,7 +1261,6 @@ extension ApolloCodegenConfiguration.SelectionSetInitializers {
 
     try decode(option: .operations, forKey: .operations)
     try decode(option: .namedFragments, forKey: .namedFragments)
-    try decode(option: .localCacheMutations, forKey: .localCacheMutations)
 
     self.options = options
     self.definitions = try values.decodeIfPresent(
@@ -1291,7 +1279,6 @@ extension ApolloCodegenConfiguration.SelectionSetInitializers {
 
     try encodeIfPresent(option: .operations, forKey: .operations)
     try encodeIfPresent(option: .namedFragments, forKey: .namedFragments)
-    try encodeIfPresent(option: .localCacheMutations, forKey: .localCacheMutations)
 
     if !definitions.isEmpty {
       try container.encode(definitions.sorted(), forKey: .definitionsNamed)
@@ -1635,6 +1622,12 @@ extension ApolloCodegenConfiguration.ConversionStrategies {
   
 }
 
+extension ApolloCodegenConfiguration.SelectionSetInitializers {
+  /// Option to generate initializers for all local cache mutations.
+  @available(*, deprecated, message: "Local Cache Mutations will now always have initializers generated.")
+  public static let localCacheMutations: ApolloCodegenConfiguration.SelectionSetInitializers = .init([])
+}
+
 private struct AnyCodingKey: CodingKey {
   var stringValue: String