Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate localCacheMutation selectionSetInitializer option #350

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
/// 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 @@
/// 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 @@ -1023,7 +1016,7 @@
) ?? Default.legacySafelistingCompatibleOperations
}

public func encode(to encoder: Encoder) throws {

Check failure on line 1019 in apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift

View workflow job for this annotation

GitHub Actions / Codegen CLI Unit Tests - macOS

use of protocol 'Encoder' as a type must be written 'any Encoder'

Check failure on line 1019 in apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

use of protocol 'Encoder' as a type must be written 'any Encoder'

Check failure on line 1019 in apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

use of protocol 'Encoder' as a type must be written 'any Encoder'
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.fieldMerging, forKey: .fieldMerging)
Expand Down Expand Up @@ -1207,14 +1200,13 @@
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 @@

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.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 @@
enum CodingKeys: CodingKey, CaseIterable {
case operations
case namedFragments
case localCacheMutations
case definitionsNamed
}

Expand All @@ -1284,7 +1273,6 @@

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 @@

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.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
Loading