Skip to content

Commit dabfb13

Browse files
Merge pull request #991 from giantramen/master
Fix SQLite Cache Key Bug
2 parents 7e8ace5 + 5c8d9a2 commit dabfb13

6 files changed

Lines changed: 223 additions & 6 deletions

File tree

Sources/ApolloSQLite/SQLiteNormalizedCache.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,24 @@ public final class SQLiteNormalizedCache {
3333
}
3434

3535
private func recordCacheKey(forFieldCacheKey fieldCacheKey: CacheKey) -> CacheKey {
36-
var components = fieldCacheKey.components(separatedBy: ".")
37-
if components.count > 1 {
38-
components.removeLast()
36+
let components = fieldCacheKey.components(separatedBy: ".")
37+
var updatedComponents = [String]()
38+
if components.first?.contains("_ROOT") == true {
39+
for component in components {
40+
if updatedComponents.last?.last?.isNumber ?? false && component.first?.isNumber ?? false {
41+
updatedComponents[updatedComponents.count - 1].append(".\(component)")
42+
} else {
43+
updatedComponents.append(component)
44+
}
45+
}
46+
} else {
47+
updatedComponents = components
48+
}
49+
50+
if updatedComponents.count > 1 {
51+
updatedComponents.removeLast()
3952
}
40-
return components.joined(separator: ".")
53+
return updatedComponents.joined(separator: ".")
4154
}
4255

4356
private func createTableIfNeeded() throws {

Tests/ApolloCacheDependentTests/LoadQueryFromStoreTests.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,45 @@ class LoadQueryFromStoreTests: XCTestCase {
319319
}
320320
}
321321
}
322-
322+
323+
324+
func testLoadingQueryWithFloats() throws {
325+
let starshipLength = 1234.5
326+
let coordinates = [[38.857150, -94.798464]]
327+
328+
let initialRecords: RecordSet = [
329+
"QUERY_ROOT": ["starshipCoordinates(coordinates:\(coordinates))": Reference(key: "starshipCoordinates(coordinates:\(coordinates))")],
330+
"starshipCoordinates(coordinates:\(coordinates))": ["__typename": "Starship",
331+
"name": "Millennium Falcon",
332+
"length": starshipLength,
333+
"coordinates": coordinates]
334+
]
335+
336+
withCache(initialRecords: initialRecords) { (cache) in
337+
store = ApolloStore(cache: cache)
338+
339+
let query = StarshipCoordinatesQuery(coordinates: coordinates)
340+
341+
load(query: query) { result in
342+
switch result {
343+
case .success(let graphQLResult):
344+
XCTAssertNil(graphQLResult.errors)
345+
346+
guard let data = graphQLResult.data else {
347+
XCTFail("No data returned with result")
348+
return
349+
}
350+
351+
XCTAssertEqual(data.starshipCoordinates?.name, "Millennium Falcon")
352+
XCTAssertEqual(data.starshipCoordinates?.length, starshipLength)
353+
XCTAssertEqual(data.starshipCoordinates?.coordinates, coordinates)
354+
case .failure(let error):
355+
XCTFail("Unexpected error: \(error)")
356+
}
357+
}
358+
}
359+
}
360+
323361
// MARK: - Helpers
324362

325363
private func load<Query: GraphQLQuery>(query: Query, resultHandler: @escaping GraphQLResultHandler<Query.Data>) {

Tests/StarWarsAPI/API.swift

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5556,6 +5556,121 @@ public final class StarshipQuery: GraphQLQuery {
55565556
}
55575557
}
55585558

