You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apollo iOS will generate a `HeroNameQuery` class that you can construct (with variables) and pass to `ApolloClient#fetch(query:)`:
28
28
29
29
```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
+
guardlet data =try? result.get().data else { return }
32
+
print(data.hero?.name) // Luke Skywalker
32
33
}
33
34
```
34
35
35
36
> 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.
36
37
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
+
iflet name = graphQLResult.data?.hero?.name {
45
+
print(name) // Luke Skywalker
46
+
} elseiflet 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
+
```
38
56
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).
Copy file name to clipboardExpand all lines: docs/source/fragments.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,8 +36,8 @@ This also works the other way around. The parent view controller only has to kno
36
36
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:
@@ -72,7 +72,8 @@ fragment DroidDetails on Droid {
72
72
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:
73
73
74
74
```swift
75
-
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { (result, error) in
75
+
apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { result in
76
+
guardlet data =try? result.get().data else { return }
Copy file name to clipboardExpand all lines: docs/source/mutations.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,8 +34,9 @@ The above mutation will upvote a post on the server. The result might be:
34
34
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:
35
35
36
36
```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
+
guardlet data =try? result.get().data else { return }
Copy file name to clipboardExpand all lines: docs/source/watching-queries.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,9 @@ As mentioned in the introduction, Apollo iOS does more than simply run your quer
9
9
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:
10
10
11
11
```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
+
guardlet data =try? result.get().data else { return }
14
+
print(data.hero?.name) // Luke Skywalker
14
15
}
15
16
```
16
17
@@ -58,10 +59,10 @@ store.withinReadWriteTransaction { transaction in
58
59
try transaction.update(query: query) { (data: inout HeroNameQuery.Data) in
0 commit comments