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

Field Merging [5/x] Move fieldMerging option to experimental #344

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 @@ -30,6 +30,7 @@ extension ApolloCodegenConfiguration {
public static func mock(
_ moduleType: ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType,
options: ApolloCodegenConfiguration.OutputOptions = .init(),
experimentalFeatures: ExperimentalFeatures = .init(),
schemaNamespace: String = "TestSchema",
to path: String = "MockModulePath"
) -> Self {
Expand All @@ -42,7 +43,8 @@ extension ApolloCodegenConfiguration {
output: .init(
schemaTypes: .init(path: path, moduleType: moduleType)
),
options: options
options: options,
experimentalFeatures: experimentalFeatures
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
markOperationDefinitionsAsFinal: true
),
experimentalFeatures: .init(
fieldMerging: .all,
legacySafelistingCompatibleOperations: true
),
operationManifest: .init(
Expand All @@ -81,6 +82,9 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
"""
{
"experimentalFeatures" : {
"fieldMerging" : [
"all"
],
"legacySafelistingCompatibleOperations" : true
},
"input" : {
Expand Down Expand Up @@ -112,9 +116,6 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
"inputObjects" : "none"
},
"deprecatedEnumCases" : "exclude",
"fieldMerging" : [
"all"
],
"markOperationDefinitionsAsFinal" : true,
"operationDocumentFormat" : [
"definition"
Expand Down
40 changes: 40 additions & 0 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,46 @@ class ApolloCodegenTests: XCTestCase {
})
}

func test__validation__givenFieldMerging_notAll_andSelectionSetInitializers_enabled_shouldThrowError() throws {
// given
let fieldMergingOptions: [ApolloCodegenConfiguration.FieldMerging] = [
.none,
.ancestors,
.namedFragments,
.siblings,
[.ancestors, .namedFragments],
[.siblings, .ancestors],
[.siblings, .namedFragments]
]
let initializerOptions: [ApolloCodegenConfiguration.SelectionSetInitializers] = [
.all,
.localCacheMutations,
.operations,
.namedFragments,
.fragment(named: "TestFragment"),
[.operations, .localCacheMutations]
]

for fieldMergingOption in fieldMergingOptions {
for initializerOption in initializerOptions {

let config = ApolloCodegenConfiguration.mock(
.other,
options: .init(
selectionSetInitializers: initializerOption
),
experimentalFeatures: .init(
fieldMerging: fieldMergingOption
)
)

// then
expect(try ApolloCodegen._validate(config: config))
.to(throwError(ApolloCodegen.Error.fieldMergingIncompatibility))
}
}
}

// MARK: Path Match Exclusion Tests

func test__match__givenFilesInSpecialExcludedPaths_shouldNotReturnExcludedPaths() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,9 @@ class FragmentTemplateTests: XCTestCase {
// when
try await buildSubjectAndFragment(config: .mock(
options: .init(
selectionSetInitializers: [.all],
fieldMerging: test
)
selectionSetInitializers: [.all]
),
experimentalFeatures: .init(fieldMerging: test)
))

let actual = renderSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ class OperationDefinitionTemplateTests: XCTestCase {

config = .mock(
options: .init(
selectionSetInitializers: [.all],
fieldMerging: test
)
selectionSetInitializers: [.all]
),
experimentalFeatures: .init(fieldMerging: test)
)

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SelectionSetTemplate_ErrorHandling_Tests: XCTestCase {
let config = ApolloCodegenConfiguration.mock(
schemaNamespace: "TestSchema",
output: .mock(moduleType: .swiftPackageManager, operations: .inSchemaModule),
options: .init(
experimentalFeatures: .init(
fieldMerging: fieldMerging
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class SelectionSetTemplate_FieldMerging_Tests: XCTestCase {
options: .init(
additionalInflectionRules: inflectionRules,
schemaDocumentation: schemaDocumentation,
fieldMerging: fieldMerging,
cocoapodsCompatibleImportStatements: cocoapodsImportStatements,
warningsOnDeprecatedUsage: warningsOnDeprecatedUsage,
conversionStrategies: conversionStrategies
)
),
experimentalFeatures: .init(fieldMerging: fieldMerging)
))
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile(),
Expand Down Expand Up @@ -1218,4 +1218,82 @@ 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 @@ -17,6 +17,7 @@ extension ApolloCodegen {
case invalidConfiguration(message: String)
case invalidSchemaName(_ name: String, message: String)
case targetNameConflict(name: String)
case fieldMergingIncompatibility

public var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -52,6 +53,13 @@ extension ApolloCodegen {
Target name '\(name)' conflicts with a reserved library name. Please choose a different \
target name.
"""
case .fieldMergingIncompatibility:
return """
Options for disabling 'fieldMerging' and enabling 'selectionSetInitializers' are
incompatible.

Please set either 'fieldMerging' to 'all' or 'selectionSetInitializers' to be empty.
"""
}
}
}
Expand Down
Loading
Loading