Skip to content

Commit afa819c

Browse files
Merge pull request #1370 from paulkite/add-extensions-to-graphqlresult
Add extensions property to GraphQLResult.
2 parents c6c0fa8 + 16e5ec6 commit afa819c

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

Sources/Apollo/ApolloStore.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public final class ApolloStore {
175175
accumulator: zip(mapper, dependencyTracker))
176176
}.map { (data: Query.Data, dependentKeys: Set<CacheKey>) in
177177
GraphQLResult(data: data,
178+
extensions: nil,
178179
errors: nil,
179180
source:.cache,
180181
dependentKeys: dependentKeys)

Sources/Apollo/GraphQLResponse.swift

Lines changed: 10 additions & 3 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]))
@@ -40,15 +42,17 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
4042
}.map { (data, records, dependentKeys) in
4143
(
4244
GraphQLResult(data: data,
43-
errors: errors,
44-
source: .server,
45-
dependentKeys: dependentKeys),
45+
extensions: extensions,
46+
errors: errors,
47+
source: .server,
48+
dependentKeys: dependentKeys),
4649
records
4750
)
4851
}
4952
} else {
5053
return Promise(fulfilled: (
5154
GraphQLResult(data: nil,
55+
extensions: extensions,
5256
errors: errors,
5357
source: .server,
5458
dependentKeys: nil),
@@ -67,18 +71,21 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
6771

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

7176
if let dataEntry = body["data"] as? JSONObject {
7277
let data = try decode(selectionSet: Data.self,
7378
from: dataEntry,
7479
variables: variables)
7580

7681
return GraphQLResult(data: data,
82+
extensions: extensions,
7783
errors: errors,
7884
source: .server,
7985
dependentKeys: nil)
8086
} else {
8187
return GraphQLResult(data: nil,
88+
extensions: extensions,
8289
errors: errors,
8390
source: .server,
8491
dependentKeys: nil)

Sources/Apollo/GraphQLResult.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public struct GraphQLResult<Data> {
44
public let data: Data?
55
/// A list of errors, or `nil` if the operation completed without encountering any errors.
66
public let errors: [GraphQLError]?
7+
/// A dictionary which services can use however they see fit to provide additional information to clients.
8+
public let extensions: [String: Any]?
79

810
/// Represents source of data
911
public enum Source {
@@ -16,10 +18,12 @@ public struct GraphQLResult<Data> {
1618
let dependentKeys: Set<CacheKey>?
1719

1820
public init(data: Data?,
21+
extensions: [String: Any]?,
1922
errors: [GraphQLError]?,
2023
source: Source,
2124
dependentKeys: Set<CacheKey>?) {
2225
self.data = data
26+
self.extensions = extensions
2327
self.errors = errors
2428
self.source = source
2529
self.dependentKeys = dependentKeys

Tests/ApolloTests/ParseQueryResponseTests.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,60 @@ class ParseQueryResponseTests: XCTestCase {
314314
}
315315
}
316316

317+
func testExtensionsEntryNotNullWhenProvidedInResponseAccompanyingDataEntry() throws {
318+
let query = HumanQuery(id: "9999")
319+
320+
let response = GraphQLResponse(operation: query, body: [
321+
"data": ["human": NSNull()],
322+
"extensions": [:]
323+
])
324+
325+
let (result, _) = try response.parseResult().await()
326+
327+
XCTAssertNotNil(result.extensions)
328+
}
329+
330+
func testExtensionsValuesWhenPopulatedInResponse() throws {
331+
let query = HumanQuery(id: "9999")
332+
333+
let response = GraphQLResponse(operation: query, body: [
334+
"data": ["human": NSNull()],
335+
"extensions": ["parentKey": ["childKey": "someValue"]]
336+
])
337+
338+
let (result, _) = try response.parseResult().await()
339+
let extensionsDictionary = result.extensions
340+
let childDictionary = extensionsDictionary?["parentKey"] as? JSONObject
341+
342+
XCTAssertNotNil(extensionsDictionary)
343+
XCTAssertNotNil(childDictionary)
344+
XCTAssertEqual(childDictionary, ["childKey": "someValue"])
345+
}
346+
347+
func testExtensionsEntryNullWhenNotProvidedInResponse() throws {
348+
let query = HumanQuery(id: "9999")
349+
350+
let response = GraphQLResponse(operation: query, body: [
351+
"data": ["human": NSNull()]
352+
])
353+
354+
let (result, _) = try response.parseResult().await()
355+
356+
XCTAssertNil(result.extensions)
357+
}
358+
359+
func testExtensionsEntryNotNullWhenDataEntryNotProvidedInResponse() throws {
360+
let query = HumanQuery(id: "9999")
361+
362+
let response = GraphQLResponse(operation: query, body: [
363+
"extensions": [:]
364+
])
365+
366+
let (result, _) = try response.parseResult().await()
367+
368+
XCTAssertNotNil(result.extensions)
369+
}
370+
317371
// MARK: Mutations
318372

319373
func testCreateReviewForEpisode() throws {

0 commit comments

Comments
 (0)