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
Copy file name to clipboardExpand all lines: src/pages/[platform]/build-a-backend/data/aws-appsync-apollo-extensions/index.mdx
+79-13Lines changed: 79 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ export function getStaticProps(context) {
23
23
};
24
24
}
25
25
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.
27
27
28
28
<InlineFilterfilters={["swift"]}>
29
29
@@ -40,30 +40,35 @@ To learn more about Apollo, see https://www.apollographql.com/docs/kotlin.
40
40
41
41
## Features
42
42
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.
44
44
45
45
The Amplify library provides components to facilitate configuring the authorizers with Apollo client by providing configuration values to connect to your Amplify Data backend.
46
46
47
-
<InlineFilterfilters={["android"]}>
47
+
### Install the AWS AppSync Apollo Extensions library
48
48
49
-
### Install the AWS AppSync Apollo library
49
+
<InlineFilterfilters={["android"]}>
50
50
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.
52
53
53
54
```kotlin title="app/build.gradle.kts"
54
55
dependencies {
55
56
// highlight-start
57
+
// Connect Apollo to AppSync without using Amplify
Add AWS AppSync Apollo Extensions into your project using Swift Package Manager.
68
73
69
74
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
75
80
76
81
### Connecting to AWS AppSync with Apollo client
77
82
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):
79
84
80
85
#### API_KEY
81
86
@@ -92,9 +97,25 @@ let interceptor = AppSyncInterceptor(authorizer)
92
97
93
98
<InlineFilterfilters={["android"]}>
94
99
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
+
95
102
```kotlin
103
+
// highlight-start
104
+
// Use a hard-coded API key
96
105
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
98
119
```
99
120
100
121
</InlineFilter>
@@ -134,10 +155,30 @@ let interceptor = AppSyncInterceptor(authorizer)
134
155
135
156
<InlineFilterfilters={["android"]}>
136
157
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.
138
159
139
160
```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
When using `apollo-appsync`, you create `AppSyncEndpoint` and `AppSyncAuthorizer` instances, and pass them to the ApolloClient's Builder extension function.
371
+
316
372
```kotlin
317
373
val endpoint =AppSyncEndpoint("<your_appsync_endpoint>")
318
374
val authorizer =/* your Authorizer */
@@ -322,4 +378,14 @@ val apolloClient = ApolloClient.Builder()
322
378
.build()
323
379
```
324
380
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()
0 commit comments