Skip to content

Commit 4e0c879

Browse files
feat: Adds concatAST
1 parent cc44dfd commit 4e0c879

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Provided a collection of ASTs, presumably each from different files,
3+
* concatenate the ASTs together into batched AST, useful for validating many
4+
* GraphQL source files which together represent one conceptual application.
5+
*/
6+
func concatAST(
7+
documents: [Document]
8+
) -> Document {
9+
var definitions: [Definition] = []
10+
for doc in documents {
11+
definitions.append(contentsOf: doc.definitions)
12+
}
13+
return Document(definitions: definitions)
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@testable import GraphQL
2+
import XCTest
3+
4+
class ConcatASTTests: XCTestCase {
5+
func testConcatenatesTwoASTsTogether() throws {
6+
let sourceA = Source(body: """
7+
{ a, b, ...Frag }
8+
""")
9+
10+
let sourceB = Source(body: """
11+
fragment Frag on T {
12+
c
13+
}
14+
""")
15+
16+
let astA = try parse(source: sourceA)
17+
let astB = try parse(source: sourceB)
18+
let astC = concatAST(documents: [astA, astB])
19+
20+
XCTAssertEqual(
21+
print(ast: astC),
22+
"""
23+
{
24+
a
25+
b
26+
...Frag
27+
}
28+
29+
fragment Frag on T {
30+
c
31+
}
32+
"""
33+
)
34+
}
35+
}

0 commit comments

Comments
 (0)