Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Apollo/ApolloStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public final class ApolloStore {
accumulator: zip(mapper, dependencyTracker))
}.map { (data: Query.Data, dependentKeys: Set<CacheKey>) in
GraphQLResult(data: data,
extensions: nil,
errors: nil,
source:.cache,
dependentKeys: dependentKeys)
Expand Down
12 changes: 9 additions & 3 deletions Sources/Apollo/GraphQLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
let mapper = GraphQLSelectionSetMapper<Data>()
let normalizer = GraphQLResultNormalizer()
let dependencyTracker = GraphQLDependencyTracker()
let extensions = body["extensions"] as? JSONObject

return firstly {
try executor.execute(selections: Data.selections,
Expand All @@ -40,15 +41,17 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
}.map { (data, records, dependentKeys) in
(
GraphQLResult(data: data,
errors: errors,
source: .server,
dependentKeys: dependentKeys),
extensions: extensions,
errors: errors,
source: .server,
dependentKeys: dependentKeys),
records
)
}
} else {
return Promise(fulfilled: (
GraphQLResult(data: nil,
extensions: nil,
errors: errors,
source: .server,
dependentKeys: nil),
Expand All @@ -72,13 +75,16 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
let data = try decode(selectionSet: Data.self,
from: dataEntry,
variables: variables)
let extensions = body["extensions"] as? JSONObject
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be outside the if let statement since theoretically someone could return stuff in extensions even if data is nil.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Yeah, I was wondering if I was making the right choice there or not. I'll make an update ASAP. Thank you!


return GraphQLResult(data: data,
extensions: extensions,
errors: errors,
source: .server,
dependentKeys: nil)
} else {
return GraphQLResult(data: nil,
extensions: nil,
errors: errors,
source: .server,
dependentKeys: nil)
Expand Down
4 changes: 4 additions & 0 deletions Sources/Apollo/GraphQLResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public struct GraphQLResult<Data> {
public let data: Data?
/// A list of errors, or `nil` if the operation completed without encountering any errors.
public let errors: [GraphQLError]?
/// A dictionary which services can use however they see fit to provide additional information to clients.
public let extensions: [String: Any]?

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

public init(data: Data?,
extensions: [String: Any]?,
errors: [GraphQLError]?,
source: Source,
dependentKeys: Set<CacheKey>?) {
self.data = data
self.extensions = extensions
self.errors = errors
self.source = source
self.dependentKeys = dependentKeys
Expand Down
54 changes: 54 additions & 0 deletions Tests/ApolloTests/ParseQueryResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,60 @@ class ParseQueryResponseTests: XCTestCase {
}
}

func testExtensionsEntryNotNullWhenProvidedInResponseAccompanyingDataEntry() throws {
let query = HumanQuery(id: "9999")

let response = GraphQLResponse(operation: query, body: [
"data": ["human": NSNull()],
"extensions": [:]
])

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

XCTAssertNotNil(result.extensions)
}

func testExtensionsValuesWhenPopulatedInResponse() throws {
let query = HumanQuery(id: "9999")

let response = GraphQLResponse(operation: query, body: [
"data": ["human": NSNull()],
"extensions": ["parentKey": ["childKey": "someValue"]]
])

let (result, _) = try response.parseResult().await()
let extensionsDictionary = result.extensions
let childDictionary = extensionsDictionary?["parentKey"] as? JSONObject

XCTAssertNotNil(extensionsDictionary)
XCTAssertNotNil(childDictionary)
XCTAssertEqual(childDictionary, ["childKey": "someValue"])
}

func testExtensionsEntryNullWhenNotProvidedInResponse() throws {
let query = HumanQuery(id: "9999")

let response = GraphQLResponse(operation: query, body: [
"data": ["human": NSNull()]
])

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

XCTAssertNil(result.extensions)
}

func testExtensionsEntryNullWhenDataEntryNotProvidedInResponse() throws {
let query = HumanQuery(id: "9999")

let response = GraphQLResponse(operation: query, body: [
"extensions": [:]
])

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

XCTAssertNil(result.extensions)
}

// MARK: Mutations

func testCreateReviewForEpisode() throws {
Expand Down