Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Apollo): Add constructor to ApolloAmplifyConnector for directly specifying connection information #2980

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 6 additions & 7 deletions apollo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ For applications using `apollo-appsync-amplify`, you can connect directly to you

```kotlin
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
// or
val connector = ApolloAmplifyConnector(
endpointUrl = "https://example1234567890123456789.appsync-api.us-east-1.amazonaws.com/graphql",
region = "us-east-1",
apiKey = "[YOUR_API_KEY]"
)

val apolloClient = ApolloClient.Builder()
.appSync(connector.endpoint, connector.apiKeyAuthorizer())
Expand Down Expand Up @@ -75,7 +81,6 @@ val authorizer = ApiKeyAuthorizer { fetchApiKey() }
```
```kotlin
// Using ApolloAmplifyConnector to read API key from amplify_outputs.json
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
val authorizer = connector.apiKeyAuthorizer()
```

Expand All @@ -95,10 +100,7 @@ val authorizer = AuthTokenAuthorizer { fetchUserToken() }
```
```kotlin
// Use ApolloAmplifyConnector to get Cognito tokens from Amplify for the signed-in user
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
val authorizer = connector.authTokenAuthorizer()
// or
val authorizer = AuthTokenAuthorizer { ApolloAmplifyConnector.fetchLatestCognitoAuthToken() }
```

### IamAuthorizer
Expand All @@ -114,10 +116,7 @@ val authorizer = IamAuthorizer { signRequestAndReturnHeaders(it) }
```
```kotlin
// Use ApolloAmplifyConnector to sign the request
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))
val authorizer = connector.iamAuthorizer()
// or supply a region to sign via the companion function
val authorizer = IamAuthorizer { ApolloAmplifyConnector.signAppSyncRequest(it, "us-east-1") }
```

## Contributing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
public final class com/amplifyframework/apollo/appsync/ApolloAmplifyConnector {
public static final field Companion Lcom/amplifyframework/apollo/appsync/ApolloAmplifyConnector$Companion;
public fun <init> (Landroid/content/Context;Lcom/amplifyframework/core/configuration/AmplifyOutputs;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun apiKeyAuthorizer ()Lcom/amplifyframework/apollo/appsync/authorizers/ApiKeyAuthorizer;
public final fun cognitoUserPoolAuthorizer ()Lcom/amplifyframework/apollo/appsync/authorizers/AuthTokenAuthorizer;
public static final fun fetchLatestCognitoAuthToken (Ljava/util/function/Consumer;Ljava/util/function/Consumer;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ import java.util.function.Consumer
* Connect Apollo to AppSync by delegating to Amplify for tokens and signing.
*/
class ApolloAmplifyConnector internal constructor(
data: AmplifyOutputsData.Data,
private val requestSigner: ApolloRequestSigner = ApolloRequestSigner(),
private val accessTokenProvider: AccessTokenProvider = AccessTokenProvider()
endpointUrl: String,
/**
* The AWS Region for your AWS AppSync GraphQL API
*/
val region: String,
/**
* The API Key configured for your AWS AppSync GraphQL API, if any
*/
val apiKey: String?,
private val requestSigner: ApolloRequestSigner,
private val accessTokenProvider: AccessTokenProvider
) {
/**
* Instantiate a connector with the configuration for an Amplify Gen2 App
Expand All @@ -48,19 +56,39 @@ class ApolloAmplifyConnector internal constructor(
) : this(AmplifyOutputsData.deserialize(context, outputs).data ?: error("No data section in AmplifyOutputs"))

/**
* The [AppSyncEndpoint] instance pointing to the endpoint specified in the Amplify Outputs
* Instantiate a connector directly with the data needed to access your AWS AppSync GraphQL API
* @param endpointUrl The endpoint url for your AWS AppSync GraphQL API
* @param region The AWS Region for your AWS AppSync GraphQL API
* @param apiKey The API Key configured for your AWS AppSync GraphQL API, if any
*/
val endpoint: AppSyncEndpoint = AppSyncEndpoint(data.url)
constructor(
endpointUrl: String,
region: String,
apiKey: String? = null
) : this(
endpointUrl = endpointUrl,
region = region,
apiKey = apiKey,
requestSigner = ApolloRequestSigner(),
accessTokenProvider = AccessTokenProvider()
)

/**
* The AWS Region specified in the Amplify Outputs
*/
val region: String = data.awsRegion
internal constructor(
data: AmplifyOutputsData.Data,
requestSigner: ApolloRequestSigner = ApolloRequestSigner(),
accessTokenProvider: AccessTokenProvider = AccessTokenProvider()
) : this(
endpointUrl = data.url,
region = data.awsRegion,
apiKey = data.apiKey,
requestSigner = requestSigner,
accessTokenProvider = accessTokenProvider
)

/**
* The API Key specified in the Amplify Outputs, if any
* The [AppSyncEndpoint] instance pointing to the endpoint specified in the Amplify Outputs
*/
val apiKey: String? = data.apiKey
val endpoint: AppSyncEndpoint = AppSyncEndpoint(endpointUrl)

/**
* Create an [AppSyncAuthorizer] instance that uses the API Key specified in your Amplify Outputs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,35 @@ import org.junit.Test

class ApolloAmplifyConnectorTest {
private val serverUrl = "https://example1234567890123456789.appsync-api.us-east-1.amazonaws.com/graphql"
private val region = "us-east-1"
private val key = "test-key"

private val outputs = mockk<AmplifyOutputsData.Data> {
every { url } returns serverUrl
every { awsRegion } returns "us-east-1"
every { apiKey } returns "test-key"
every { awsRegion } returns region
every { apiKey } returns key
}

@Test
fun `reads endpoint from AmplifyOutputs`() {
fun `reads data from AmplifyOutputs`() {
val connector = ApolloAmplifyConnector(outputs)

connector.endpoint.serverUrl.toString() shouldBe serverUrl
connector.region shouldBe region
connector.apiKey shouldBe key
}

@Test
fun `reads region from AmplifyOutputs`() {
val connector = ApolloAmplifyConnector(outputs)
connector.region shouldBe "us-east-1"
}
fun `returns data passed to constructor`() {
val connector = ApolloAmplifyConnector(
endpointUrl = serverUrl,
region = region,
apiKey = key
)

@Test
fun `reads api key from AmplifyOutputs`() {
val connector = ApolloAmplifyConnector(outputs)
connector.apiKey shouldBe "test-key"
connector.endpoint.serverUrl.toString() shouldBe serverUrl
connector.region shouldBe region
connector.apiKey shouldBe key
}

@Test
Expand Down