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

Allow periods in arguments to be ignored when parsing cacheKeys #2057

Merged
merged 6 commits into from
Dec 15, 2021
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
13 changes: 12 additions & 1 deletion Sources/ApolloSQLite/SQLiteNormalizedCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class SQLiteNormalizedCache {
}

private func recordCacheKey(forFieldCacheKey fieldCacheKey: CacheKey) -> CacheKey {
let components = fieldCacheKey.components(separatedBy: ".")
let components = fieldCacheKey.split(usingRegex: "\\.(?![^()]*\\))") // Matches `.` so long as it's not contained within a parentheses group
var updatedComponents = [String]()
if components.first?.contains("_ROOT") == true {
for component in components {
Expand Down Expand Up @@ -117,3 +117,14 @@ extension SQLiteNormalizedCache: NormalizedCache {
try self.database.clearDatabase(shouldVacuumOnClear: self.shouldVacuumOnClear)
}
}

private extension String {
func split(usingRegex pattern: String) -> [String] {
guard let regex = try? NSRegularExpression(pattern: pattern) else { return [] }
Iron-Ham marked this conversation as resolved.
Show resolved Hide resolved
let matches = regex.matches(in: self, range: NSRange(0..<utf16.count))
let ranges = [startIndex..<startIndex]
+ matches.compactMap { Range($0.range, in: self) }
+ [endIndex..<endIndex]
return (0...matches.count).map { String(self[ranges[$0].upperBound..<ranges[$0+1].lowerBound]) }
}
}
61 changes: 61 additions & 0 deletions Tests/ApolloTests/Cache/SQLite/CachePersistenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,67 @@ class CachePersistenceTests: XCTestCase {
}
}

func testFetchAndPersistWithPeriodArguments() throws {
let query = SearchQuery(term: "Luke.Skywalker")
let sqliteFileURL = SQLiteTestCacheProvider.temporarySQLiteFileURL()

try SQLiteTestCacheProvider.withCache(fileURL: sqliteFileURL) { (cache) in
let store = ApolloStore(cache: cache)

let server = MockGraphQLServer()
let networkTransport = MockNetworkTransport(server: server, store: store)

let client = ApolloClient(networkTransport: networkTransport, store: store)

_ = server.expect(SearchQuery.self) { request in
[
"data": [
"search": [
[
"id": "1000",
"name": "Luke Skywalker",
"__typename": "Human"
]
]
]
]
}
let networkExpectation = self.expectation(description: "Fetching query from network")
let newCacheExpectation = self.expectation(description: "Fetch query from new cache")

client.fetch(query: query, cachePolicy: .fetchIgnoringCacheData) { outerResult in
defer { networkExpectation.fulfill() }

switch outerResult {
case .failure(let error):
XCTFail("Unexpected error: \(error)")
return
case .success(let graphQLResult):
XCTAssertEqual(graphQLResult.data?.search?.first??.asHuman?.name, "Luke Skywalker")
// Do another fetch from cache to ensure that data is cached before creating new cache
client.fetch(query: query, cachePolicy: .returnCacheDataDontFetch) { innerResult in
try! SQLiteTestCacheProvider.withCache(fileURL: sqliteFileURL) { cache in
let newStore = ApolloStore(cache: cache)
let newClient = ApolloClient(networkTransport: networkTransport, store: newStore)
newClient.fetch(query: query, cachePolicy: .returnCacheDataDontFetch) { newClientResult in
defer { newCacheExpectation.fulfill() }
switch newClientResult {
case .success(let newClientGraphQLResult):
XCTAssertEqual(newClientGraphQLResult.data?.search?.first??.asHuman?.name, "Luke Skywalker")
case .failure(let error):
XCTFail("Unexpected error with new client: \(error)")
}
_ = newClient // Workaround for a bug - ensure that newClient is retained until this block is run
}
}
}
}
}

self.waitForExpectations(timeout: 2, handler: nil)
}
}

func testPassInConnectionDoesNotThrow() {
do {
let database = try SQLiteDotSwiftDatabase(connection: Connection())
Expand Down