Skip to content

Commit 0ccaeac

Browse files
committed
Improve Android Apollo Extensions documentation
1 parent 68c8632 commit 0ccaeac

File tree

1 file changed

+79
-13
lines changed
  • src/pages/[platform]/build-a-backend/data/aws-appsync-apollo-extensions

1 file changed

+79
-13
lines changed

src/pages/[platform]/build-a-backend/data/aws-appsync-apollo-extensions/index.mdx

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function getStaticProps(context) {
2323
};
2424
}
2525

26-
AWS AppSync Apollo Extensions provides a seamless way to connect to your AWS AppSync while using the Apollo client. Apollo client is an open-source GraphQL client.
26+
AWS AppSync Apollo Extensions provide a seamless way to connect to your AWS AppSync backend using Apollo client, an open-source GraphQL client.
2727

2828
<InlineFilter filters={["swift"]}>
2929

@@ -40,30 +40,35 @@ To learn more about Apollo, see https://www.apollographql.com/docs/kotlin.
4040

4141
## Features
4242

43-
AWS AppSync Apollo Extensions provide AWS AppSync authorizers to be used with the Apollo client to make it simple to configure runtime interceptors and apply the correct authorization payloads to your GraphQL operations.
43+
AWS AppSync Apollo Extensions provide AWS AppSync authorizers to be used with the Apollo client to make it simple to apply the correct authorization payloads to your GraphQL operations.
4444

4545
The Amplify library provides components to facilitate configuring the authorizers with Apollo client by providing configuration values to connect to your Amplify Data backend.
4646

47-
<InlineFilter filters={["android"]}>
47+
### Install the AWS AppSync Apollo Extensions library
4848

49-
### Install the AWS AppSync Apollo library
49+
<InlineFilter filters={["android"]}>
5050

51-
Add `apollo-appsync` dependency to your app/build.gradle.kts file:
51+
To connect Apollo to AppSync without using Amplify, add the `apollo-appsync` dependency to your app/build.gradle.kts file. If your
52+
application is using Amplify Android, add the `apollo-appsync-amplify` dependency instead.
5253

5354
```kotlin title="app/build.gradle.kts"
5455
dependencies {
5556
// highlight-start
57+
// Connect Apollo to AppSync without using Amplify
5658
implementation("com.amplifyframework:apollo-appsync:1.0.0")
5759
// highlight-end
60+
// or
61+
// highlight-start
62+
// Connect Apollo to AppSync, delegating some implementation details to Amplify
63+
implementation("com.amplifyframework:apollo-appsync-amplify:1.0.0")
64+
// highlight-end
5865
}
5966
```
6067

6168
</InlineFilter>
6269

6370
<InlineFilter filters={["swift"]}>
6471

65-
### Install the AWS AppSync Apollo library
66-
6772
Add AWS AppSync Apollo Extensions into your project using Swift Package Manager.
6873

