From 66139d297997ab326638e4643091e795795e2b7d Mon Sep 17 00:00:00 2001 From: Nicholas Bellucci Date: Sun, 8 Nov 2020 19:07:22 -0500 Subject: [PATCH 1/2] Added ForEachWeavable and other updates --- .../FunctionBuilders/OperationBuilder.swift | 4 ++ .../Helpers/ForEachWeavable.swift | 48 +++++++++++++++++++ .../SociableWeaverBuilderTests.swift | 23 +++++++++ 3 files changed, 75 insertions(+) create mode 100644 Sources/SociableWeaver/Helpers/ForEachWeavable.swift diff --git a/Sources/SociableWeaver/FunctionBuilders/OperationBuilder.swift b/Sources/SociableWeaver/FunctionBuilders/OperationBuilder.swift index 8713739..10757b0 100644 --- a/Sources/SociableWeaver/FunctionBuilders/OperationBuilder.swift +++ b/Sources/SociableWeaver/FunctionBuilders/OperationBuilder.swift @@ -16,6 +16,10 @@ public struct OperationBuilder { weavables.append(field) } else if let fragment = $0 as? Fragment { fragments.append(fragment) + } else if let forEach = $0 as? ForEachWeavable { + if forEach.skip || !forEach.include { return } + + weavables.append(forEach) } } diff --git a/Sources/SociableWeaver/Helpers/ForEachWeavable.swift b/Sources/SociableWeaver/Helpers/ForEachWeavable.swift new file mode 100644 index 0000000..690ffd1 --- /dev/null +++ b/Sources/SociableWeaver/Helpers/ForEachWeavable.swift @@ -0,0 +1,48 @@ +public struct ForEachWeavable: Directive { + private var objects: [ObjectWeavable] = [] + + var include: Bool = true + var skip: Bool = false + + init(_ array: [T], content: @escaping (T) -> ObjectWeavable) { + array.forEach { + objects.append(content($0)) + } + } +} + +public extension ForEachWeavable { + /** + Only include this object in the operation if the argument is true. + + - Parameter argument: A boolean argument. + - Returns: An `Object` with its include value set. + */ + func include(if argument: Bool) -> ForEachWeavable { + var copy = self + copy.include = argument + return copy + } + + /** + Skip this object if the argument is true + + - Parameter argument: A boolean argument. + - Returns: An `Object` with its skip value set. + */ + func skip(if argument: Bool) -> ForEachWeavable { + var copy = self + copy.skip = argument + return copy + } +} + +extension ForEachWeavable: ObjectWeavable { + public var description: String { + objects.map { $0.description }.joined(separator: " ") + } + + public var debugDescription: String { + objects.map { $0.debugDescription }.joined(separator: " ") + } +} diff --git a/Tests/SociableWeaverTests/SociableWeaverBuilderTests.swift b/Tests/SociableWeaverTests/SociableWeaverBuilderTests.swift index 1d8149e..a1f14af 100644 --- a/Tests/SociableWeaverTests/SociableWeaverBuilderTests.swift +++ b/Tests/SociableWeaverTests/SociableWeaverBuilderTests.swift @@ -235,6 +235,29 @@ final class SociableWeaverBuilderTests: XCTestCase { XCTAssertEqual(String(describing: query), expected) } + func testForEachWeavable() { + let authors = [ + Author(id: "1", name: "John", age: 17, birthplace: [:]), + Author(id: "2", name: "Jane", age: 29, birthplace: [:]), + Author(id: "3", name: "Adam", age: 41, birthplace: [:]) + ] + + let query = Weave(.query) { + ForEachWeavable(authors) { author in + Object("postsForAuthor") { + Field(Author.CodingKeys.id) + Field(Author.CodingKeys.name) + Field(Author.CodingKeys.age) + Field(Author.CodingKeys.birthplace) + } + .argument(key: "id", value: author.id) + } + } + + let expected = "query { postsForAuthor(id: \"1\") { id name age birthplace } postsForAuthor(id: \"2\") { id name age birthplace } postsForAuthor(id: \"3\") { id name age birthplace } }" + XCTAssertEqual(String(describing: query), expected) + } + static var allTests = [ ("testBuildField", testBuildField), ("testBuildObject", testBuildObject), From 8a519be0235bffd0c668487ab153adaecfd5b162 Mon Sep 17 00:00:00 2001 From: Nicholas Bellucci Date: Mon, 9 Nov 2020 11:17:25 -0500 Subject: [PATCH 2/2] Added public init --- Sources/SociableWeaver/Helpers/ForEachWeavable.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sources/SociableWeaver/Helpers/ForEachWeavable.swift b/Sources/SociableWeaver/Helpers/ForEachWeavable.swift index 690ffd1..fb1c79b 100644 --- a/Sources/SociableWeaver/Helpers/ForEachWeavable.swift +++ b/Sources/SociableWeaver/Helpers/ForEachWeavable.swift @@ -4,10 +4,8 @@ public struct ForEachWeavable: Directive { var include: Bool = true var skip: Bool = false - init(_ array: [T], content: @escaping (T) -> ObjectWeavable) { - array.forEach { - objects.append(content($0)) - } + public init(_ array: [T], content: @escaping (T) -> ObjectWeavable) { + array.forEach { objects.append(content($0)) } } }