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: Typename collisions in generated test mocks #2762

Merged
merged 2 commits into from
Jan 9, 2023
Merged
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 @@ -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