Skip to content

Commit 72a9fc9

Browse files
authored
Multiline deprecation reasons are now supported (#169)
* Updates API.swift * Multiline deprecation reasons are now supported
1 parent ab840d9 commit 72a9fc9

File tree

5 files changed

+19606
-6913
lines changed

5 files changed

+19606
-6913
lines changed

Sources/SwiftGraphQLCodegen/Generator/Field.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ extension Field {
9292
// NOTE: It's possible that a string contains double-quoted characters in deprecation reason.
9393
// http://spec.graphql.org/October2021/#sec-Language.Directives
9494
let message = deprecationReason?.replacingOccurrences(of: "\"", with: "\\\"") ?? ""
95-
return "@available(*, deprecated, message: \"\(message)\")"
95+
if message.contains("\n") {
96+
return "@available(*, deprecated, message: \"\"\"\n\(message)\n\"\"\")"
97+
} else {
98+
return "@available(*, deprecated, message: \"\(message)\")"
99+
}
96100
}
97101
return ""
98102
}

Tests/SwiftGraphQLCodegenTests/Generator/FieldTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,51 @@ final class FieldTests: XCTestCase {
7878
""")
7979
}
8080

81+
func testMultilineDeprecationReason() throws {
82+
let field = Field(
83+
name: "id",
84+
description: "Deprecation\nMultiline.",
85+
args: [],
86+
type: .named(.scalar("ID")),
87+
isDeprecated: true,
88+
deprecationReason: """
89+
Use ID instead.
90+
See: <link> for more detail.
91+
"""
92+
)
93+
94+
let generated = try field.getDynamicSelection(
95+
parent: "TestType",
96+
context: Context.from(scalars: ["ID": "String"])
97+
).format()
98+
99+
let expected = """
100+
/// Deprecation
101+
/// Multiline.
102+
@available(*, deprecated, message: \"\"\"
103+
Use ID instead.
104+
See: <link> for more detail.
105+
\"\"\")
106+
public func id() throws -> String? {
107+
let field = GraphQLField.leaf(
108+
field: "id",
109+
parent: "TestType",
110+
arguments: []
111+
)
112+
self.__select(field)
113+
114+
switch self.__state {
115+
case .decoding:
116+
return try self.__decode(field: field.alias!) { try String?(from: $0) }
117+
case .selecting:
118+
return nil
119+
}
120+
}
121+
"""
122+
123+
generated.assertInlineSnapshot(matching: expected)
124+
}
125+
81126
// MARK: - Scalar
82127

83128
func testScalarField() throws {

0 commit comments

Comments
 (0)