Skip to content

Commit fea49c0

Browse files
committed
Observe input extension
1 parent 14bdab6 commit fea49c0

3 files changed

Lines changed: 39 additions & 16 deletions

File tree

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let package = Package(
1919
.executable(name: "OtterCLI", targets: ["OtterCLI"]),
2020
],
2121
dependencies: [
22-
.package(url: "https://github.com/apple/swift-syntax.git", from: "601.0.1"),
22+
.package(url: "https://github.com/apple/swift-syntax", from: "601.0.1"),
2323
.package(url: "https://github.com/apple/swift-collections", from: "1.1.4"),
2424
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
2525
],

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -545,30 +545,47 @@ public struct SwiftLanguage: Language {
545545
for query: GeneratedQuery,
546546
input: GeneratedModel
547547
) {
548-
extensionOn("Query") {
549-
self.writer.write("Input == ", query.inputName)
550-
} builder: {
551-
writer.write("func execute(")
548+
let writeInput: () -> Void = {
552549
for (position, field) in input.fields.elements.positional() {
553550
writer.write(field.value.name, ": ", field.value.type.description)
554551

555552
if !position.isLast {
556553
writer.write(", ")
557554
}
558555
}
559-
556+
}
557+
558+
let initInput: () -> Void = {
559+
for (position, field) in input.fields.elements.positional() {
560+
writer.write(field.key, ": ", field.key)
561+
562+
if !position.isLast {
563+
writer.write(", ")
564+
}
565+
}
566+
}
567+
568+
extensionOn("Query") {
569+
self.writer.write("Input == ", query.inputName)
570+
} builder: {
571+
writer.write("func execute(")
572+
writeInput()
560573
writer.write(") async throws -> Output ")
574+
561575
writer.braces {
562576
writer.write(line: "try await execute(with: ", query.inputName, "(")
563-
564-
for (position, field) in input.fields.elements.positional() {
565-
writer.write(field.key, ": ", field.key)
566-
567-
if !position.isLast {
568-
writer.write(", ")
569-
}
570-
}
571-
577+
initInput()
578+
writer.write("))")
579+
}
580+
581+
writer.blankLine()
582+
writer.write(line: "func observe(")
583+
writeInput()
584+
writer.write(") -> any QueryObservation<Output> ")
585+
586+
writer.braces {
587+
writer.write(line: "observe(with: ", query.inputName, "(")
588+
initInput()
572589
writer.write("))")
573590
}
574591
}
@@ -595,6 +612,7 @@ public struct SwiftLanguage: Language {
595612
}
596613

597614
writer.braces {
615+
writer.blankLine()
598616
builder()
599617
}
600618
writer.blankLine()

Tests/CompilerTests/Gen/Swift.output

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,14 @@ typealias SelectUserByIdQuery = Query<Int, User?>
356356
typealias SelectUserByIdsQuery = Query<[Int], [User]>
357357
typealias SelectUserByNameQuery = Query<String, [User]>
358358
typealias SelectUserWithManyInputsQuery = Query<SelectUserWithManyInputsInput, SelectUserWithManyInputsOutput?>
359-
extension Query where Input == SelectUserWithManyInputsInput {func execute(id: Int, firstName: String) async throws -> Output {
359+
extension Query where Input == SelectUserWithManyInputsInput {
360+
func execute(id: Int, firstName: String) async throws -> Output {
360361
try await execute(with: SelectUserWithManyInputsInput(id: id, firstName: firstName))
361362
}
363+
364+
func observe(id: Int, firstName: String) -> any QueryObservation<Output> {
365+
observe(with: SelectUserWithManyInputsInput(id: id, firstName: firstName))
366+
}
362367
}
363368

364369
typealias SelectWithInterestQuery = Query<(), [SelectWithInterestOutput]>

0 commit comments

Comments
 (0)