Skip to content

Commit 5c8d9a2

Browse files
Kamin, GrantKamin, Grant
authored andcommitted
Update With More Complicated Test Case
Updated loadingQueryWithFloats test with more complicated case that matches the original issue I was seeing.
1 parent d896fb3 commit 5c8d9a2

6 files changed

Lines changed: 65 additions & 45 deletions

File tree

Sources/ApolloSQLite/SQLiteNormalizedCache.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +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, let lastComponent = components.last,
38-
lastComponent.first?.isNumber ?? false && components[components.count - 2].last?.isNumber ?? false {
39-
components[components.count - 2].append(".\(lastComponent)")
40-
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
4148
}
42-
if components.count > 1 {
43-
components.removeLast()
49+
50+
if updatedComponents.count > 1 {
51+
updatedComponents.removeLast()
4452
}
45-
return components.joined(separator: ".")
53+
return updatedComponents.joined(separator: ".")
4654
}
4755

4856
private func createTableIfNeeded() throws {

Tests/ApolloCacheDependentTests/LoadQueryFromStoreTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,20 @@ class LoadQueryFromStoreTests: XCTestCase {
323323

324324
func testLoadingQueryWithFloats() throws {
325325
let starshipLength = 1234.5
326+
let coordinates = [[38.857150, -94.798464]]
326327

327328
let initialRecords: RecordSet = [
328-
"QUERY_ROOT": ["starshipLength(length:1234.5)": Reference(key: "starshipLength(length:1234.5)")],
329-
"starshipLength(length:1234.5)": ["__typename": "Starship",
330-
"name": "Millennium Falcon",
331-
"length": starshipLength,
332-
"coordinates": [[0.0,1.0], [2.0,3.0]]]
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]
333334
]
334335

335-
336336
withCache(initialRecords: initialRecords) { (cache) in
337337
store = ApolloStore(cache: cache)
338338

339-
let query = StarshipLengthQuery(length: starshipLength)
339+
let query = StarshipCoordinatesQuery(coordinates: coordinates)
340340

341341
load(query: query) { result in
342342
switch result {
@@ -348,9 +348,9 @@ class LoadQueryFromStoreTests: XCTestCase {
348348
return
349349
}
350350

351-
XCTAssertEqual(data.starshipLength?.name, "Millennium Falcon")
352-
XCTAssertEqual(data.starshipLength?.length, starshipLength)
353-
XCTAssertEqual(data.starshipLength?.coordinates, [[0.0,1.0], [2.0,3.0]])
351+
XCTAssertEqual(data.starshipCoordinates?.name, "Millennium Falcon")
352+
XCTAssertEqual(data.starshipCoordinates?.length, starshipLength)
353+
XCTAssertEqual(data.starshipCoordinates?.coordinates, coordinates)
354354
case .failure(let error):
355355
XCTFail("Unexpected error: \(error)")
356356
}

Tests/StarWarsAPI/API.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,12 +5548,12 @@ public final class StarshipQuery: GraphQLQuery {
55485548
}
55495549
}
55505550

5551-
public final class StarshipLengthQuery: GraphQLQuery {
5551+
public final class StarshipCoordinatesQuery: GraphQLQuery {
55525552
/// The raw GraphQL definition of this operation.
55535553
public let operationDefinition =
55545554
"""
5555-
query StarshipLength($length: Float!) {
5556-
starshipLength(length: $length) {
5555+
query StarshipCoordinates($coordinates: [[Float!]!]) {
5556+
starshipCoordinates(coordinates: $coordinates) {
55575557
__typename
55585558
name
55595559
coordinates
@@ -5562,25 +5562,25 @@ public final class StarshipLengthQuery: GraphQLQuery {
55625562
}
55635563
"""
55645564

5565-
public let operationName = "StarshipLength"
5565+
public let operationName = "StarshipCoordinates"
55665566

5567-
public let operationIdentifier: String? = "f890b11aaa5d41e6d53ced4549beb27b9157c8c301d47e076909262ed9edbfb1"
5567+
public let operationIdentifier: String? = "8dd77d4bc7494c184606da092a665a7c2ca3c2a3f14d3b23fa5e469e207b3406"
55685568

5569-
public var length: Double
5569+
public var coordinates: [[Double]]?
55705570

5571-
public init(length: Double) {
5572-
self.length = length
5571+
public init(coordinates: [[Double]]?) {
5572+
self.coordinates = coordinates
55735573
}
55745574

55755575
public var variables: GraphQLMap? {
5576-
return ["length": length]
5576+
return ["coordinates": coordinates]
55775577
}
55785578

55795579
public struct Data: GraphQLSelectionSet {
55805580
public static let possibleTypes = ["Query"]
55815581

55825582
public static let selections: [GraphQLSelection] = [
5583-
GraphQLField("starshipLength", arguments: ["length": GraphQLVariable("length")], type: .object(StarshipLength.selections)),
5583+
GraphQLField("starshipCoordinates", arguments: ["coordinates": GraphQLVariable("coordinates")], type: .object(StarshipCoordinate.selections)),
55845584
]
55855585

55865586
public private(set) var resultMap: ResultMap
@@ -5589,20 +5589,20 @@ public final class StarshipLengthQuery: GraphQLQuery {
55895589
self.resultMap = unsafeResultMap
55905590
}
55915591

5592-
public init(starshipLength: StarshipLength? = nil) {
5593-
self.init(unsafeResultMap: ["__typename": "Query", "starshipLength": starshipLength.flatMap { (value: StarshipLength) -> ResultMap in value.resultMap }])
5592+
public init(starshipCoordinates: StarshipCoordinate? = nil) {
5593+
self.init(unsafeResultMap: ["__typename": "Query", "starshipCoordinates": starshipCoordinates.flatMap { (value: StarshipCoordinate) -> ResultMap in value.resultMap }])
55945594
}
55955595

5596-
public var starshipLength: StarshipLength? {
5596+
public var starshipCoordinates: StarshipCoordinate? {
55975597
get {
5598-
return (resultMap["starshipLength"] as? ResultMap).flatMap { StarshipLength(unsafeResultMap: $0) }
5598+
return (resultMap["starshipCoordinates"] as? ResultMap).flatMap { StarshipCoordinate(unsafeResultMap: $0) }
55995599
}
56005600
set {
5601-
resultMap.updateValue(newValue?.resultMap, forKey: "starshipLength")
5601+
resultMap.updateValue(newValue?.resultMap, forKey: "starshipCoordinates")
56025602
}
56035603
}
56045604

5605-
public struct StarshipLength: GraphQLSelectionSet {
5605+
public struct StarshipCoordinate: GraphQLSelectionSet {
56065606
public static let possibleTypes = ["Starship"]
56075607

56085608
public static let selections: [GraphQLSelection] = [

Tests/StarWarsAPI/Starship.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ query Starship {
55
}
66
}
77

8-
query StarshipLength($length: Float!) {
9-
starshipLength(length: $length) {
8+
query StarshipCoordinates($coordinates: [[Float!]!]) {
9+
starshipCoordinates(coordinates: $coordinates) {
1010
name
1111
coordinates
1212
length

Tests/StarWarsAPI/operationIdsPath.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@
127127
"name": "Starship",
128128
"source": "query Starship {\n starship(id: 3000) {\n __typename\n name\n coordinates\n }\n}"
129129
},
130-
"f890b11aaa5d41e6d53ced4549beb27b9157c8c301d47e076909262ed9edbfb1": {
131-
"name": "StarshipLength",
132-
"source": "query StarshipLength($length: Float!) {\n starshipLength(length: $length) {\n __typename\n name\n coordinates\n length\n }\n}"
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}"
133133
},
134134
"38644c5e7cf4fd506b91d2e7010cabf84e63dfcd33cf1deb443b4b32b55e2cbe": {
135135
"name": "ReviewAdded",

Tests/StarWarsAPI/schema.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,31 @@
206206
"deprecationReason": null
207207
},
208208
{
209-
"name": "starshipLength",
209+
"name": "starshipCoordinates",
210210
"description": "",
211211
"args": [
212212
{
213-
"name": "length",
213+
"name": "coordinates",
214214
"description": "",
215215
"type": {
216-
"kind": "NON_NULL",
216+
"kind": "LIST",
217217
"name": null,
218218
"ofType": {
219-
"kind": "SCALAR",
220-
"name": "Float",
221-
"ofType": null
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+
}
222234
}
223235
},
224236
"defaultValue": null

0 commit comments

Comments
 (0)