Skip to content

Commit 17a6f7d

Browse files
committed
Add a test for when a resolver returns a failed Future (as opposed to throwing an error )and fix the implementation of completeValueCatchingError
1 parent 2ba73c3 commit 17a6f7d

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

Tests/GraphQLTests/FieldExecutionStrategyTests/FieldExecutionStrategyTests.swift

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ class FieldExecutionStrategyTests: XCTestCase {
3838
msg: "\(info.fieldName): \(info.path.last as! String)"
3939
)
4040
}
41+
),
42+
"futureBang": GraphQLField(
43+
type: GraphQLString,
44+
resolve: { (_, _, _, eventLoopGroup, info: GraphQLResolveInfo) in
45+
let g = DispatchGroup()
46+
g.enter()
47+
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 0.1) {
48+
g.leave()
49+
}
50+
g.wait()
51+
return eventLoopGroup.next().newFailedFuture(error: StrategyError.exampleError(
52+
msg: "\(info.fieldName): \(info.path.last as! String)"
53+
))
54+
}
4155
)
4256
]
4357
)
@@ -81,8 +95,24 @@ class FieldExecutionStrategyTests: XCTestCase {
8195
]
8296
]
8397
]
98+
99+
let singleFailedFutureQuery = "{ futureBang }"
100+
let singleFailedFutureExpected: Map = [
101+
"data": [
102+
"futureBang": nil
103+
],
104+
"errors": [
105+
[
106+
"locations": [
107+
["column": 3, "line": 1]
108+
],
109+
"message": "exampleError(msg: \"futureBang: futureBang\")",
110+
"path":["futureBang"]
111+
]
112+
]
113+
]
84114

85-
let multiThrowsQuery = "{ a: bang b: bang c: bang d: bang e: bang f: bang g: bang h: bang i: bang j: bang }"
115+
let multiThrowsQuery = "{ a: bang b: bang c: bang d: bang e: bang f: bang g: bang h: bang i: bang j: futureBang }"
86116
let multiThrowsExpectedData: Map = [
87117
"a": nil,
88118
"b": nil,
@@ -163,7 +193,7 @@ class FieldExecutionStrategyTests: XCTestCase {
163193
"locations": [
164194
["column": 75, "line": 1]
165195
],
166-
"message": "exampleError(msg: \"bang: j\")",
196+
"message": "exampleError(msg: \"futureBang: j\")",
167197
"path":["j"]
168198
],
169199
]
@@ -210,6 +240,22 @@ class FieldExecutionStrategyTests: XCTestCase {
210240
XCTAssertEqual(result.value, singleThrowsExpected)
211241
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
212242
}
243+
244+
func testSerialFieldExecutionStrategyWithSingleFieldFailedFuture() throws {
245+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
246+
defer {
247+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
248+
}
249+
250+
let result = try timing(try graphql(
251+
queryStrategy: SerialFieldExecutionStrategy(),
252+
schema: schema,
253+
request: singleFailedFutureQuery,
254+
eventLoopGroup: eventLoopGroup
255+
).wait())
256+
XCTAssertEqual(result.value, singleFailedFutureExpected)
257+
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
258+
}
213259

214260
func testSerialFieldExecutionStrategyWithMultipleFields() throws {
215261
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
@@ -324,6 +370,7 @@ extension FieldExecutionStrategyTests {
324370
return [
325371
("testSerialFieldExecutionStrategyWithSingleField", testSerialFieldExecutionStrategyWithSingleField),
326372
("testSerialFieldExecutionStrategyWithSingleFieldError", testSerialFieldExecutionStrategyWithSingleFieldError),
373+
("testSerialFieldExecutionStrategyWithSingleFieldFailedFuture", testSerialFieldExecutionStrategyWithSingleFieldFailedFuture),
327374
("testSerialFieldExecutionStrategyWithMultipleFields", testSerialFieldExecutionStrategyWithMultipleFields),
328375
("testSerialFieldExecutionStrategyWithMultipleFieldErrors", testSerialFieldExecutionStrategyWithMultipleFieldErrors),
329376
("testConcurrentDispatchFieldExecutionStrategyWithSingleField", testConcurrentDispatchFieldExecutionStrategyWithSingleField),

0 commit comments

Comments
 (0)