Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extensions property to GraphQLResult. #1370

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions Sources/Apollo/GraphQLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {
errors = nil
}

let extensions = body["extensions"] as? JSONObject

if let dataEntry = body["data"] as? JSONObject {
let executor = GraphQLExecutor { object, info in
return .result(.success(object[info.responseKeyForField]))
Expand All @@ -40,15 +42,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: extensions,
errors: errors,
source: .server,
dependentKeys: nil),
Expand All @@ -67,18 +71,21 @@ public final class GraphQLResponse<Data: GraphQLSelectionSet> {

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

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

return GraphQLResult(data: data,
extensions: extensions,
errors: errors,
source: .server,
dependentKeys: nil)
} else {
return GraphQLResult(data: nil,
extensions: extensions,
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 testExtensionsEntryNotNullWhenDataEntryNotProvidedInResponse() throws {
let query = HumanQuery(id: "9999")

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

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

XCTAssertNotNil(result.extensions)
}

// MARK: Mutations

func testCreateReviewForEpisode() throws {
Expand Down