Skip to content

Commit

Permalink
Deprecate localCacheMutation selectionSetInitializer option
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Jul 16, 2024
1 parent fb6138b commit 363d0dc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
},
"schemaDocumentation" : "exclude",
"selectionSetInitializers" : {
"localCacheMutations" : true
},
"warningsOnDeprecatedUsage" : "exclude"
},
Expand Down Expand Up @@ -513,66 +513,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
}
Expand All @@ -590,8 +536,7 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
let subject = """
{
"operations" : true,
"namedFragments" : true,
"localCacheMutations" : true
"namedFragments" : true
}
""".asData

Expand Down
5 changes: 2 additions & 3 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2427,12 +2427,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -612,7 +612,7 @@ class FragmentTemplateTests: XCTestCase {
// when
try await buildSubjectAndFragment(
config: .mock(options: .init(
selectionSetInitializers: [.localCacheMutations]
selectionSetInitializers: []
)))

let actual = renderSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -700,7 +700,7 @@ class LocalCacheMutationDefinitionTemplateTests: XCTestCase {
config = ApolloCodegenConfiguration.mock(
schemaNamespace: "TestSchema",
options: .init(
selectionSetInitializers: [.localCacheMutations]
selectionSetInitializers: []
)
)

Expand All @@ -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))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,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 schemaCustomization: SchemaCustomization = .init()
public static let cocoapodsCompatibleImportStatements: Bool = false
Expand Down Expand Up @@ -854,12 +854,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
Expand All @@ -872,13 +868,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.
Expand Down Expand Up @@ -1207,14 +1200,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)
}
}
Expand All @@ -1225,8 +1217,7 @@ extension ApolloCodegenConfiguration {

if options.selectionSetInitializers.contains(.namedFragments) { return true }

if fragment.definition.isLocalCacheMutation &&
options.selectionSetInitializers.contains(.localCacheMutations) {
if fragment.definition.isLocalCacheMutation {
return true
}

Expand All @@ -1239,7 +1230,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)
}
Expand Down Expand Up @@ -1267,7 +1257,6 @@ extension ApolloCodegenConfiguration.SelectionSetInitializers {
enum CodingKeys: CodingKey, CaseIterable {
case operations
case namedFragments
case localCacheMutations
case definitionsNamed
}

Expand All @@ -1284,7 +1273,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(
Expand All @@ -1303,7 +1291,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)
Expand Down Expand Up @@ -1649,6 +1636,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

Expand Down

0 comments on commit 363d0dc

Please sign in to comment.