Skip to content

Commit a3e26a4

Browse files
chore(api) remove responseType from GraphQLOperation, since we already know it from the GraphQLRequest (#1063)
1 parent 0d08ad2 commit a3e26a4

File tree

6 files changed

+18
-30
lines changed

6 files changed

+18
-30
lines changed

aws-api/src/main/java/com/amplifyframework/api/aws/AppSyncGraphQLOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) {
136136
}
137137

138138
try {
139-
onResponse.accept(wrapResponse(jsonResponse, getResponseType()));
139+
onResponse.accept(wrapResponse(jsonResponse));
140140
//TODO: Dispatch to hub
141141
} catch (ApiException exception) {
142142
onFailure.accept(exception);

aws-api/src/main/java/com/amplifyframework/api/aws/GsonGraphQLResponseFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ final class GsonGraphQLResponseFactory implements GraphQLResponse.Factory {
5555
}
5656

5757
@Override
58-
public <T> GraphQLResponse<T> buildResponse(GraphQLRequest<T> request, String responseJson, Type typeOfT)
58+
public <T> GraphQLResponse<T> buildResponse(GraphQLRequest<T> request, String responseJson)
5959
throws ApiException {
60-
Type responseType = TypeMaker.getParameterizedType(GraphQLResponse.class, typeOfT);
60+
Type responseType = TypeMaker.getParameterizedType(GraphQLResponse.class, request.getResponseType());
6161
try {
6262
Gson responseGson = gson.newBuilder()
6363
.registerTypeHierarchyAdapter(Iterable.class, new IterableDeserializer<>(request))

aws-api/src/main/java/com/amplifyframework/api/aws/SubscriptionEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ void awaitSubscriptionCompleted() {
389389

390390
void dispatchNextMessage(String message) {
391391
try {
392-
onNextItem.accept(responseFactory.buildResponse(request, message, responseType));
392+
onNextItem.accept(responseFactory.buildResponse(request, message));
393393
} catch (ApiException exception) {
394394
dispatchError(exception);
395395
}

aws-api/src/test/java/com/amplifyframework/api/aws/GsonGraphQLResponseFactoryTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public void nullDataNullErrorsReturnsEmptyResponseObject() throws ApiException {
8181

8282
// Act! Parse it into a model.
8383
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
84+
GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
8485
final GraphQLResponse<PaginatedResult<Todo>> response =
85-
responseFactory.buildResponse(null, nullResponseJson, responseType);
86+
responseFactory.buildResponse(request, nullResponseJson);
8687

8788
// Assert that the model is constructed without content
8889
assertNotNull(response);
@@ -107,7 +108,7 @@ public void partialResponseRendersWithTodoDataAndErrors() throws ApiException {
107108
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
108109
GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
109110
final GraphQLResponse<PaginatedResult<Todo>> response =
110-
responseFactory.buildResponse(request, partialResponseJson, responseType);
111+
responseFactory.buildResponse(request, partialResponseJson);
111112

112113
// Assert that the model contained things...
113114
assertNotNull(response);
@@ -222,7 +223,7 @@ public void responseRendersAsPaginatedResult() throws AmplifyException {
222223

223224
final GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
224225
final GraphQLResponse<PaginatedResult<Todo>> response =
225-
responseFactory.buildResponse(request, partialResponseJson, responseType);
226+
responseFactory.buildResponse(request, partialResponseJson);
226227

227228
// Assert
228229
assertEquals(expectedResponse, response);
@@ -248,8 +249,9 @@ public void errorResponseDeserializesExtensionsMap() throws ApiException {
248249

249250
// Act! Parse it into a model.
250251
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
252+
GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
251253
final GraphQLResponse<PaginatedResult<Todo>> response =
252-
responseFactory.buildResponse(null, partialResponseJson, responseType);
254+
responseFactory.buildResponse(request, partialResponseJson);
253255

254256
// Build the expected response.
255257
String message = "Conflict resolver rejects mutation.";
@@ -298,8 +300,9 @@ public void errorWithNullFieldsCanBeParsed() throws ApiException {
298300

299301
// Act! Parse it into a model.
300302
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
303+
GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
301304
final GraphQLResponse<PaginatedResult<Todo>> response =
302-
responseFactory.buildResponse(null, responseJson, responseType);
305+
responseFactory.buildResponse(request, responseJson);
303306

304307
// Build the expected response.
305308
Map<String, Object> extensions = new HashMap<>();
@@ -331,7 +334,7 @@ public void partialResponseCanBeRenderedAsStringType() throws JSONException, Api
331334

332335
// Act! Parse it into a String data type.
333336
final GraphQLResponse<String> response =
334-
responseFactory.buildResponse(request, partialResponseJson.toString(), String.class);
337+
responseFactory.buildResponse(request, partialResponseJson.toString());
335338

336339
// Assert that the response data is just the data block as a JSON string
337340
assertEquals(
@@ -352,7 +355,7 @@ public void syncQueryResponseCanBeRenderedAsStringType() throws ApiException {
352355
Type responseType = TypeMaker.getParameterizedType(Iterable.class, String.class);
353356
GraphQLRequest<Iterable<String>> request = buildDummyRequest(responseType);
354357
GraphQLResponse<Iterable<String>> response =
355-
responseFactory.buildResponse(request, baseQueryResponseJson.toString(), responseType);
358+
responseFactory.buildResponse(request, baseQueryResponseJson.toString());
356359
final Iterable<String> queryResults = response.getData();
357360

358361
final List<String> resultJsons = new ArrayList<>();
@@ -434,7 +437,7 @@ public void awsDateTypesCanBeDeserialized() throws ApiException {
434437
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Meeting.class);
435438
GraphQLRequest<PaginatedResult<Meeting>> request = buildDummyRequest(responseType);
436439
final GraphQLResponse<PaginatedResult<Meeting>> response =
437-
responseFactory.buildResponse(request, responseString, responseType);
440+
responseFactory.buildResponse(request, responseString);
438441
final Iterable<Meeting> actualMeetings = response.getData().getItems();
439442

440443
// Assert

core/src/main/java/com/amplifyframework/api/graphql/GraphQLOperation.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import com.amplifyframework.api.ApiException;
2222
import com.amplifyframework.api.ApiOperation;
2323

24-
import java.lang.reflect.Type;
25-
2624
/**
2725
* A GraphQLOperation is an API operation which returns a GraphQLResponse.
2826
* @param <R> The type of data contained in the GraphQLResponse.
2927
*/
3028
public abstract class GraphQLOperation<R> extends ApiOperation<GraphQLRequest<R>> {
3129
private final GraphQLResponse.Factory responseFactory;
32-
private final Type responseType;
3330

3431
/**
3532
* Constructs a new instance of a GraphQLOperation.
@@ -42,31 +39,21 @@ public GraphQLOperation(
4239
) {
4340
super(graphQLRequest);
4441
this.responseFactory = responseFactory;
45-
this.responseType = graphQLRequest.getResponseType();
4642
}
4743

4844
/**
4945
* Converts a response json string containing a single object to a formatted
5046
* {@link GraphQLResponse} object that a response consumer can receive.
5147
* @param jsonResponse json response from API to be converted
52-
* @param type Type of R, the data contained in the GraphQLResponse
5348
* @return wrapped response object
5449
* @throws ApiException If the class provided mismatches the data
5550
*/
56-
protected final GraphQLResponse<R> wrapResponse(String jsonResponse, Type type) throws ApiException {
51+
protected final GraphQLResponse<R> wrapResponse(String jsonResponse) throws ApiException {
5752
try {
58-
return responseFactory.buildResponse(getRequest(), jsonResponse, type);
53+
return responseFactory.buildResponse(getRequest(), jsonResponse);
5954
} catch (ClassCastException cce) {
6055
throw new ApiException("Amplify encountered an error while deserializing an object",
6156
AmplifyException.TODO_RECOVERY_SUGGESTION);
6257
}
6358
}
64-
65-
/**
66-
* Gets the Type to use for deserializing the response.
67-
* @return response type
68-
*/
69-
protected final Type getResponseType() {
70-
return responseType;
71-
}
7259
}

core/src/main/java/com/amplifyframework/api/graphql/GraphQLResponse.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.amplifyframework.api.ApiException;
2323
import com.amplifyframework.util.Immutable;
2424

25-
import java.lang.reflect.Type;
2625
import java.util.ArrayList;
2726
import java.util.List;
2827
import java.util.Map;
@@ -235,12 +234,11 @@ public interface Factory {
235234
* @param request The request which resulted in this GraphQLResponse
236235
* @param apiResponseJson Response from the endpoint, containing a string response
237236
*
238-
* @param typeOfR The typeOfR to which the JSON string should be interpreted
239237
* @param <R> The typeOfR of the response object
240238
* @return An instance of provided typeOfR which models the data provided in the response JSON string
241239
* @throws ApiException If the class provided mismatches the data
242240
*/
243-
<R> GraphQLResponse<R> buildResponse(GraphQLRequest<R> request, String apiResponseJson, Type typeOfR)
241+
<R> GraphQLResponse<R> buildResponse(GraphQLRequest<R> request, String apiResponseJson)
244242
throws ApiException;
245243
}
246244
}

0 commit comments

Comments
 (0)