@Test public void onNonPersistedQueryErrorOriginalCallbackCalled() { ApolloInterceptorChain chain = mock(ApolloInterceptorChain.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((ApolloInterceptor.CallBack) invocation.getArguments()[2]).onResponse( new ApolloInterceptor.InterceptorResponse( mockHttpResponse(), com.apollographql.apollo.api.Response.<MockOperation.Data>builder(new MockOperation()) .errors(Collections.singletonList(new Error("SomeOtherError", null, null))) .build(), Collections.<Record>emptyList() ) ); return null; } }).when(chain).proceedAsync( any(ApolloInterceptor.InterceptorRequest.class), any(Executor.class), any(ApolloInterceptor.CallBack.class) ); ApolloInterceptor.CallBack interceptorCallBack = mock(ApolloInterceptor.CallBack.class); interceptor.interceptAsync(request, chain, new TrampolineExecutor(), interceptorCallBack); verify(chain).proceedAsync(any(ApolloInterceptor.InterceptorRequest.class), any(Executor.class), any(ApolloInterceptor.CallBack.class)); ArgumentCaptor<ApolloInterceptor.InterceptorResponse> interceptorResponseArgumentCaptor = ArgumentCaptor.forClass(ApolloInterceptor.InterceptorResponse.class); verify(interceptorCallBack).onResponse(interceptorResponseArgumentCaptor.capture()); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().hasErrors()).isTrue(); }
verify(interceptorCallBack).onResponse(interceptorResponseArgumentCaptor.capture()); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().data()).isNotNull(); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().hasErrors()).isFalse();
@SuppressWarnings("unchecked") InterceptorResponse parse(Operation operation, okhttp3.Response httpResponse) throws ApolloHttpException, ApolloParseException { String cacheKey = httpResponse.request().header(HttpCache.CACHE_KEY_HEADER); if (httpResponse.isSuccessful()) { try { OperationResponseParser parser = new OperationResponseParser(operation, responseFieldMapper, scalarTypeAdapters, normalizer); Response parsedResponse = parser.parse(httpResponse.body().source()) .toBuilder() .fromCache(httpResponse.cacheResponse() != null) .build(); if (parsedResponse.hasErrors() && httpCache != null) { httpCache.removeQuietly(cacheKey); } return new InterceptorResponse(httpResponse, parsedResponse, normalizer.records()); } catch (Exception rethrown) { logger.e(rethrown, "Failed to parse network response for operation: %s", operation); closeQuietly(httpResponse); if (httpCache != null) { httpCache.removeQuietly(cacheKey); } throw new ApolloParseException("Failed to parse http response", rethrown); } } else { logger.e("Failed to parse network response: %s", httpResponse); throw new ApolloHttpException(httpResponse); } }
@NotNull @Override public Optional<InterceptorRequest> apply(@NotNull Response response) { if (response.hasErrors()) { if (isPersistedQueryNotFound(response.errors())) { 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).build()); } 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).build()); } } return Optional.absent(); } });
ArgumentCaptor.forClass(ApolloInterceptor.InterceptorResponse.class); verify(interceptorCallBack).onResponse(interceptorResponseArgumentCaptor.capture()); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().hasErrors()).isFalse(); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().data()).isNotNull();
ArgumentCaptor.forClass(ApolloInterceptor.InterceptorResponse.class); verify(interceptorCallBack).onResponse(interceptorResponseArgumentCaptor.capture()); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().hasErrors()).isFalse(); assertThat(interceptorResponseArgumentCaptor.getValue().parsedResponse.get().data()).isNotNull();
static boolean conflictPresent(Optional<Response> parsedResponse) { //Check if the parsed response contains a conflict. //The contract for conflicts is that the response will contain an error with the //string "The conditional request failed" and will contain a data element. if (parsedResponse == null || parsedResponse.get() == null || parsedResponse.get().hasErrors() == false) { return false; } Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: onResponse -- found error"); if ( ! parsedResponse.get().errors().get(0).toString().contains("The conditional request failed") ) { return false; } Map customAttributes = ((Error) parsedResponse.get().errors().get(0)).customAttributes(); if (customAttributes == null || customAttributes.get("data") == null ) { return false; } return true; }
public void onMessage(final String msg) { try { //TODO: Check why is this being converted to a Response Body ResponseBody messageBody = ResponseBody.create(MediaType.parse("text/plain"), msg); OperationResponseParser<D, T> parser = new OperationResponseParser(subscription, subscription.responseFieldMapper(), scalarTypeAdapters, normalizer); Response<T> parsedResponse = parser.parse(messageBody.source()); if (parsedResponse.hasErrors()) { Log.w(TAG, "Errors detected in parsed subscription message"); } //TODO: Check why the message is this is not done in an else clause propagateMessageToAllListeners(parsedResponse); } catch (Exception rethrown) { Log.e(TAG, "Failed to parse: " + msg, rethrown); notifyErrorToAllListeners(new ApolloParseException("Failed to parse http response", rethrown)); } }
.fromCache(httpResponse.cacheResponse() != null) .build(); if (parsedResponse.hasErrors() && httpCache != null) { httpCache.removeQuietly(cacheKey);
.fromCache(httpResponse.cacheResponse() != null) .build(); if (parsedResponse.hasErrors() && httpCache != null) { httpCache.removeQuietly(cacheKey);