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

fix: Uses correct casing for schema name in generated namespace #2586

Merged
merged 9 commits into from
Oct 15, 2022
1 change: 0 additions & 1 deletion Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ public class ApolloCodegen {
try autoreleasepool {
try UnionFileGenerator(
graphqlUnion: graphQLUnion,
schemaName: config.schemaName,
config: config
).generate(
forConfig: config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ struct SchemaModuleFileGenerator {
case .swiftPackageManager:
filePath = pathURL.appendingPathComponent("Package.swift").path
rendered = SwiftPackageManagerModuleTemplate(
moduleName: config.schemaName,
testMockConfig: config.output.testMocks,
config: config
).render()

case .embeddedInTarget:
filePath = pathURL.appendingPathComponent("\(config.schemaName).graphql.swift").path
filePath = pathURL
.appendingPathComponent("\(config.schemaName.firstUppercased).graphql.swift").path
rendered = SchemaModuleNamespaceTemplate(
namespace: config.schemaName,
config: config
).render()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import Foundation
struct UnionFileGenerator: FileGenerator {
/// Source GraphQL union.
let graphqlUnion: GraphQLUnionType
/// Schema name
let schemaName: String
/// Shared codegen configuration.
let config: ApolloCodegen.ConfigurationContext

var template: TemplateRenderer { UnionTemplate(
moduleName: schemaName,
graphqlUnion: graphqlUnion,
config: config
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct MockObjectTemplate: TemplateRenderer {

return """
public class \(objectName): MockObject {
public static let objectType: Object = \(config.schemaName).Objects.\(objectName)
public static let objectType: Object = \(config.schemaName.firstUppercased).Objects.\(objectName)
public static let _mockFields = MockFields()
public typealias MockValueCollectionType = Array<Mock<\(objectName)>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension GraphQLType {
) -> String {

lazy var schemaModuleName: String = {
!config.output.operations.isInModule ? "\(config.schemaName)." : ""
!config.output.operations.isInModule ? "\(config.schemaName.firstUppercased)." : ""
}()

switch self {
Expand Down Expand Up @@ -99,7 +99,7 @@ extension GraphQLType {
) -> String {

lazy var schemaModuleName: String = {
!config.output.schemaTypes.isInModule ? "\(config.schemaName)." : ""
!config.output.schemaTypes.isInModule ? "\(config.schemaName.firstUppercased)." : ""
}()

switch self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fileprivate extension GraphQLInputObjectType {
}

return """
\(if: !config.output.operations.isInModule, "\(config.schemaName).")\(name)(\(list: entries))
\(if: !config.output.operations.isInModule, "\(config.schemaName.firstUppercased).")\(name)(\(list: entries))
"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import Foundation
/// Provides the format to define a namespace that is used to wrap other templates to prevent
/// naming collisions in Swift code.
struct SchemaModuleNamespaceTemplate: TemplateRenderer {
/// Module namespace.
let namespace: String

let config: ApolloCodegen.ConfigurationContext

let target: TemplateTarget = .moduleFile

var template: TemplateString {
TemplateString("""
public enum \(namespace.firstUppercased) { }
public enum \(config.schemaName.firstUppercased) { }

""")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct SelectionSetTemplate {
selectionSetTypeName = "MutableInlineFragment"
}

return "\(schema.name.firstUppercased).\(selectionSetTypeName)"
return "\(config.schemaName.firstUppercased).\(selectionSetTypeName)"
}

// MARK: - Selection Set Name Documentation
Expand Down Expand Up @@ -119,7 +119,7 @@ struct SelectionSetTemplate {
private func ParentTypeTemplate(_ type: GraphQLCompositeType) -> String {
"""
public static var __parentType: ParentType { \
\(schema.name.firstUppercased).\(type.schemaTypesNamespace).\(type.name.firstUppercased) }
\(config.schemaName.firstUppercased).\(type.schemaTypesNamespace).\(type.name.firstUppercased) }
"""
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import Foundation
/// Provides the format to define a Swift Package Manager module in Swift code. The output must
/// conform to the [configuration definition of a Swift package](https://docs.swift.org/package-manager/PackageDescription/PackageDescription.html#).
struct SwiftPackageManagerModuleTemplate: TemplateRenderer {
/// Module name used to name the generated package.
let moduleName: String

let testMockConfig: ApolloCodegenConfiguration.TestMockFileOutput

Expand All @@ -15,30 +13,30 @@ struct SwiftPackageManagerModuleTemplate: TemplateRenderer {
let config: ApolloCodegen.ConfigurationContext

var template: TemplateString {
let casedModuleName = moduleName.firstUppercased
let casedSchemaName = config.schemaName.firstUppercased

return TemplateString("""
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "\(casedModuleName)",
name: "\(casedSchemaName)",
platforms: [
.iOS(.v12),
.macOS(.v10_14),
.tvOS(.v12),
.watchOS(.v5),
],
products: [
.library(name: "\(casedModuleName)", targets: ["\(casedModuleName)"]),
.library(name: "\(casedSchemaName)", targets: ["\(casedSchemaName)"]),
],
dependencies: [
.package(url: "https://github.com/apollographql/apollo-ios.git", from: "1.0.0"),
],
targets: [
.target(
name: "\(casedModuleName)",
name: "\(casedSchemaName)",
dependencies: [
.product(name: "ApolloAPI", package: "apollo-ios"),
],
Expand All @@ -49,7 +47,7 @@ struct SwiftPackageManagerModuleTemplate: TemplateRenderer {
name: "\($0.targetName)",
dependencies: [
.product(name: "ApolloTestSupport", package: "apollo-ios"),
.target(name: "\(casedModuleName)"),
.target(name: "\(casedSchemaName)"),
],
path: "\($0.path)"
),
Expand All @@ -68,7 +66,7 @@ struct SwiftPackageManagerModuleTemplate: TemplateRenderer {
if let targetName = targetName {
return (targetName.firstUppercased, "./\(targetName.firstUppercased)")
} else {
return ("\(moduleName.firstUppercased)TestMocks", "./TestMocks")
return ("\(config.schemaName.firstUppercased)TestMocks", "./TestMocks")
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/ApolloCodegenLib/Templates/TemplateRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ extension TemplateRenderer {
case (false, nil):
return nil
case (true, nil):
return config.schemaName
return config.schemaName.firstUppercased
case let (false, .some(schemaTypeNamespace)):
return schemaTypeNamespace
case let (true, .some(schemaTypeNamespace)):
return "\(config.schemaName).\(schemaTypeNamespace)"
return "\(config.schemaName.firstUppercased).\(schemaTypeNamespace)"
}
}()

Expand All @@ -115,7 +115,7 @@ extension TemplateRenderer {
\(ImportStatementTemplate.Operation.template(for: config))

\(if: config.output.operations.isInModule && !config.output.schemaTypes.isInModule,
template.wrappedInNamespace(config.schemaName),
template.wrappedInNamespace(config.schemaName.firstUppercased),
else:
template)
"""
Expand Down Expand Up @@ -154,7 +154,7 @@ extension TemplateString {
fileprivate func wrappedInNamespace(_ namespace: String) -> Self {
TemplateString(
"""
public extension \(namespace.firstUppercased) {
public extension \(namespace) {
\(self)
}
"""
Expand Down
2 changes: 0 additions & 2 deletions Sources/ApolloCodegenLib/Templates/UnionTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import Foundation
/// Provides the format to convert a [GraphQL Union](https://spec.graphql.org/draft/#sec-Unions)
/// into Swift code.
struct UnionTemplate: TemplateRenderer {
/// Module name.
let moduleName: String
/// IR representation of source [GraphQL Union](https://spec.graphql.org/draft/#sec-Unions).
let graphqlUnion: GraphQLUnionType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,61 @@ class SchemaModuleFileGeneratorTests: XCTestCase {
expect(self.mockFileManager.allClosuresCalled).to(beTrue())
}

func test__generate__givenModuleType_none_shouldGenerateNamespaceFile() throws {
func test__generate__givenModuleTypeEmbeddedInTarget_lowercaseSchemaName_shouldGenerateNamespaceFileWithCapitalizedName() throws {
// given
let fileURL = rootURL.appendingPathComponent("ModuleTestSchema.graphql.swift")
let fileURL = rootURL.appendingPathComponent("Schema.graphql.swift")

let configuration = ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock(
.embeddedInTarget(name: "MockApplication"),
schemaName: "ModuleTestSchema",
schemaName: "schema",
to: rootURL.path
))

mockFileManager.mock(closure: .createFile({ path, data, attributes in
// then
expect(path).to(equal(fileURL.path))

return true
}))

// when
try SchemaModuleFileGenerator.generate(configuration, fileManager: mockFileManager)

// then
expect(self.mockFileManager.allClosuresCalled).to(beTrue())
}

func test__generate__givenModuleTypeEmbeddedInTarget_uppercaseSchemaName_shouldGenerateNamespaceFileWithUppercaseName() throws {
// given
let fileURL = rootURL.appendingPathComponent("SCHEMA.graphql.swift")

let configuration = ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock(
.embeddedInTarget(name: "MockApplication"),
schemaName: "SCHEMA",
to: rootURL.path
))

mockFileManager.mock(closure: .createFile({ path, data, attributes in
// then
expect(path).to(equal(fileURL.path))

return true
}))

// when
try SchemaModuleFileGenerator.generate(configuration, fileManager: mockFileManager)

// then
expect(self.mockFileManager.allClosuresCalled).to(beTrue())
}

func test__generate__givenModuleTypeEmbeddedInTarget_capitalizedSchemaName_shouldGenerateNamespaceFileWithCapitalizedName() throws {
// given
let fileURL = rootURL.appendingPathComponent("MySchema.graphql.swift")

let configuration = ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock(
.embeddedInTarget(name: "MockApplication"),
schemaName: "MySchema",
to: rootURL.path
))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class UnionFileGeneratorTests: XCTestCase {
private func buildSubject() {
subject = UnionFileGenerator(
graphqlUnion: graphqlUnion,
schemaName: "MockSchema",
config: ApolloCodegen.ConfigurationContext(config: ApolloCodegenConfiguration.mock())
)
}
Expand Down
Loading