Skip to content

Commit 1d31d8c

Browse files
authored
Merge pull request #46 from SportlabsTechnology/master
Fix 'completeValueCatchingError' to handle failed futures
2 parents 2ba73c3 + a12f7c3 commit 1d31d8c

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/GraphQL/Execution/Execute.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func completeValueCatchingError(
789789
result: result
790790
).mapIfError { error -> Any? in
791791
guard let error = error as? GraphQLError else {
792-
fatalError()
792+
fatalError()
793793
}
794794
exeContext.append(error: error)
795795
return nil
@@ -824,8 +824,9 @@ func completeValueWithLocatedError(
824824
info: info,
825825
path: path,
826826
result: result
827-
)
828-
827+
).thenIfErrorThrowing { error -> Any? in
828+
throw locatedError(originalError: error, nodes: fieldASTs, path: path)
829+
}
829830
return completed
830831
} catch {
831832
throw locatedError(

Tests/GraphQLTests/FieldExecutionStrategyTests/FieldExecutionStrategyTests.swift

Lines changed: 56 additions & 35 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
]
@@ -178,12 +208,18 @@ class FieldExecutionStrategyTests: XCTestCase {
178208
seconds: seconds
179209
)
180210
}
211+
212+
private var eventLoopGroup: EventLoopGroup!
213+
214+
override func setUp() {
215+
eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
216+
}
217+
218+
override func tearDown() {
219+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
220+
}
181221

182222
func testSerialFieldExecutionStrategyWithSingleField() throws {
183-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
184-
defer {
185-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
186-
}
187223

188224
let result = try timing(try graphql(
189225
queryStrategy: SerialFieldExecutionStrategy(),
@@ -196,11 +232,7 @@ class FieldExecutionStrategyTests: XCTestCase {
196232
}
197233

198234
func testSerialFieldExecutionStrategyWithSingleFieldError() throws {
199-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
200-
defer {
201-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
202-
}
203-
235+
204236
let result = try timing(try graphql(
205237
queryStrategy: SerialFieldExecutionStrategy(),
206238
schema: schema,
@@ -210,12 +242,20 @@ class FieldExecutionStrategyTests: XCTestCase {
210242
XCTAssertEqual(result.value, singleThrowsExpected)
211243
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
212244
}
245+
246+
func testSerialFieldExecutionStrategyWithSingleFieldFailedFuture() throws {
247+
248+
let result = try timing(try graphql(
249+
queryStrategy: SerialFieldExecutionStrategy(),
250+
schema: schema,
251+
request: singleFailedFutureQuery,
252+
eventLoopGroup: eventLoopGroup
253+
).wait())
254+
XCTAssertEqual(result.value, singleFailedFutureExpected)
255+
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
256+
}
213257

214258
func testSerialFieldExecutionStrategyWithMultipleFields() throws {
215-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
216-
defer {
217-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
218-
}
219259

220260
let result = try timing(try graphql(
221261
queryStrategy: SerialFieldExecutionStrategy(),
@@ -228,10 +268,6 @@ class FieldExecutionStrategyTests: XCTestCase {
228268
}
229269

230270
func testSerialFieldExecutionStrategyWithMultipleFieldErrors() throws {
231-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
232-
defer {
233-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
234-
}
235271

236272
let result = try timing(try graphql(
237273
queryStrategy: SerialFieldExecutionStrategy(),
@@ -249,10 +285,6 @@ class FieldExecutionStrategyTests: XCTestCase {
249285
}
250286

251287
func testConcurrentDispatchFieldExecutionStrategyWithSingleField() throws {
252-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
253-
defer {
254-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
255-
}
256288

257289
let result = try timing(try graphql(
258290
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
@@ -265,10 +297,6 @@ class FieldExecutionStrategyTests: XCTestCase {
265297
}
266298

267299
func testConcurrentDispatchFieldExecutionStrategyWithSingleFieldError() throws {
268-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
269-
defer {
270-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
271-
}
272300

273301
let result = try timing(try graphql(
274302
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
@@ -281,10 +309,6 @@ class FieldExecutionStrategyTests: XCTestCase {
281309
}
282310

283311
func testConcurrentDispatchFieldExecutionStrategyWithMultipleFields() throws {
284-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
285-
defer {
286-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
287-
}
288312

289313
let result = try timing(try graphql(
290314
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
@@ -297,10 +321,6 @@ class FieldExecutionStrategyTests: XCTestCase {
297321
}
298322

299323
func testConcurrentDispatchFieldExecutionStrategyWithMultipleFieldErrors() throws {
300-
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
301-
defer {
302-
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
303-
}
304324

305325
let result = try timing(try graphql(
306326
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
@@ -324,6 +344,7 @@ extension FieldExecutionStrategyTests {
324344
return [
325345
("testSerialFieldExecutionStrategyWithSingleField", testSerialFieldExecutionStrategyWithSingleField),
326346
("testSerialFieldExecutionStrategyWithSingleFieldError", testSerialFieldExecutionStrategyWithSingleFieldError),
347+
("testSerialFieldExecutionStrategyWithSingleFieldFailedFuture", testSerialFieldExecutionStrategyWithSingleFieldFailedFuture),
327348
("testSerialFieldExecutionStrategyWithMultipleFields", testSerialFieldExecutionStrategyWithMultipleFields),
328349
("testSerialFieldExecutionStrategyWithMultipleFieldErrors", testSerialFieldExecutionStrategyWithMultipleFieldErrors),
329350
("testConcurrentDispatchFieldExecutionStrategyWithSingleField", testConcurrentDispatchFieldExecutionStrategyWithSingleField),

0 commit comments

Comments
 (0)