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

Don't add a raw type to Enums with no generated cases #519

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -488,7 +488,48 @@ class EnumTemplateTests: XCTestCase {
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
}
}


// MARK: - No Enum Cases Tests

func test_render_givenEmptyEnum_shouldRenderWithoutRawType() {
// given
buildSubject(values: [], config: .mock(.other))

let expected = """
public enum TestEnum: EnumType {
"""

// when
let actual = renderSubject()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
}

func test_render_givenEnumWithAllDeprecatedValues_shouldRenderWithoutRawType() {
// given
buildSubject(
values: [
("ONE", "Deprecated for tests", "Doc: One", nil),
("TWO", "Deprecated for tests", "Doc: Two", nil),
("THREE", "Deprecated for tests", nil, nil)
],
config: ApolloCodegenConfiguration.mock(options: .init(
deprecatedEnumCases: .exclude
))
)

let expected = """
enum TestEnum: EnumType {
"""

// when
let actual = renderSubject()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
}

// MARK: - Schema Customization Tests

func test__render__givenEnumAndValues_withCustomNames_shouldRenderWithCustomNames() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ struct EnumTemplate: TemplateRenderer {
func renderBodyTemplate(
nonFatalErrorRecorder: ApolloCodegen.NonFatalError.Recorder
) -> TemplateString {
TemplateString(
let omitRawType = graphqlEnum.values.isEmpty
|| (config.options.deprecatedEnumCases == .exclude && graphqlEnum.values.allSatisfy(\.isDeprecated))

return TemplateString(
"""
\(documentation: graphqlEnum.documentation, config: config)
\(graphqlEnum.name.typeNameDocumentation)
\(accessControlModifier(for: .parent))\
enum \(graphqlEnum.render(as: .typename)): String, EnumType {
enum \(graphqlEnum.render(as: .typename)): \(omitRawType ? "" : "String, ")EnumType {
\(graphqlEnum.values.compactMap({
enumCase(for: $0)
}), separator: "\n")
Expand Down
Loading