diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift index 72cbef648..1e182cbc1 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift @@ -621,6 +621,50 @@ class FragmentTemplateTests: XCTestCase { expect(actual).to(equalLineByLine(expected, atLine: 20, ignoringExtraLines: true)) } + func test__render_givenOperationSelectionSet_initializerConfig_all_fieldMergingConfig_notAll_doesNotRenderInitializer() async throws { + let tests: [ApolloCodegenConfiguration.FieldMerging] = [ + .none, + .ancestors, + .namedFragments, + .siblings, + [.ancestors, .namedFragments], + [.siblings, .ancestors], + [.siblings, .namedFragments] + ] + + for test in tests { + // given + schemaSDL = """ + type Query { + allAnimals: [Animal!] + } + + type Animal { + species: String! + } + """ + + document = """ + fragment TestFragment on Animal { + species + } + """ + + // when + try await buildSubjectAndFragment(config: .mock( + options: .init( + selectionSetInitializers: [.all], + fieldMerging: test + ) + )) + + let actual = renderSubject() + + // then + expect(actual).to(equalLineByLine("}", atLine: 16, ignoringExtraLines: true)) + } + } + // MARK: Local Cache Mutation Tests func test__render__givenFragment__asLocalCacheMutation_generatesFragmentDeclarationDefinitionAsMutableSelectionSetAndBoilerplate() async throws { // given diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift index 75fdec1a3..80879a6d2 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift @@ -636,6 +636,54 @@ class OperationDefinitionTemplateTests: XCTestCase { expect(actual).to(equalLineByLine(" }", atLine: 35, ignoringExtraLines: true)) } + func test__render_givenOperationSelectionSet_initializerConfig_all_fieldMergingConfig_notAll_doesNotRenderInitializer() async throws { + let tests: [ApolloCodegenConfiguration.FieldMerging] = [ + .none, + .ancestors, + .namedFragments, + .siblings, + [.ancestors, .namedFragments], + [.siblings, .ancestors], + [.siblings, .namedFragments] + ] + + for test in tests { + // given + schemaSDL = """ + type Query { + allAnimals: [Animal!] + } + + type Animal { + species: String! + } + """ + + document = """ + query TestOperation { + allAnimals { + species + } + } + """ + + config = .mock( + options: .init( + selectionSetInitializers: [.all], + fieldMerging: test + ) + ) + + // when + try await buildSubjectAndOperation() + + let actual = renderSubject() + + // then + expect(actual).to(equalLineByLine(" }", atLine: 35, ignoringExtraLines: true)) + } + } + // MARK: - Variables func test__generate__givenQueryWithScalarVariable_generatesQueryOperationWithVariable() async throws { diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplate_FieldMerging_Tests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplate_FieldMerging_Tests.swift index d40d7123c..010fb7a6f 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplate_FieldMerging_Tests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplate_FieldMerging_Tests.swift @@ -543,7 +543,7 @@ class SelectionSetTemplate_FieldMerging_Tests: XCTestCase { allAnimals { predator { species - } + } ... on Dog { species } @@ -1218,82 +1218,4 @@ class SelectionSetTemplate_FieldMerging_Tests: XCTestCase { expect(actual).to(equalLineByLine(expected, atLine: 12, ignoringExtraLines: true)) } - // MARK: - SelectionSetInitializers - - func test__render_selectionSetInitializer__givenFieldMerging_none_withMergedSelections_rendersInitializerWithAllMergedSelections() async throws { - // given - schemaSDL = """ - type Query { - allAnimals: [Animal!] - } - - interface Animal { - species: String! - age: Int! - } - - interface Pet implements Animal { - species: String! - age: Int! - } - - type Dog implements Animal & Pet { - species: String! - age: Int! - bark: Boolean! - } - """ - - document = """ - query TestOperation { - allAnimals { - age - ... on Pet { - species - } - ... on Dog { - bark - } - } - } - """ - - let expected = - """ - public init( - bark: Bool, - age: Int, - species: String - ) { - self.init(_dataDict: DataDict( - data: [ - "__typename": TestSchema.Objects.Dog.typename, - "bark": bark, - "age": age, - "species": species, - ], - fulfilledFragments: [ - ObjectIdentifier(TestOperationQuery.Data.AllAnimal.self), - ObjectIdentifier(TestOperationQuery.Data.AllAnimal.AsDog.self), - ObjectIdentifier(TestOperationQuery.Data.AllAnimal.AsPet.self) - ] - )) - } - """ - - // when - try await buildSubjectAndOperation( - fieldMerging: .none, - selectionSetInitializers: true - ) - - let allAnimals_asDog = try XCTUnwrap( - operation[field: "query"]?[field: "allAnimals"]?[as: "Dog"] - ) - - let actual = subject.test_render(childEntity: allAnimals_asDog.computed) - - // then - expect(actual).to(equalLineByLine(expected, atLine: 14, ignoringExtraLines: true)) - } } diff --git a/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift b/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift index 0bff859f8..cff16aa86 100644 --- a/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift +++ b/apollo-ios-codegen/Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift @@ -1177,6 +1177,8 @@ extension ApolloCodegenConfiguration.OperationsFileOutput { extension ApolloCodegenConfiguration.OutputOptions { /// Determine whether the operations files are output to the schema types module. func shouldGenerateSelectionSetInitializers(for operation: IR.Operation) -> Bool { + guard fieldMerging == .all else { return false } + switch operation.definition.isLocalCacheMutation { case true where selectionSetInitializers.contains(.localCacheMutations): return true @@ -1191,6 +1193,8 @@ extension ApolloCodegenConfiguration.OutputOptions { /// Determine whether the operations files are output to the schema types module. func shouldGenerateSelectionSetInitializers(for fragment: IR.NamedFragment) -> Bool { + guard fieldMerging == .all else { return false } + if selectionSetInitializers.contains(.namedFragments) { return true } if fragment.definition.isLocalCacheMutation &&