Skip to content

Commit

Permalink
fix: Typename collisions in generated test mocks (#2762)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored Jan 9, 2023
1 parent e8f30b7 commit 923d4cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,46 +59,12 @@ extension GraphQLType {
replacingNamedTypeWith newTypeName: String? = nil,
config: ApolloCodegenConfiguration
) -> String {

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

switch self {
case let .entity(type as GraphQLNamedType), let .inputObject(type as GraphQLNamedType):
let typeName = newTypeName ?? type.testMockFieldTypeName.firstUppercased
return TemplateString("\(typeName)\(if: !containedInNonNull, "?")").description

case let .scalar(type):
let typeName = newTypeName ?? type.swiftName.firstUppercased

return TemplateString(
"\(if: !type.isSwiftType, "\(schemaModuleName)")\(typeName)\(if: !containedInNonNull, "?")"
).description

case let .enum(type as GraphQLNamedType):
let typeName = newTypeName ?? type.name.firstUppercased
let enumType = "GraphQLEnum<\(schemaModuleName)\(typeName)>"

return containedInNonNull ? enumType : "\(enumType)?"

case let .nonNull(ofType):
return ofType.renderedAsTestMockField(
containedInNonNull: true,
replacingNamedTypeWith: newTypeName,
config: config
)

case let .list(ofType):
let rendered = ofType.renderedAsTestMockField(
containedInNonNull: false,
replacingNamedTypeWith: newTypeName,
config: config
)
let inner = "[\(rendered)]"

return containedInNonNull ? inner : "\(inner)?"
}
renderType(
in: .testMockField(),
containedInNonNull: containedInNonNull,
replacingNamedTypeWith: newTypeName,
config: config
)
}

// MARK: Input Value
Expand Down Expand Up @@ -183,9 +149,16 @@ extension GraphQLNamedType {
replacingNamedTypeWith newTypeName: String? = nil,
config: ApolloCodegenConfiguration
) -> String {
lazy var typeName = { newTypeName ?? self.swiftName.firstUppercased }()

lazy var schemaModuleName: String = {
let typeName: String = {
if case .testMockField = context {
return newTypeName ?? testMockFieldTypeName.firstUppercased
} else {
return newTypeName ?? self.swiftName.firstUppercased
}
}()

let schemaModuleName: String = {
switch self {
case is GraphQLCompositeType:
return ""
Expand All @@ -195,14 +168,14 @@ extension GraphQLNamedType {

default:
switch context {
case .testMockField, .inputValue:
case .inputValue:
if !config.output.operations.isInModule {
fallthrough
} else {
return ""
}

case .selectionSetField:
case .selectionSetField, .testMockField:
return "\(config.schemaName.firstUppercased)."
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class MockObjectTemplateTests: XCTestCase {

let expected = """
public struct MockFields {
@Field<CustomScalar>("customScalar") public var customScalar
@Field<TestSchema.CustomScalar>("customScalar") public var customScalar
@Field<Cat>("object") public var object
@Field<[Cat]>("objectList") public var objectList
@Field<[[Cat]]>("objectNestedList") public var objectNestedList
Expand Down Expand Up @@ -207,8 +207,8 @@ class MockObjectTemplateTests: XCTestCase {

let expected = """
public struct MockFields {
@Field<CustomScalar>("customScalar") public var customScalar
@Field<GraphQLEnum<EnumType>>("enumType") public var enumType
@Field<TestSchema.CustomScalar>("customScalar") public var customScalar
@Field<GraphQLEnum<TestSchema.EnumType>>("enumType") public var enumType
@Field<Cat>("object") public var object
}
"""
Expand Down Expand Up @@ -467,6 +467,7 @@ class MockObjectTemplateTests: XCTestCase {
"unionList": .mock("unionList", type: .list(.nonNull(Pet))),
"unionNestedList": .mock("unionNestedList", type: .list(.nonNull(.list(.nonNull(Pet))))),
"unionOptionalList": .mock("unionOptionalList", type: .list(Pet)),
"enumType": .mock("enumType", type: .enum(.mock(name: "enumType"))),
]

ir.fieldCollector.add(
Expand All @@ -481,7 +482,8 @@ class MockObjectTemplateTests: XCTestCase {
public extension Mock where O == Dog {
convenience init(
customScalar: CustomScalar? = nil,
customScalar: TestSchema.CustomScalar? = nil,
enumType: GraphQLEnum<TestSchema.EnumType>? = nil,
interface: AnyMock? = nil,
interfaceList: [AnyMock]? = nil,
interfaceNestedList: [[AnyMock]]? = nil,
Expand All @@ -499,6 +501,7 @@ class MockObjectTemplateTests: XCTestCase {
) {
self.init()
self.customScalar = customScalar
self.enumType = enumType
self.interface = interface
self.interfaceList = interfaceList
self.interfaceNestedList = interfaceNestedList
Expand Down

0 comments on commit 923d4cc

Please sign in to comment.