Search in sources :

Example 6 with GraphQLLocation

use of com.amplifyframework.api.graphql.GraphQLLocation in project amplify-android by aws-amplify.

the class GsonGraphQLResponseFactoryTest method partialResponseRendersWithTodoDataAndErrors.

/**
 * Validates that the converter is able to parse a partial GraphQL
 * response into a result. In this case, the result contains some
 * data, but also a list of errors.
 * @throws ApiException From API configuration
 */
@Test
public void partialResponseRendersWithTodoDataAndErrors() throws ApiException {
    // Arrange some JSON string from a "server"
    final String partialResponseJson = Resources.readAsString("partial-gql-response.json");
    // Act! Parse it into a model.
    Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
    GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
    final GraphQLResponse<PaginatedResult<Todo>> response = responseFactory.buildResponse(request, partialResponseJson);
    // Assert that the model contained things...
    assertNotNull(response);
    assertNotNull(response.getData());
    assertNotNull(response.getData().getItems());
    // Assert that all of the fields of the different todos
    // match what we would expect from a manual inspection of the
    // JSON.
    final Iterable<Todo> actualTodos = response.getData().getItems();
    final List<Todo> expectedTodos = Arrays.asList(Todo.builder().id("fa1c21cc-0458-4bca-bcb1-101579fb85c7").name(null).description("Test").build(), Todo.builder().id("68bad242-dec5-415b-acb3-daee3b069ce5").name(null).description("Test").build(), Todo.builder().id("f64e2e9a-42ad-4455-b8ee-d1cfae7e9f01").name(null).description("Test").build());
    assertEquals(expectedTodos, actualTodos);
    // Assert that we parsed the errors successfully.
    assertNotNull(response.getErrors());
    final List<GraphQLResponse.Error> expectedErrors = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        String message = "failed";
        List<GraphQLLocation> locations = Collections.singletonList(new GraphQLLocation(5, 7));
        List<GraphQLPathSegment> path = Arrays.asList(new GraphQLPathSegment("listTodos"), new GraphQLPathSegment("items"), new GraphQLPathSegment(i), new GraphQLPathSegment("name"));
        Map<String, Object> extensions = new HashMap<>();
        extensions.put("errorType", null);
        extensions.put("errorInfo", null);
        extensions.put("data", null);
        expectedErrors.add(new GraphQLResponse.Error(message, locations, path, extensions));
    }
    assertEquals(expectedErrors, response.getErrors());
}
Also used : GraphQLLocation(com.amplifyframework.api.graphql.GraphQLLocation) HashMap(java.util.HashMap) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) ArrayList(java.util.ArrayList) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) GraphQLPathSegment(com.amplifyframework.api.graphql.GraphQLPathSegment) QueryType(com.amplifyframework.api.graphql.QueryType) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Aggregations

GraphQLLocation (com.amplifyframework.api.graphql.GraphQLLocation)6 GraphQLPathSegment (com.amplifyframework.api.graphql.GraphQLPathSegment)6 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)6 HashMap (java.util.HashMap)6 Test (org.junit.Test)5 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)4 JSONObject (org.json.JSONObject)4 QueryType (com.amplifyframework.api.graphql.QueryType)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Context (android.content.Context)1 NonNull (androidx.annotation.NonNull)1 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)1 AmplifyException (com.amplifyframework.AmplifyException)1 ApiCategory (com.amplifyframework.api.ApiCategory)1 ApiCategoryConfiguration (com.amplifyframework.api.ApiCategoryConfiguration)1 ApiPlugin (com.amplifyframework.api.ApiPlugin)1 ApiChannelEventName (com.amplifyframework.api.events.ApiChannelEventName)1