Search in sources :

Example 26 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class SubscriptionInstrumentationTest method testAddSubscriptionWithIAMAuthModelForNullPatching.

@Test
public void testAddSubscriptionWithIAMAuthModelForNullPatching() {
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration();
    final String title = "22 Acacia Avenue";
    final String author = "Maiden @ " + System.currentTimeMillis();
    final String url = "1998 Remastered ";
    final String content = "If you are feeling down, depressed and lonely @" + System.currentTimeMillis();
    AppSyncSubscriptionCall<OnCreatePostSubscription.Data> onCreatePostSubscriptionCall = awsAppSyncClient.subscribe(OnCreatePostSubscription.builder().build());
    LatchedSubscriptionCallback<OnCreatePostSubscription.Data> onCreatePostCallback = LatchedSubscriptionCallback.instance();
    onCreatePostSubscriptionCall.execute(onCreatePostCallback);
    Log.d(TAG, "Subscribed and setup callback handler.");
    // Sleep for a while to make sure the subscription goes through
    Sleep.milliseconds(REASONABLE_WAIT_TIME_MS);
    // Try to create post using only the fields that are required for success.
    Posts.addRequiredFieldsOnly(awsAppSyncClient, title, author, url, content);
    Log.d(TAG, "Added Post using addPostRequireFieldsOnlyMutation ");
    onCreatePostCallback.awaitNextSuccessfulResponse();
    // Try to create a post, by supplying an incomplete set of parameters.
    // Expect errors in the GraphQL response.
    Posts.addMissingRequiredFields(awsAppSyncClient, title, author + System.currentTimeMillis(), url, content);
    Log.d(TAG, "Added Post using addPostMissingRequiredFieldsMutation");
    assertTrue(onCreatePostCallback.awaitNextResponse().hasErrors());
    // Cancel the subscription and expect a completion callback.
    onCreatePostSubscriptionCall.cancel();
    onCreatePostCallback.awaitCompletion();
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Test(org.junit.Test)

Example 27 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class Posts method list.

public static Map<String, Response<AllPostsQuery.Data>> list(AWSAppSyncClient client, ResponseFetcher responseFetcher) {
    int expectedResponseCount = responseFetcher.equals(AppSyncResponseFetchers.CACHE_AND_NETWORK) ? 2 : 1;
    CountDownLatch queryCountDownLatch = new CountDownLatch(expectedResponseCount);
    Log.d(TAG, "Calling Query listPosts with responseFetcher: " + responseFetcher.toString());
    Map<String, Response<AllPostsQuery.Data>> listPostsQueryResponses = new HashMap<>();
    client.query(AllPostsQuery.builder().build()).responseFetcher(responseFetcher).enqueue(DelegatingGraphQLCallback.to(response -> {
        if (response.fromCache()) {
            listPostsQueryResponses.put("CACHE", response);
        } else {
            listPostsQueryResponses.put("NETWORK", response);
        }
        queryCountDownLatch.countDown();
    }, failure -> {
        failure.printStackTrace();
        queryCountDownLatch.countDown();
    }));
    Await.latch(queryCountDownLatch);
    if (responseFetcher.equals(AppSyncResponseFetchers.CACHE_AND_NETWORK)) {
        assertNotNull(listPostsQueryResponses.get("CACHE"));
        assertNotNull(listPostsQueryResponses.get("NETWORK"));
        assertEquals(2, listPostsQueryResponses.size());
    } else {
        assertEquals(1, listPostsQueryResponses.size());
    }
    return listPostsQueryResponses;
}
Also used : Response(com.apollographql.apollo.api.Response) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) AddPostRequiredFieldsOnlyMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostRequiredFieldsOnlyMutation) AppSyncResponseFetchers(com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers) HashMap(java.util.HashMap) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) UpdatePostMutation(com.amazonaws.mobileconnectors.appsync.demo.UpdatePostMutation) AppSyncMutationCall(com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall) DelegatingGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback) Map(java.util.Map) GetPostQuery(com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery) DeletePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.DeletePostInput) CacheAndNetworkFetcher(com.apollographql.apollo.internal.fetcher.CacheAndNetworkFetcher) Log(android.util.Log) AddPostMissingRequiredFieldsMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMissingRequiredFieldsMutation) Sleep(com.amazonaws.mobileconnectors.appsync.util.Sleep) Assert.assertNotNull(org.junit.Assert.assertNotNull) CreatePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput) Await(com.amazonaws.mobileconnectors.appsync.util.Await) LatchedGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) UpdatePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.UpdatePostInput) Response(com.apollographql.apollo.api.Response) ResponseFetcher(com.apollographql.apollo.fetcher.ResponseFetcher) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Assert.assertEquals(org.junit.Assert.assertEquals) DeletePostMutation(com.amazonaws.mobileconnectors.appsync.demo.DeletePostMutation) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 28 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class AWSAppSyncClients method withUserPools2FromAWSConfiguration.

@NonNull
public static AWSAppSyncClient withUserPools2FromAWSConfiguration(String idTokenStringForCustomCognitoUserPool, ResponseFetcher responseFetcher) {
    // Amazon Cognito User Pools - Custom CognitoUserPool
    String clientDatabasePrefix = null;
    String clientName = null;
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    awsConfiguration.setConfiguration("MultiAuthAndroidIntegTestApp_AMAZON_COGNITO_USER_POOLS_2");
    try {
        clientDatabasePrefix = awsConfiguration.optJsonObject("AppSync").getString("ClientDatabasePrefix");
        clientName = awsConfiguration.optJsonObject("AppSync").getString("AuthMode");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    AWSAppSyncClient.Builder awsAppSyncClientBuilder4 = AWSAppSyncClient.builder().context(getTargetContext()).cognitoUserPoolsAuthProvider(() -> idTokenStringForCustomCognitoUserPool).awsConfiguration(awsConfiguration).useClientDatabasePrefix(true).defaultResponseFetcher(responseFetcher);
    AWSAppSyncClient awsAppSyncClient4 = awsAppSyncClientBuilder4.build();
    validateAppSyncClient(awsAppSyncClient4, clientDatabasePrefix, clientName);
    return awsAppSyncClient4;
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) JSONException(org.json.JSONException) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) NonNull(androidx.annotation.NonNull)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)28 Test (org.junit.Test)19 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)8 AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)6 AllPostsQuery (com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery)6 Response (com.apollographql.apollo.api.Response)5 GetPostQuery (com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery)4 ResponseFetcher (com.apollographql.apollo.fetcher.ResponseFetcher)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 JSONObject (org.json.JSONObject)4 DelegatingGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback)3 LatchedGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback)3 UpdatePostMutation (com.amazonaws.mobileconnectors.appsync.demo.UpdatePostMutation)3 CreatePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput)3 AppSyncResponseFetchers (com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers)3 BasicAPIKeyAuthProvider (com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider)3 Await (com.amazonaws.mobileconnectors.appsync.util.Await)3 Cancelable (com.apollographql.apollo.internal.util.Cancelable)3 Map (java.util.Map)3