|
| 1 | +<img src="https://s3.amazonaws.com/aws-mobile-hub-images/aws-amplify-logo.png" alt="AWS Amplify" width="225"> |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# AWS AppSync Apollo Extensions for Kotlin |
| 6 | + |
| 7 | +These libraries allows you to connect [Apollo Kotlin](https://www.apollographql.com/docs/kotlin/) v4.x to [AWS AppSync](https://aws.amazon.com/pm/appsync/). |
| 8 | + |
| 9 | +There are two libraries available: |
| 10 | + |
| 11 | +- `apollo-appsync`: This library implements the authorization and protocol logic for Apollo to connect to AppSync. It does not depend on Amplify, and instead leaves it to the application developer to supply tokens or signatures when using Owner or IAM-based authorization. This is the recommended library if your application does not already use Amplify. |
| 12 | +- `apollo-appsync-amplify`: This library depends on both `apollo-appsync` and `Amplify Android`, and contains some glue classes to use Amplify to implement the authorizers for `apollo-appsync`. This is the recommended library if your application is already using Amplify. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Add the dependency you prefer to your `build.gradle.kts` file. |
| 17 | + |
| 18 | +```kotlin |
| 19 | +// To only use Apollo to speak to AppSync, without using Amplify |
| 20 | +implementation("com.amplifyframework:apollo-appsync:1.0.0") |
| 21 | + |
| 22 | +// To connect Apollo to AppSync using your existing Amplify Gen2 Backend |
| 23 | +implementation("com.amplifyframework:apollo-appsync-amplify:1.0.0") |
| 24 | +``` |
| 25 | + |
| 26 | +For applications using `apollo-appsync` directly, instantiate the Endpoint and the desired Authorizer instance, and then call the Apollo builder extension. |
| 27 | + |
| 28 | +```kotlin |
| 29 | +val endpoint = AppSyncEndpoint("https://example1234567890123456789.appsync-api.us-east-1.amazonaws.com/graphql") |
| 30 | +val authorizer = ApiKeyAuthorizer("[YOUR_API_KEY") |
| 31 | + |
| 32 | +val apolloClient = ApolloClient.Builder() |
| 33 | + .appSync(endpoint, authorizer) |
| 34 | + .build() |
| 35 | +``` |
| 36 | + |
| 37 | +For applications using `apollo-appsync-amplify`, you can connect directly to your Amplify Gen2 Backend using an `ApolloAmplifyConnector`. This class can create Authorizer instances that use Amplify to provide Cognito Tokens and sign requests as needed. |
| 38 | + |
| 39 | +```kotlin |
| 40 | +val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs)) |
| 41 | + |
| 42 | +val apolloClient = ApolloClient.Builder() |
| 43 | + .appSync(connector.endpoint, connect.apiKeyAuthorizer()) |
| 44 | + .build() |
| 45 | +``` |
| 46 | + |
| 47 | +Once you have constructed the Apollo client you can use it as normal for queries, mutations, and subscriptions to AppSync. |
| 48 | + |
| 49 | +## Contributing |
| 50 | + |
| 51 | +- [CONTRIBUTING.md](../CONTRIBUTING.md) |
| 52 | + |
| 53 | +## Security |
| 54 | + |
| 55 | +See [CONTRIBUTING](../CONTRIBUTING.md#security-issue-notifications) for more information. |
| 56 | + |
| 57 | +## License |
| 58 | + |
| 59 | +This project is licensed under the Apache-2.0 License. |
0 commit comments