Skip to content

Commit 16e5ec6

Browse files
committed
- Addressed PR feedback.
- Moved reference to extensions object into function level scope to be able to assign it to the result even if data doesn't exist in the response. - Updated tests.
1 parent e69ac35 commit 16e5ec6

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

Sources/Apollo/GraphQLResponse.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
2020
errors = nil
2121
}
2222

23+
let extensions = body["extensions"] as? JSONObject
24+
2325
if let dataEntry = body["data"] as? JSONObject {
2426
let executor = GraphQLExecutor { object, info in
2527
return .result(.success(object[info.responseKeyForField]))
@@ -30,7 +32,6 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
3032
let mapper = GraphQLSelectionSetMapper<Data>()
3133
let normalizer = GraphQLResultNormalizer()
3234
let dependencyTracker = GraphQLDependencyTracker()
33-
let extensions = body["extensions"] as? JSONObject
3435

3536
return firstly {
3637
try executor.execute(selections: Data.selections,
@@ -51,7 +52,7 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
5152
} else {
5253
return Promise(fulfilled: (
5354
GraphQLResult(data: nil,
54-
extensions: nil,
55+
extensions: extensions,
5556
errors: errors,
5657
source: .server,
5758
dependentKeys: nil),
@@ -70,12 +71,12 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
7071

7172
func parseResultFast() throws -> GraphQLResult<Data> {
7273
let errors = self.parseErrorsOnlyFast()
74+
let extensions = body["extensions"] as? JSONObject
7375

7476
if let dataEntry = body["data"] as? JSONObject {
7577
let data = try decode(selectionSet: Data.self,
7678
from: dataEntry,
7779
variables: variables)
78-
let extensions = body["extensions"] as? JSONObject
7980

8081
return GraphQLResult(data: data,
8182
extensions: extensions,
@@ -84,7 +85,7 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
8485
dependentKeys: nil)
8586
} else {
8687
return GraphQLResult(data: nil,
87-
extensions: nil,
88+
extensions: extensions,
8889
errors: errors,
8990
source: .server,
9091
dependentKeys: nil)

Tests/ApolloTests/ParseQueryResponseTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class ParseQueryResponseTests: XCTestCase {
356356
XCTAssertNil(result.extensions)
357357
}
358358

359-
func testExtensionsEntryNullWhenDataEntryNotProvidedInResponse() throws {
359+
func testExtensionsEntryNotNullWhenDataEntryNotProvidedInResponse() throws {
360360
let query = HumanQuery(id: "9999")
361361

362362
let response = GraphQLResponse(operation: query, body: [
@@ -365,7 +365,7 @@ class ParseQueryResponseTests: XCTestCase {
365365

366366
let (result, _) = try response.parseResult().await()
367367

368-
XCTAssertNil(result.extensions)
368+
XCTAssertNotNil(result.extensions)
369369
}
370370

371371
// MARK: Mutations

0 commit comments

Comments
 (0)