6974
Enter its GitHub URL (`https://github.com/aws-amplify/aws-appsync-apollo-extensions-swift`), select **Up to Next Major Version** and click **Add Package**
@@ -75,7 +80,7 @@ Enter its GitHub URL (`https://github.com/aws-amplify/aws-appsync-apollo-extensi
7580

7681
### Connecting to AWS AppSync with Apollo client
7782

78-
AWS AppSync supports the following [authorization modes](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html)
83+
AWS AppSync supports the following [authorization modes](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html):
7984

8085
#### API_KEY
8186

@@ -92,9 +97,25 @@ let interceptor = AppSyncInterceptor(authorizer)
9297

9398
<InlineFilter filters={["android"]}>
9499

100+
An `ApiKeyAuthorizer` can be used with a hardcoded API key, by fetching the key from some source, or reading it from `amplify_outputs.json`:
101+
95102
```kotlin
103+
// highlight-start
104+
// Use a hard-coded API key
96105
val authorizer = ApiKeyAuthorizer("[API_KEY]")
97-
val interceptor = AppSyncInterceptor(authorizer)
106+
//highlight-end
107+
// or
108+
// highlight-start
109+
// Fetch the API key from some source. This function may be called many times,
110+
// so it should implement appropriate caching internally.
111+
val authorizer = ApiKeyAuthorizer { fetchApiKey() }
112+
//highlight-end
113+
// or
114+
// highlight-start
115+
// Using ApolloAmplifyConnector to read API key from amplify_outputs.json
116+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
117+
val authorizer = connector.apiKeyAuthorizer()
118+
//highlight-end
98119
```
99120

100121
</InlineFilter>
@@ -134,10 +155,30 @@ let interceptor = AppSyncInterceptor(authorizer)
134155

135156
<InlineFilter filters={["android"]}>
136157

137-
Create the AuthTokenAuthorizer with this method.
158+
You can use `AmplifyApolloConnector` to get an `AuthTokenAuthorizer` instance that supplies the token for the current logged-in Amplify user, or implement the token fetching yourself.
138159

139160
```kotlin
140-
val authorizer = AuthTokenAuthorizer { ApolloAmplifyConnector.fetchLatestCognitoAuthToken() }
161+
// highlight-start
162+
// Using ApolloAmplifyConnector to get the authorizer that connects to your
163+
// Amplify instance
164+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
165+
val authorizer = connector.authTokenAuthorizer()
166+
//highlight-end
167+
// or
168+
// highlight-start
169+
// Using the ApolloAmplifyConnector companion function
170+
val authorizer = AuthTokenAuthorizer {
171+
ApolloAmplifyConnector.fetchLatestCognitoAuthToken()
172+
}
173+
//highlight-end
174+
// or
175+
// highlight-start
176+
// Use your own token fetching. This function may be called many times,
177+
// so it should implement appropriate caching internally.
178+
val authorizer = AuthTokenAuthorizer {
179+
fetchLatestAuthToken()
180+
}
181+
//highlight-end
141182
```
142183

143184
</InlineFilter>
@@ -163,10 +204,23 @@ let authorizer = IAMAuthorizer(
163204

164205
<InlineFilter filters={["android"]}>
165206

166-
If you are using Amplify Auth, you can use the following method for AWS_IAM auth
207+
If you are using `apollo-appsync-amplify`, you can use the `ApolloAmplifyConnector` to delegate token fetching and request
208+
signing to Amplify.
167209

168210
```kotlin
169-
val authorizer = IamAuthorizer { ApolloAmplifyConnector.signAppSyncRequest(it, "us-east-1") }
211+
// highlight-start
212+
// Using ApolloAmplifyConnector to get the authorizer that connects to your
213+
// Amplify instance
214+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
215+
val authorizer = connector.iamAuthorizer()
216+
//highlight-end
217+
// or
218+
// highlight-start
219+
// Using the ApolloAmplifyConnector companion function
220+
val authorizer = IamAuthorizer {
221+
ApolloAmplifyConnector.signAppSyncRequest(it, "us-east-1")
222+
}
223+
//highlight-end
170224
```
171225

172226
</InlineFilter>
@@ -313,6 +367,8 @@ func createApolloClient() throws -> ApolloClient {
313367

314368
<InlineFilter filters={["android"]}>
315369

370+
When using `apollo-appsync`, you create `AppSyncEndpoint` and `AppSyncAuthorizer` instances, and pass them to the ApolloClient's Builder extension function.
371+
316372
```kotlin
317373
val endpoint = AppSyncEndpoint("<your_appsync_endpoint>")
318374
val authorizer = /* your Authorizer */
@@ -322,4 +378,14 @@ val apolloClient = ApolloClient.Builder()
322378
.build()
323379
```
324380

381+
When using `apollo-appsync-amplify`, you can get the endpoint and authorizer from an `ApolloAmplifyConnector` to connect to your Amplify backend.
382+
383+
```kotlin
384+
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
385+
386+
val apolloClient = ApolloClient.Builder()
387+
.appSync(connector.endpoint, connector.apiKeyAuthorizer()) // or .authTokenAuthorizer(), or .iamAuthorizer()
388+
.build()
389+
```
390+
325391
</InlineFilter>

0 commit comments

Comments
 (0)