Skip to content

Commit ca4b06a

Browse files
Add SQLite setup playground page
1 parent 5891a0d commit ca4b06a

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//: [Subscriptons](@previous)
2+
3+
import Apollo
4+
import Foundation
5+
import PlaygroundSupport
6+
7+
//: # Setting up a client with a SQLite cache
8+
9+
//: First, you'll need to set up a network transport, since you will also need that to set up the client:
10+
let serverURL = URL(string: "http://localhost:8080/graphql")!
11+
let networkTransport = HTTPNetworkTransport(url: serverURL)
12+
13+
//: You'll need to make sure you import the ApolloSQLite library IF you are not using CocoaPods (CocoaPods will automatically flatten everything down to a single Apollo import):
14+
import ApolloSQLite
15+
16+
//: Next, you'll have to figure out where to store your SQLite file. A reasonable place is the user's Documents directory in your sandbox.
17+
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
18+
let documentsURL = URL(fileURLWithPath: documentsPath)
19+
let sqliteFileURL = documentsURL.appendingPathComponent("test_apollo_db.sqlite")
20+
21+
print("File path: \(sqliteFileURL.path)")
22+
23+
//: Use that file URL to instantiate the SQLite cache:
24+
let sqliteCache = try SQLiteNormalizedCache(fileURL: sqliteFileURL)
25+
26+
//: And then instantiate an instance of `ApolloStore` with the cache you've just created:
27+
let store = ApolloStore(cache: sqliteCache)
28+
29+
//: Finally, pass that into your `ApolloClient` initializer, and you're now set up to use the SQLite cache for persistent storage:
30+
let apolloClient = ApolloClient(networkTransport: networkTransport, store: store)
31+
32+
33+
//: Now, let's test
34+
import StarWarsAPI
35+
let query = HeroDetailsQuery(episode: .newhope)
36+
apolloClient.fetch(query: query) { result in
37+
// This is the outer Result, which has either a `GraphQLResult` or an `Error`
38+
switch result {
39+
case .success(let graphQLResult):
40+
if let errors = graphQLResult.errors {
41+
// Errors here were returned by the GraphQL system.
42+
// Note that the presence of errors does NOT necessarily indicate a request failed: GraphQL requests can return partial results in the event of an error.
43+
print("GraphQL errors: \(errors)")
44+
}
45+
46+
if let hero = graphQLResult.data?.hero {
47+
// Here, we've used a type-safe accessor to the nested type to access the specific type with the specific fields requested by this query.
48+
print("Hero name: \(hero.name)")
49+
}
50+
case .failure(let error):
51+
// If this happens, something's gone wrong at the network level before the GraphQL server could respond.
52+
print("Network error: \(error)")
53+
}
54+
}
55+
56+
//: Give the database write a chance to finish before the playground finishes executing
57+
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
58+
print("FINISHED")
59+
PlaygroundPage.current.finishExecution()
60+
}
61+
62+
PlaygroundPage.current.needsIndefiniteExecution = true
63+
64+
//: Once "FINISHED" prints, you can open the database file at the path printed out with "File path" and examine it to see the persisted data.
65+
66+
//: [Next](@next)

Playgrounds/ApolloMacPlayground.playground/Pages/Subscriptions.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ for _ in 0..<loops {
126126
}
127127

128128
PlaygroundPage.current.needsIndefiniteExecution = true
129+
130+
//: [SQLiteCache](@next)

Playgrounds/ApolloMacPlayground.playground/contents.xcplayground

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='6.0' target-platform='macos' display-mode='rendered' buildActiveScheme='true'>
2+
<playground version='6.0' target-platform='macos' display-mode='raw' buildActiveScheme='true'>
33
<pages>
44
<page name='Introduction'/>
55
<page name='Queries'/>

0 commit comments

Comments
 (0)