Skip to content

Commit

Permalink
remove redundant variable, reuse original request for retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Hong committed Jun 24, 2019
1 parent 658c620 commit b35c6b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class ApolloAutoPersistedQueryInterceptor implements ApolloInterceptor {

private final ApolloLogger logger;
private volatile boolean disposed;
private boolean useHttpGetMethodForQueries;

final boolean useHttpGetMethodForPersistedQueries;

Expand All @@ -34,12 +33,10 @@ public ApolloAutoPersistedQueryInterceptor(@NotNull ApolloLogger logger,
public void interceptAsync(@NotNull final InterceptorRequest request, @NotNull final ApolloInterceptorChain chain,
@NotNull final Executor dispatcher, @NotNull final CallBack callBack) {

useHttpGetMethodForQueries = request.useHttpGetMethodForQueries;

InterceptorRequest newRequest = request.toBuilder()
.sendQueryDocument(false)
.autoPersistQueries(true)
.useHttpGetMethodForQueries(useHttpGetMethodForQueries || useHttpGetMethodForPersistedQueries)
.useHttpGetMethodForQueries(request.useHttpGetMethodForQueries || useHttpGetMethodForPersistedQueries)
.build();
chain.proceedAsync(newRequest, dispatcher, new CallBack() {
@Override public void onResponse(@NotNull InterceptorResponse response) {
Expand Down Expand Up @@ -81,21 +78,13 @@ Optional<InterceptorRequest> handleProtocolNegotiation(final InterceptorRequest
logger.w("GraphQL server couldn't find Automatic Persisted Query for operation name: "
+ request.operation.name().name() + " id: " + request.operation.operationId());

return Optional.of(request.toBuilder()
.sendQueryDocument(true)
.autoPersistQueries(true)
.useHttpGetMethodForQueries(useHttpGetMethodForQueries)
.build());
return Optional.of(request);
}

if (isPersistedQueryNotSupported(response.errors())) {
// TODO how to disable Automatic Persisted Queries in future and how to notify user about this
logger.e("GraphQL server doesn't support Automatic Persisted Queries");
return Optional.of(request.toBuilder()
.sendQueryDocument(true)
.autoPersistQueries(true)
.useHttpGetMethodForQueries(useHttpGetMethodForQueries)
.build());
return Optional.of(request);
}
}
return Optional.absent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class ApolloAutoPersistedQueryInterceptorTest {
new ApolloAutoPersistedQueryInterceptor(new ApolloLogger(Optional.<Logger>absent()), true);

private ApolloInterceptor.InterceptorRequest request = ApolloInterceptor.InterceptorRequest.builder(new MockOperation())
.build();
.autoPersistQueries(true)
.build();

@Test
public void initialRequestWithoutQueryDocument() {
Expand All @@ -60,6 +61,8 @@ public void initialRequestWithoutQueryDocument() {
any(ApolloInterceptor.CallBack.class));

assertThat(requestArgumentCaptor.getValue().sendQueryDocument).isFalse();
assertThat(requestArgumentCaptor.getValue().useHttpGetMethodForQueries).isFalse();
assertThat(requestArgumentCaptor.getValue().autoPersistQueries).isTrue();
}

@Test
Expand All @@ -73,7 +76,9 @@ public void initialRequestWithGetMethodForPersistedQueries() {
verify(chain).proceedAsync(requestArgumentCaptor.capture(), any(Executor.class),
any(ApolloInterceptor.CallBack.class));

assertThat(requestArgumentCaptor.getValue().sendQueryDocument).isFalse();
assertThat(requestArgumentCaptor.getValue().useHttpGetMethodForQueries).isTrue();
assertThat(requestArgumentCaptor.getValue().autoPersistQueries).isTrue();
}

@Test
Expand All @@ -85,6 +90,7 @@ public void proceedAsync(@NotNull ApolloInterceptor.InterceptorRequest request,
super.proceedAsync(request, dispatcher, callBack);
if (proceedAsyncInvocationCount == 1) {
assertThat(request.sendQueryDocument).isFalse();
assertThat(request.autoPersistQueries).isTrue();
callBack.onResponse(
new ApolloInterceptor.InterceptorResponse(
mockHttpResponse(),
Expand All @@ -96,6 +102,7 @@ public void proceedAsync(@NotNull ApolloInterceptor.InterceptorRequest request,
);
} else if (proceedAsyncInvocationCount == 2) {
assertThat(request.sendQueryDocument).isTrue();
assertThat(request.autoPersistQueries).isTrue();
callBack.onResponse(
new ApolloInterceptor.InterceptorResponse(
mockHttpResponse(),
Expand Down Expand Up @@ -132,6 +139,7 @@ public void proceedAsync(@NotNull ApolloInterceptor.InterceptorRequest request,
super.proceedAsync(request, dispatcher, callBack);
if (proceedAsyncInvocationCount == 1) {
assertThat(request.sendQueryDocument).isFalse();
assertThat(request.autoPersistQueries).isTrue();
callBack.onResponse(
new ApolloInterceptor.InterceptorResponse(
mockHttpResponse(),
Expand All @@ -143,6 +151,7 @@ public void proceedAsync(@NotNull ApolloInterceptor.InterceptorRequest request,
);
} else if (proceedAsyncInvocationCount == 2) {
assertThat(request.sendQueryDocument).isTrue();
assertThat(request.autoPersistQueries).isTrue();
callBack.onResponse(
new ApolloInterceptor.InterceptorResponse(
mockHttpResponse(),
Expand Down

0 comments on commit b35c6b4

Please sign in to comment.