Skip to content

Commit

Permalink
Disable init when using field merging
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed May 1, 2024
1 parent cdebc1e commit fd51cc3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ class SelectionSetTemplate_FieldMerging_Tests: XCTestCase {
allAnimals {
predator {
species
}
}
... on Dog {
species
}
Expand Down Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 &&
Expand Down

0 comments on commit fd51cc3

Please sign in to comment.