Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
a8a35ae
Attempted fix for integration test failing on CI (#2024)
AnthonyMDev Nov 9, 2021
ece5b5b
fix(deps): update dependency gatsby-theme-apollo-docs to v5.3.6 (#2026)
renovate[bot] Nov 10, 2021
55c5db8
Update apollo-tooling to v2.33.9 (#2028)
calvincestari Nov 12, 2021
8d48031
Gateway clarification based on license change
hwillson Nov 12, 2021
46957a3
Update SQLite.swift to version 13.0 (#2015)
Iron-Ham Nov 17, 2021
d93568f
Release `0.50.0` (#2031)
calvincestari Nov 17, 2021
3262cd0
fix(deps): update dependency gatsby-theme-apollo-docs to v5.3.8 (#2038)
renovate[bot] Nov 18, 2021
f1707b4
Update caching.mdx (#2042)
bogdanbeczkowski Nov 24, 2021
c3687d2
Update ROADMAP.md (#2053)
calvincestari Dec 7, 2021
f192578
Clean up Github API for performance tests
AnthonyMDev Dec 13, 2021
c3969d1
fix(deps): update dependency gatsby-theme-apollo-docs to v5.3.9 (#2062)
renovate[bot] Dec 13, 2021
dbd7b87
Make filesystem diagram a bit easier to understand (#2063)
designatednerd Dec 14, 2021
67d86ae
Allow periods in arguments to be ignored when parsing cacheKeys (#2057)
Iron-Ham Dec 15, 2021
5079b14
fix(deps): update dependency gatsby-theme-apollo-docs to v5.3.11 (#2068)
renovate[bot] Dec 16, 2021
4d8b5bb
fix(deps): update dependency gatsby-theme-apollo-docs to v5.3.12 (#2074)
renovate[bot] Dec 17, 2021
95ee510
Add tutorial on subscriptions to match Android (#2072)
designatednerd Dec 20, 2021
0358975
Bump mermaid from 8.13.6 to 8.13.8 in /docs (#2083)
dependabot[bot] Jan 6, 2022
d46bd37
chore(deps): update dependency engine.io to 4.1.2 [security] (#2094)
renovate[bot] Jan 13, 2022
60ecd70
chore(deps): update dependency follow-redirects to 1.14.7 [security] …
renovate[bot] Jan 13, 2022
d44820d
chore(deps): update dependency node-forge to 1.0.0 [security] (#2095)
renovate[bot] Jan 13, 2022
fbeee46
Add note about acceptable extensions for types of schema file. (#2059)
designatednerd Jan 19, 2022
a4e7b4f
Docs: Bump to Gatsby v3 (#2104)
jgarrow Jan 25, 2022
0c9aa54
fix(deps): pin dependency gatsby-theme-apollo-docs to 6.0.0 (#2113)
renovate[bot] Jan 26, 2022
161873b
fix(deps): pin dependency gatsby to 3.14.6 (#2123)
renovate[bot] Jan 29, 2022
63eacd1
Edits to intro and installation docs (#2125)
Feb 1, 2022
eb6cbe1
Fix tutorial URL and tweak downloading a schema article (#2133)
Feb 3, 2022
7538bef
Split advanced "Creating a client" content into separate article (#2143)
Feb 7, 2022
ca88016
Disable flaky integration test
AnthonyMDev Feb 8, 2022
f51e7c4
Update gatsby config to deploy 1.0 alpha docs
AnthonyMDev Feb 8, 2022
ec034e8
Update roadmap after 1.0.0-alpha.1 release (#2145)
calvincestari Feb 9, 2022
0beea3d
[v1.0.0 Alpha 1] JavascriptError @unchecked Sendable (#2147)
TizianoCoroneo Feb 11, 2022
bf4cb5a
Update documentation to clarify schema download format and filename (…
calvincestari Feb 14, 2022
3c58987
chore(deps): update dependency follow-redirects to 1.14.8 [security] …
renovate[bot] Feb 14, 2022
f409e81
Update license copyright notice
calvincestari Feb 15, 2022
cf713c1
Delete Jazzy config - not supported
calvincestari Feb 16, 2022
c188637
Update list of active maintainers
calvincestari Feb 16, 2022
2b607ec
[Fix #2170] Do not refetch query watcher from server after cache miss…
AnthonyMDev Feb 25, 2022
bf86e01
Add `graphql-ws` protocol support (#2168)
calvincestari Feb 25, 2022
8b6a50a
chore(deps): update dependency prismjs to 1.27.0 [security] (#2179)
renovate[bot] Feb 28, 2022
cb5f1ce
Release 0.51.0 (#2086)
calvincestari Feb 28, 2022
664c1f1
Enable lazy access to the request body creation for leverage in custo…
rickfast Mar 3, 2022
52bbeac
Release `0.51.1` (#2188)
calvincestari Mar 7, 2022
6aee64a
Updates for new docs infra (#2197)
trevorblades Mar 15, 2022
f916c5f
Remove files from old Gatsby docs theme (#2202)
jgarrow Mar 15, 2022
62f4554
Clear body on APQ retry in JSONRequest (#2206)
AnthonyMDev Mar 18, 2022
729fedb
Release 0.51.2 (#2207)
AnthonyMDev Mar 18, 2022
08a41ba
Fix changelog version.
AnthonyMDev Mar 18, 2022
fecf44b
Add netlify config for deploy previews (#2208)
trevorblades Mar 22, 2022
b25d1de
Add codegen option for excludes (#2205)
bannzai Mar 23, 2022
a5f6f5d
Fix typo (#2212)
changm4n Mar 23, 2022
b4f58e2
Start a docs production deploy when docs changes get merged (#2211)
trevorblades Mar 23, 2022
4772297
Bump minimist from 1.2.5 to 1.2.6 in /SimpleUploadServer (#2214)
dependabot[bot] Mar 23, 2022
79876e5
Bump node-fetch from 2.6.6 to 2.6.7 in /SimpleUploadServer (#2215)
dependabot[bot] Mar 23, 2022
a563c40
Add note to tutorial about the `graphql-ws` subscription library (#2219)
rkoron007 Mar 25, 2022
c8c3951
Update tutorial-obtain-schema.md (#2225)
Carrione Mar 29, 2022
26351b5
Update tutorial-execute-query.md (#2228)
Carrione Mar 29, 2022
ab160e4
chore: Update Xcode build tool versions on CircleCI (#2242)
calvincestari Apr 8, 2022
57f329d
chore: Update roadmap
calvincestari Apr 15, 2022
b9a6116
fix: Sandbox documentation links (#2254)
martinbonnin Apr 21, 2022
09003c7
ci: Update xcode image to fix Node version mismatch
calvincestari Apr 21, 2022
fc260c7
ci: Update Node version to match what is installed with Xcode 13.3.1 …
calvincestari Apr 21, 2022
90d2c34
chore: updates roadmap (#2258)
jpvajda Apr 27, 2022
51c686f
Update build phases example to propagate errors to Xcode (#2260)
adam-zethraeus May 3, 2022
eecef09
Add a comment about a new use of the X-APOLLO-OPERATION-NAME header (…
glasser May 5, 2022
c78e0b1
Set permissions for GitHub actions (#2248)
neilnaveen May 16, 2022
858c769
updates roadmap for june 2022 (#2295)
jpvajda Jun 6, 2022
51c81bd
Update CircleCI tool versions (#2304)
calvincestari Jun 8, 2022
402dd16
fix: `graphql_transport_ws` protocol should send 'complete' to end su…
calvincestari Jun 17, 2022
c0a8ce3
Replace print statement with CodegenLogger.log (#2348)
hiltonc Jun 29, 2022
d26751a
Fix buttons on docs root (#2369)
Jul 12, 2022
f214c8e
Expose GraphQLResultError Path String (#2361)
Jul 12, 2022
414cc63
Revert "Fix buttons on docs root (#2369)" (#2371)
trevorblades Jul 12, 2022
0c43e49
release: 0.52.0 (#2370)
calvincestari Jul 14, 2022
bfa089b
Fix typo (#2379)
mischa-hildebrand Jul 18, 2022
b2ed705
Fix incorrect parameter name (#2380)
mischa-hildebrand Jul 18, 2022
135acc4
chore: Update ROADMAP for Beta release cycle (#2393)
calvincestari Jul 25, 2022
928f9ea
Remove all instances of bitcode as not supported in Xcode 14 (#2398)
stareque-atlassian Jul 28, 2022
42646f7
Release 0.53.0 (#2406)
calvincestari Jul 29, 2022
5f31e2a
fix: checkout at tag with legacy optionals behavior (#2412)
calvincestari Aug 2, 2022
323ed07
Update version in Swift Scripting docs (#2416)
redryerye Aug 3, 2022
ddc8953
Re-added support for iOS 11
adamayoung Aug 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tutorial on subscriptions to match Android (apollographql#2072)
* Remove bits from under construction that are going to be handled with blog posts, update versions

* Get rid of fragments and cache, bring over android subscriptions stuff

* this is actually part 9 on iOS

* bring over screenshots from Android tutorial

* Correct link

* Update everything for iOS

* update index and link from prior step to include subscriptions

* Fix link to subscriptions

* add missing image

* fix borked link

* be clearer about replacing the print statement

* remove "unnecessary" quotation marks

* add missing sentence, make image less ginormous

* Fix direction

I definitely know my left from my right! :woman-facepaming:

Co-authored-by: Calvin Cestari <[email protected]>

* add missing word

Co-authored-by: Calvin Cestari <[email protected]>
  • Loading branch information
designatednerd and calvincestari authored Dec 20, 2021
commit 95ee510487e5a0eaa739a74b323d7ca0e00c87d0
3 changes: 2 additions & 1 deletion docs/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module.exports = {
'tutorial/tutorial-pagination',
'tutorial/tutorial-detail-view',
'tutorial/tutorial-authentication',
'tutorial/tutorial-mutations'
'tutorial/tutorial-mutations',
'tutorial/tutorial-subscriptions'
],
Usage:[
'downloading-schema',
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 0 additions & 73 deletions docs/source/tutorial/tutorial-fragments-and-cache.md

This file was deleted.

10 changes: 4 additions & 6 deletions docs/source/tutorial/tutorial-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ title: "0. Introduction"

Welcome! This tutorial demonstrates adding the Apollo iOS SDK to an app to communicate with a GraphQL server. It is confirmed to work with the following tools:

- Xcode 13.0
- Swift 5.5
- Apollo iOS SDK 0.49.1
- Xcode 13.2
- Swift 5.6
- Apollo iOS SDK 0.50.0

The tutorial assumes that you're using a Mac with Xcode installed. It also assumes some prior experience with iOS development.

Expand All @@ -20,9 +20,7 @@ This tutorial takes you through building an app to "book a seat" on any of the r
* Code generation
* Queries and mutations
* Error handling
* Query watching [🚧 UNDER CONSTRUCTION 🚧]
* File uploads [🚧 UNDER CONSTRUCTION 🚧]
* Subscriptions [🚧 UNDER CONSTRUCTION 🚧]
* Subscriptions

A pre-built GraphQL server is provided for your app to execute operations against.

Expand Down
2 changes: 1 addition & 1 deletion docs/source/tutorial/tutorial-mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,4 @@ self.loadLaunchDetails(forceReload: true)

Run the application. When you book or cancel a trip, the application will fetch the updated state and update the UI with the correct state. When you go out and back in, the cache will be updated with the most recent state, and the most recent state will display.

This works well, but it could be more efficient. In the next section (🤞 coming soon 🤞), you'll learn more about how to get details in a reusable fashion and how to work with the cache directly.
In the next section, you'll learn how to use [subscriptions](/tutorial/tutorial-subscriptions/) with the Apollo client.
198 changes: 198 additions & 0 deletions docs/source/tutorial/tutorial-subscriptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
---
title: "9. Write your first subscription"
---

In this section, you will use subscriptions to get notified whenever someone books a flight 🚀! [Subscriptions](https://graphql.org/blog/subscriptions-in-graphql-and-relay/) allow you to be notified in real time whenever an event happens on your server. The [fullstack backend](https://apollo-fullstack-tutorial.herokuapp.com) supports subscriptions based on [WebSockets](https://en.wikipedia.org/wiki/WebSocket).


## Write your subscription

Open your [Sandbox](https://studio.apollographql.com/sandbox/explorer?endpoint=https%3A%2F%2Fapollo-fullstack-tutorial.herokuapp.com) back up, click on the Schema tab at the far left. In addition to `queries` and `mutations`, you will see a third type of operations, `subscriptions`. Click on subscriptions to see the `tripsBooked` subscription:

<img alt="The definition of tripsBooked in the schema" class="screenshot" src="images/schema_tripsBooked_definition.png"/>

This subscription doesn't take any argument and returns a single scalar named `tripsBooked`. Since you can book multiple trips at once, `tripsBooked` is an `Int`. It will contain the number of trips booked at once or -1 if a trip has been cancelled.

Click the play button to the far right of `tripsBooked` to open the subscription in Explorer. Open a new tab, then check the `tripsBooked` button to have the subscription added:

<img alt="The initial definition of the TripsBooked subscription" class="screenshot" src="images/explorer_tripsbooked_initial.png"/>

Again, rename your subscription so it's easier to find:

<img alt="The subscription after rename" class="screenshot" src="images/explorer_tripsbooked_renamed.png"/>

Click the Submit Operation button, and your subscription will start listening to events. You can tell it's up and running because a panel will pop up at the lower left where subscription data will come in:

<img alt="The UI showing that it's listening for subscription updates" class="screenshot" src="images/explorer_subscriptions_listening.png"/>

## Test your subscription

Open a new tab in Explorer. In this new tab, add code to book a trip like on [step 8](tutorial-mutations), but with a hard-coded ID:

```graphql
mutation BookTrip {
bookTrips(launchIds: ["93"]){
message
}
}
```

Do not forget to include the authentication header. At the bottom of the Sandbox Explorer pane where you add operations, there's a `Headers` section:

<img alt="Adding a login token to explorer" class="screenshot" src="images/explorer_authentication_header.png"/>

Click the Submit Operation button. If everything went well, you just booked a trip! At the top of the right panel, you'll see the success JSON for your your `BookTrip` mutation, and below it, updated JSON for the `TripsBooked` subscription:

<img alt="Subscription success in Explorer" class="screenshot" src="images/explorer_subscription_success.png"/>

Continue booking and/or canceling trips, you will see events coming in the subscription panel in real time. After some time, the server might close the connection and you'll have to restart your subscription to keep receiving events.

## Add the subscription to the project

Now that your subscription is working, add it to your project. Create an empty file named `TripsBooked.graphql` next to your other GraphQL files and paste the contents of the subscription. The process is similar to what you've already done for queries and mutations:

```graphql:title=TripsBooked.graphql
subscription TripsBooked {
tripsBooked
}
```

Build your project, and the subscription will be picked up and added to your `API.swift` file.

## Configure your ApolloClient to use subscriptions

In `Network.swift`, you'll need to set up a transport which supports subscriptions in addition to general network usage. In practice, this means adding a `WebSocketTransport` which will allow real-time communication with your server.

First, at the top of the file, add an import for the **ApolloWebSocket** framework to get access to the classes you'll need:

```swift:title=Network.swift
import ApolloWebSocket
```

Next, in the lazy declaration of the `apollo` variable, immediately after `transport` is declared, set up what you need to add subscription support to your client:

```swift:title=Network.swift
// 1
let webSocket = WebSocket(url: URL(string: "wss://apollo-fullstack-tutorial.herokuapp.com/graphql")!)

// 2
let webSocketTransport = WebSocketTransport(websocket: webSocket)

// 3
let splitTransport = SplitNetworkTransport(uploadingNetworkTransport: transport,
webSocketNetworkTransport: webSocketTransport)

// 4
return ApolloClient(networkTransport: splitTransport, store: store)
```

What's happening here?

1. You've created a web socket with the server's web socket URL - `wss://` is the protocol for a secure web socket.
2. You've created a `WebSocketTransport`, which allows the Apollo SDK to communicate with the web socket.
3. You've created a `SplitNetworkTransport`, which can decide whether to use a web socket or not automatically, with both the `RequestChainNetworkTransport` you had previously set up, and the `WebSocketTransport` you just set up.
4. You're now passing the `splitTransport` into the `ApolloClient`, so that it's the main transport being used in your `ApolloClient`.

Now, you're ready to actually use your subscription!

## Display a view when a trip is booked/cancelled

In `LaunchesViewController`, add a new variable just below `activeRequest` to hang on to a reference to your subscription so it doesn't get hammered by ARC as soon as it goes out of scope:

```swift:title=LaunchesViewController.swift
private var activeSubscription: Cancellable?
```

Next, just above the code for handling Segues, add code for starting and handling the result of a subscription:

```swift:title=LaunchesViewController.swift
// MARK: - Subscriptions

private func startSubscription() {
activeSubscription = Network.shared.apollo.subscribe(subscription: TripsBookedSubscription()) { result in
switch result {
case .failure(let error):
self.showAlert(title: "NetworkError",
message: error.localizedDescription)
case .success(let graphQLResult):
if let errors = graphQLResult.errors {
self.showAlertForErrors(errors)
} else if let tripsBooked = graphQLResult.data?.tripsBooked {
self.handleTripsBooked(value: tripsBooked)
} else {
// There was no data and there were no errors, do nothing.
}
}
}
}
}

private func handleSubscriptionEvent() {
print("Trips booked: \(value)")
}
```

Finally, add a line to `viewDidLoad` which actually starts the subscription:

```swift:title=LaunchesViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
self.startSubscription()
self.loadMoreLaunchesIfTheyExist()
}
```

Build and run your app and go back to Sandbox Explorer, and select the tab where you set up the `BookTrip` mutation. Book a new trip while your app is open, you'll see a log print out:

```
Trips booked: 1
```

Cancel that same trip, and you'll see another log:

```
Trips booked: -1
```

Now, let's display that information in a view! Replace the `print` statement in `handleTripsBooked` with code to use the included `NotificationView` to show a brief alert at the bottom of the screen with information about a trip being booked or cancelled:

```swift:title=LaunchesViewController.swift
private func handleTripsBooked(value: Int) {
var message: String
switch value {
case 1:
message = "A new trip was booked! 🚀"
case -1:
message = "A trip was cancelled! 😭"
default:
self.showAlert(title: "Unexpected value",
message: " Subscription returned unexpected value: \(value)")
return
}

NotificationView.show(in: self.navigationController!.view,
with: message,
for: 4.0)
}
```

Build and run the application to your simulator, then use Studio to send bookings and cancellations again, and your iOS app should see some shiny new notifications pop up:

<img alt="A new trip was booked (rocket)" class="screenshot" src="images/screenshot_trip_booked.png" width="300"/>

And you've done it! You've completed the tutorial.

## More resources

There are way more things you can do with the Apollo iOS SDK, and the rest of this documentation includes info on more advanced topics like:

- Using [fragments](/fragments/)
- Working with [custom scalars](/fetching-queries/#notes-on-working-with-custom-scalars)
- [Caching](/caching/)

Feel free to ask questions by either [opening an issue on our GitHub repo](https://github.com/apollographql/apollo-ios/issues), or [joining the community](http://community.apollographql.com/new-topic?category=Help&tags=mobile,client).

And if you want dig more and see GraphQL in real-world apps, you can take a look at these open source projects using Apollo iOS:

* https://github.com/GitHawkApp/GitHawk
* [open a PR if you have an example app that should be here!]