Skip to content

Commit

Permalink
Merge pull request #43 from NicholasBellucci/feature/foreach-weavable
Browse files Browse the repository at this point in the history
Feature/foreach weavable
  • Loading branch information
NicholasBellucci authored Nov 9, 2020
2 parents 5c4952b + 8a519be commit d984203
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
46 changes: 46 additions & 0 deletions Sources/SociableWeaver/Helpers/ForEachWeavable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public struct ForEachWeavable: Directive {
private var objects: [ObjectWeavable] = []

var include: Bool = true
var skip: Bool = false

public init<T>(_ 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: " ")
}
}
23 changes: 23 additions & 0 deletions Tests/SociableWeaverTests/SociableWeaverBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit d984203

Please sign in to comment.