Skip to content

Commit

Permalink
Expose whether a result was served from cache or fetched from the ser…
Browse files Browse the repository at this point in the history
…ver (#176)

Closes #170.
  • Loading branch information
AnandWalvekar authored and martijnwalraven committed Oct 31, 2017
1 parent fe9d0ea commit d93b763
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/Apollo/ApolloStore.swift
Expand Up @@ -98,7 +98,7 @@ public final class ApolloStore {

return try transaction.execute(selections: Query.Data.selections, onObjectWithKey: Query.rootCacheKey, variables: query.variables, accumulator: zip(mapper, dependencyTracker))
}.map { (data: Query.Data, dependentKeys: Set<CacheKey>) in
GraphQLResult(data: data, errors: nil, dependentKeys: dependentKeys)
GraphQLResult(data: data, errors: nil, source:.cache, dependentKeys: dependentKeys)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Apollo/GraphQLResponse.swift
Expand Up @@ -31,10 +31,10 @@ public final class GraphQLResponse<Operation: GraphQLOperation> {
return firstly {
try executor.execute(selections: Operation.Data.selections, on: dataEntry, withKey: Operation.rootCacheKey, variables: operation.variables, accumulator: zip(mapper, normalizer, dependencyTracker))
}.map { (data, records, dependentKeys) in
(GraphQLResult(data: data, errors: errors, dependentKeys: dependentKeys), records)
(GraphQLResult(data: data, errors: errors, source: .server, dependentKeys: dependentKeys), records)
}
} else {
return Promise(fulfilled: (GraphQLResult(data: nil, errors: errors, dependentKeys: nil), nil))
return Promise(fulfilled: (GraphQLResult(data: nil, errors: errors, source: .server, dependentKeys: nil), nil))
}
}
}
15 changes: 12 additions & 3 deletions Sources/Apollo/GraphQLResult.swift
@@ -1,15 +1,24 @@
/// Represents the result of a GraphQL operation.
public struct GraphQLResult<Data> {
/// Represents source of data
public enum Source {
case cache
case server
}

/// The typed result data, or `nil` if an error was encountered that prevented a valid response.
public let data: Data?
/// A list of errors, or `nil` if the operation completed without encountering any errors.
public let errors: [GraphQLError]?

/// Source of data
public let source:Source

let dependentKeys: Set<CacheKey>?

init(data: Data?, errors: [GraphQLError]?, dependentKeys: Set<CacheKey>?) {
init(data: Data?, errors: [GraphQLError]?, source:Source, dependentKeys: Set<CacheKey>?) {
self.data = data
self.errors = errors
self.dependentKeys = dependentKeys
self.dependentKeys = dependentKeys
self.source = source
}
}

0 comments on commit d93b763

Please sign in to comment.