Skip to content

Commit 3a2159a

Browse files
Merge pull request #669 from giladronat/docs/result-update
Update Usage docs to use `Result`
2 parents 4c5afcf + 683b5c8 commit 3a2159a

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

docs/source/fetching-queries.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,34 @@ query HeroName($episode: Episode) {
2727
Apollo iOS will generate a `HeroNameQuery` class that you can construct (with variables) and pass to `ApolloClient#fetch(query:)`:
2828

2929
```swift
30-
apollo.fetch(query: HeroNameQuery(episode: .empire)) { (result, error) in
31-
print(result?.data?.hero?.name) // Luke Skywalker
30+
apollo.fetch(query: HeroNameQuery(episode: .empire)) { result in
31+
guard let data = try? result.get().data else { return }
32+
print(data.hero?.name) // Luke Skywalker
3233
}
3334
```
3435

3536
> By default, Apollo will deliver query results on the main thread, which is probably what you want if you're using them to update the UI. `fetch(query:)` takes an optional `queue:` parameter however, if you want your result handler to be called on a background queue.
3637
37-
The `error` parameter to the completion handler signals network or response format errors (such as invalid JSON).
38+
To handle potential errors, check the `failure(Failure)` result case, which details network or response format errors (such as invalid JSON):
39+
40+
```swift
41+
apollo.fetch(query: HeroNameQuery(episode: .empire)) { result in
42+
switch result {
43+
case .success(let graphQLResult):
44+
if let name = graphQLResult.data?.hero?.name {
45+
print(name) // Luke Skywalker
46+
} else if let errors = graphQLResult.errors {
47+
// GraphQL errors
48+
print(errors)
49+
}
50+
case .failure(let error):
51+
// Network or response format errors
52+
print(error)
53+
}
54+
}
55+
```
3856

39-
In addition to an optional `data` property, `result` contains an optional `errors` array with GraphQL errors (for more on this, see the sections on [error handling](https://facebook.github.io/graphql/#sec-Error-handling) and the [response format](https://facebook.github.io/graphql/#sec-Response-Format) in the GraphQL specification).
57+
In addition to an optional `data` property, `success(Success)` result case contains an optional `errors` array with GraphQL errors (for more on this, see the sections on [response format errors](https://graphql.github.io/graphql-spec/June2018/#sec-Errors) in the GraphQL specification).
4058

4159
## Typed query results
4260

@@ -87,8 +105,8 @@ query HeroAndFriendsNames($episode: Episode) {
87105
You can fetch results and access data using the following code:
88106

89107
```swift
90-
apollo.fetch(query: HeroAndFriendsNamesQuery(episode: .empire)) { (result, error) in
91-
guard let data = result?.data else { return }
108+
apollo.fetch(query: HeroAndFriendsNamesQuery(episode: .empire)) { result in
109+
guard let data = try? result.get().data else { return }
92110
print(data.hero?.name) // Luke Skywalker
93111
print(data.hero?.friends?.flatMap { $0?.name }.joined(separator: ", "))
94112
// Prints: Han Solo, Leia Organa, C-3PO, R2-D2
@@ -126,8 +144,8 @@ The classes generated by Apollo iOS can be converted to JSON using their `jsonOb
126144

127145
For example:
128146
```swift
129-
apollo.fetch(query: HeroAndFriendsNamesQuery(episode: .empire)) { (result, error) in
130-
guard let data = result?.data else { return }
147+
apollo.fetch(query: HeroAndFriendsNamesQuery(episode: .empire)) { result in
148+
guard let data = try? result.get().data else { return }
131149

132150
// Serialize the response as JSON
133151
let json = data.jsonObject

docs/source/fragments.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ This also works the other way around. The parent view controller only has to kno
3636
In fact, this is the main reason fields included through fragments are not exposed directly, but require you to access the data through the fragment explicitly:
3737

3838
```swift
39-
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { (result, error) in
40-
guard let data = result?.data else { return }
39+
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { result in
40+
guard let data = try? result.get().data else { return }
4141
print(data.hero?.name) // Luke Skywalker
4242
print(data.hero?.appearsIn) // WON'T WORK
4343
print(data.hero?.fragments.heroDetails.appearsIn) // [.newhope, .empire, .jedi]
@@ -72,7 +72,8 @@ fragment DroidDetails on Droid {
7272
You can access named fragments with type conditions the same way you access other fragments, but their type will be optional to reflect the fact that their fields will only be available if the object type matches:
7373

7474
```swift
75-
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { (result, error) in
75+
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { result in
76+
guard let data = try? result.get().data else { return }
7677
data.hero?.fragments.droidDetails?.primaryFunction
7778
}
7879
```
@@ -93,8 +94,8 @@ query HeroAndFriends($episode: Episode) {
9394
And results from inline fragments with type conditions will be made available through specially generated `as<Type>` properties:
9495

9596
```swift
96-
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { (result, error) in
97-
guard let data = result?.data else { return }
97+
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { result in
98+
guard let data = try? result.get().data else { return }
9899
data.hero?.asDroid?.primaryFunction
99100
}
100101
```
@@ -121,8 +122,8 @@ fragment HeroDetails on Character {
121122
```
122123

123124
```swift
124-
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { (result, error) in
125-
guard let data = result?.data else { return }
125+
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { result in
126+
guard let data = try? result.get().data else { return }
126127
data.hero?.fragments.heroDetails.asDroid?.primaryFunction
127128
}
128129
```

docs/source/mutations.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ The above mutation will upvote a post on the server. The result might be:
3434
Similar to queries, mutations are represented by instances of generated classes, conforming to the `GraphQLMutation` protocol. Constructor arguments are used to define mutation variables. You pass a mutation object to `ApolloClient#perform(mutation:)` to send the mutation to the server, execute it, and receive typed results:
3535

3636
```swift
37-
apollo.perform(mutation: UpvotePostMutation(postId: postId)) { (result, error) in
38-
print(result?.data?.upvotePost?.votes)
37+
apollo.perform(mutation: UpvotePostMutation(postId: postId)) { result in
38+
guard let data = try? result.get().data else { return }
39+
print(data.upvotePost?.votes)
3940
}
4041
```
4142

@@ -52,8 +53,9 @@ mutation UpvotePost($postId: Int!) {
5253
```
5354

5455
```swift
55-
apollo.perform(mutation: UpvotePostMutation(postId: postId)) { (result, error) in
56-
self.configure(with: result?.data?.upvotePost?.fragments.postDetails)
56+
apollo.perform(mutation: UpvotePostMutation(postId: postId)) { result in
57+
guard let data = try? result.get().data else { return }
58+
self.configure(with: data.upvotePost?.fragments.postDetails)
5759
}
5860
```
5961

docs/source/watching-queries.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ As mentioned in the introduction, Apollo iOS does more than simply run your quer
99
Watching a query is very similar to fetching a query. The main difference is that you don't just receive an initial result, but your result handler will be invoked whenever relevant data in the cache changes:
1010

1111
```swift
12-
let watcher = apollo.watch(query: HeroNameQuery(episode: .empire)) { (result, error) in
13-
print(data?.hero?.name) // Luke Skywalker
12+
let watcher = apollo.watch(query: HeroNameQuery(episode: .empire)) { result in
13+
guard let data = try? result.get().data else { return }
14+
print(data.hero?.name) // Luke Skywalker
1415
}
1516
```
1617

@@ -58,10 +59,10 @@ store.withinReadWriteTransaction { transaction in
5859
try transaction.update(query: query) { (data: inout HeroNameQuery.Data) in
5960
data.hero?.name = "Artoo"
6061

61-
let result = store.load(query: query).result!.valueOrError()
62+
let graphQLResult = try? store.load(query: query).result?.get()
6263

6364
// Prints "Artoo"
64-
print(result.data?.hero?.name)
65+
print(graphQLResult?.data?.hero?.name)
6566
}
66-
})
67+
}
6768
```

0 commit comments

Comments
 (0)