5559+
public final class StarshipCoordinatesQuery: GraphQLQuery {
5560+
/// The raw GraphQL definition of this operation.
5561+
public let operationDefinition =
5562+
"""
5563+
query StarshipCoordinates($coordinates: [[Float!]!]) {
5564+
starshipCoordinates(coordinates: $coordinates) {
5565+
__typename
5566+
name
5567+
coordinates
5568+
length
5569+
}
5570+
}
5571+
"""
5572+
5573+
public let operationName = "StarshipCoordinates"
5574+
5575+
public let operationIdentifier: String? = "8dd77d4bc7494c184606da092a665a7c2ca3c2a3f14d3b23fa5e469e207b3406"
5576+
5577+
public var coordinates: [[Double]]?
5578+
5579+
public init(coordinates: [[Double]]?) {
5580+
self.coordinates = coordinates
5581+
}
5582+
5583+
public var variables: GraphQLMap? {
5584+
return ["coordinates": coordinates]
5585+
}
5586+
5587+
public struct Data: GraphQLSelectionSet {
5588+
public static let possibleTypes = ["Query"]
5589+
5590+
public static let selections: [GraphQLSelection] = [
5591+
GraphQLField("starshipCoordinates", arguments: ["coordinates": GraphQLVariable("coordinates")], type: .object(StarshipCoordinate.selections)),
5592+
]
5593+
5594+
public private(set) var resultMap: ResultMap
5595+
5596+
public init(unsafeResultMap: ResultMap) {
5597+
self.resultMap = unsafeResultMap
5598+
}
5599+
5600+
public init(starshipCoordinates: StarshipCoordinate? = nil) {
5601+
self.init(unsafeResultMap: ["__typename": "Query", "starshipCoordinates": starshipCoordinates.flatMap { (value: StarshipCoordinate) -> ResultMap in value.resultMap }])
5602+
}
5603+
5604+
public var starshipCoordinates: StarshipCoordinate? {
5605+
get {
5606+
return (resultMap["starshipCoordinates"] as? ResultMap).flatMap { StarshipCoordinate(unsafeResultMap: $0) }
5607+
}
5608+
set {
5609+
resultMap.updateValue(newValue?.resultMap, forKey: "starshipCoordinates")
5610+
}
5611+
}
5612+
5613+
public struct StarshipCoordinate: GraphQLSelectionSet {
5614+
public static let possibleTypes = ["Starship"]
5615+
5616+
public static let selections: [GraphQLSelection] = [
5617+
GraphQLField("__typename", type: .nonNull(.scalar(String.self))),
5618+
GraphQLField("name", type: .nonNull(.scalar(String.self))),
5619+
GraphQLField("coordinates", type: .list(.nonNull(.list(.nonNull(.scalar(Double.self)))))),
5620+
GraphQLField("length", type: .scalar(Double.self)),
5621+
]
5622+
5623+
public private(set) var resultMap: ResultMap
5624+
5625+
public init(unsafeResultMap: ResultMap) {
5626+
self.resultMap = unsafeResultMap
5627+
}
5628+
5629+
public init(name: String, coordinates: [[Double]]? = nil, length: Double? = nil) {
5630+
self.init(unsafeResultMap: ["__typename": "Starship", "name": name, "coordinates": coordinates, "length": length])
5631+
}
5632+
5633+
public var __typename: String {
5634+
get {
5635+
return resultMap["__typename"]! as! String
5636+
}
5637+
set {
5638+
resultMap.updateValue(newValue, forKey: "__typename")
5639+
}
5640+
}
5641+
5642+
/// The name of the starship
5643+
public var name: String {
5644+
get {
5645+
return resultMap["name"]! as! String
5646+
}
5647+
set {
5648+
resultMap.updateValue(newValue, forKey: "name")
5649+
}
5650+
}
5651+
5652+
public var coordinates: [[Double]]? {
5653+
get {
5654+
return resultMap["coordinates"] as? [[Double]]
5655+
}
5656+
set {
5657+
resultMap.updateValue(newValue, forKey: "coordinates")
5658+
}
5659+
}
5660+
5661+
/// Length of the starship, along the longest axis
5662+
public var length: Double? {
5663+
get {
5664+
return resultMap["length"] as? Double
5665+
}
5666+
set {
5667+
resultMap.updateValue(newValue, forKey: "length")
5668+
}
5669+
}
5670+
}
5671+
}
5672+
}
5673+
55595674
public final class ReviewAddedSubscription: GraphQLSubscription {
55605675
/// The raw GraphQL definition of this operation.
55615676
public let operationDefinition: String =

Tests/StarWarsAPI/Starship.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ query Starship {
44
coordinates
55
}
66
}
7+
8+
query StarshipCoordinates($coordinates: [[Float!]!]) {
9+
starshipCoordinates(coordinates: $coordinates) {
10+
name
11+
coordinates
12+
length
13+
}
14+
}

Tests/StarWarsAPI/operationIdsPath.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
"name": "Starship",
128128
"source": "query Starship {\n starship(id: 3000) {\n __typename\n name\n coordinates\n }\n}"
129129
},
130+
"8dd77d4bc7494c184606da092a665a7c2ca3c2a3f14d3b23fa5e469e207b3406": {
131+
"name": "StarshipCoordinates",
132+
"source": "query StarshipCoordinates($coordinates: [[Float!]!]) {\n starshipCoordinates(coordinates: $coordinates) {\n __typename\n name\n coordinates\n length\n }\n}"
133+
},
130134
"38644c5e7cf4fd506b91d2e7010cabf84e63dfcd33cf1deb443b4b32b55e2cbe": {
131135
"name": "ReviewAdded",
132136
"source": "subscription ReviewAdded($episode: Episode) {\n reviewAdded(episode: $episode) {\n __typename\n episode\n stars\n commentary\n }\n}"

Tests/StarWarsAPI/schema.json

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,45 @@
204204
},
205205
"isDeprecated": false,
206206
"deprecationReason": null
207+
},
208+
{
209+
"name": "starshipCoordinates",
210+
"description": "",
211+
"args": [
212+
{
213+
"name": "coordinates",
214+
"description": "",
215+
"type": {
216+
"kind": "LIST",
217+
"name": null,
218+
"ofType": {
219+
"kind": "NON_NULL",
220+
"name": null,
221+
"ofType": {
222+
"kind": "LIST",
223+
"name": null,
224+
"ofType": {
225+
"kind": "NON_NULL",
226+
"name": null,
227+
"ofType": {
228+
"kind": "SCALAR",
229+
"name": "Float",
230+
"ofType": null
231+
}
232+
}
233+
}
234+
}
235+
},
236+
"defaultValue": null
237+
}
238+
],
239+
"type": {
240+
"kind": "OBJECT",
241+
"name": "Starship",
242+
"ofType": null
243+
},
244+
"isDeprecated": false,
245+
"deprecationReason": null
207246
}
208247
],
209248
"inputFields": null,
@@ -2184,4 +2223,4 @@
21842223
]
21852224
}
21862225
}
2187-
}
2226+
}

0 commit comments

Comments
 (0)