Skip to content

Commit

Permalink
Add validation for fieldmerging w/selection set init
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed May 1, 2024
1 parent 6d72318 commit 0c1dcdb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
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
40 changes: 40 additions & 0 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ extension ApolloCodegen.ConfigurationContext {
""")
}

guard self.experimentalFeatures.fieldMerging == .all ||
self.options.selectionSetInitializers == []
else {
throw ApolloCodegen.Error.fieldMergingIncompatibility
}

guard
!SwiftKeywords.DisallowedSchemaNamespaceNames.contains(self.schemaNamespace.lowercased())
else {
Expand Down

0 comments on commit 0c1dcdb

Please sign in to comment.