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 unqualified scalar collision #2757

Merged
merged 4 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 @@ -44,50 +44,12 @@ extension GraphQLType {
replacingNamedTypeWith newTypeName: String? = nil,
config: ApolloCodegenConfiguration
) -> String {

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

switch self {
case let .entity(type as GraphQLNamedType):
let typeName = newTypeName ?? type.swiftName.firstUppercased
return containedInNonNull ? typeName : "\(typeName)?"

case let .inputObject(type as GraphQLNamedType):
let typeName = newTypeName ?? type.swiftName.firstUppercased
return TemplateString("\(schemaModuleName)\(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.renderedAsSelectionSetField(
containedInNonNull: true,
replacingNamedTypeWith: newTypeName,
config: config
)

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

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

// MARK: Mock Object Field
Expand Down Expand Up @@ -146,21 +108,63 @@ extension GraphQLType {
config: ApolloCodegenConfiguration
) -> String {
switch self {
case .entity:
preconditionFailure("Entities cannot be used as input values")
case let .nonNull(ofType):
return ofType.renderAsInputValue(inNullable: false, config: config)

case let .list(ofType):
let typeName = "[\(ofType.renderType(in: .inputValue, config: config))]"
return inNullable ? "GraphQLNullable<\(typeName)>" : typeName

case .enum, .scalar, .inputObject:
let typeName = self.renderedAsSelectionSetField(containedInNonNull: true, config: config)
default:
let typeName = renderType(in: .inputValue, containedInNonNull: true, config: config)
return inNullable ? "GraphQLNullable<\(typeName)>" : typeName
}
}

// MARK: - Render Type

private func renderType(
in context: RenderContext,
containedInNonNull: Bool = false,
replacingNamedTypeWith newTypeName: String? = nil,
config: ApolloCodegenConfiguration
) -> String {
switch self {
case
.entity(let type as GraphQLNamedType),
.scalar(let type as GraphQLNamedType),
.enum(let type as GraphQLNamedType),
.inputObject(let type as GraphQLNamedType):

let typeName = type.qualifiedRootTypeName(
in: context,
replacingNamedTypeWith: newTypeName,
config: config
).wrappedInGraphQLEnum(ifIsEnumType: self)

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

case let .nonNull(ofType):
return ofType.renderAsInputValue(inNullable: false, config: config)
return ofType.renderType(
in: context,
containedInNonNull: true,
replacingNamedTypeWith: newTypeName,
config: config
)

case let .list(ofType):
let typeName = "[\(ofType.renderedAsSelectionSetField(containedInNonNull: false, config: config))]"
return inNullable ? "GraphQLNullable<\(typeName)>" : typeName
let rendered = ofType.renderType(
in: context,
containedInNonNull: false,
replacingNamedTypeWith: newTypeName,
config: config
)
let inner = "[\(rendered)]"

return containedInNonNull ? inner : "\(inner)?"
}
}

}

extension GraphQLNamedType {
Expand All @@ -173,4 +177,47 @@ extension GraphQLNamedType {

return swiftName
}

fileprivate func qualifiedRootTypeName(
in context: GraphQLType.RenderContext,
replacingNamedTypeWith newTypeName: String? = nil,
config: ApolloCodegenConfiguration
) -> String {
lazy var typeName = { newTypeName ?? self.swiftName.firstUppercased }()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to change, my question is more out of curiosity.

Is there a benefit to typeName and schemaModuleName being defined as lazy? The return references both of them; it doesn't look like there is a logic path where they wouldn't be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there isn't anymore. In the old implementation, there we're paths that did not call them. So this is just a carryover from that. We can change these to let


lazy var schemaModuleName: String = {
switch self {
case is GraphQLCompositeType:
return ""

case let scalar as GraphQLScalarType where scalar.isSwiftType:
return ""

default:
switch context {
case .testMockField, .inputValue:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renderedAsTestMockField doesn't look like it flows through this new logic - should it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test mock one works as intended and I just decided not to mess with it to get this PR out. Next time there is a reason to make changes to that rendering, we may want to consider using this function, but I didn't want to break something else that was working at the moment.

if !config.output.operations.isInModule {
fallthrough
} else {
return ""
}

case .selectionSetField:
return "\(config.schemaName.firstUppercased)."
}
}
}()

return "\(schemaModuleName)\(typeName)"
}
}

fileprivate extension String {
func wrappedInGraphQLEnum(ifIsEnumType type: GraphQLType) -> String {
if case .enum = type {
return "GraphQLEnum<\(self)>"
} else {
return self
}
}
}
Loading