Call httpCall(Operation operation, CacheHeaders cacheHeaders, boolean writeQueryDocument) throws IOException { RequestBody requestBody = RequestBody.create(MEDIA_TYPE, httpRequestBody(operation, scalarTypeAdapters, writeQueryDocument)); Request.Builder requestBuilder = new Request.Builder() .url(serverUrl) .post(requestBody) .header(HEADER_ACCEPT_TYPE, ACCEPT_TYPE) .header(HEADER_CONTENT_TYPE, CONTENT_TYPE) .header(HEADER_APOLLO_OPERATION_ID, operation.operationId()) .header(HEADER_APOLLO_OPERATION_NAME, operation.name().name()) .tag(operation.operationId()); if (cachePolicy.isPresent()) { HttpCachePolicy.Policy cachePolicy = this.cachePolicy.get(); boolean skipCacheHttpResponse = "true".equalsIgnoreCase(cacheHeaders.headerValue( ApolloCacheHeaders.DO_NOT_STORE)); String cacheKey = httpRequestBody(operation, scalarTypeAdapters, true).md5().hex(); requestBuilder = requestBuilder .header(HttpCache.CACHE_KEY_HEADER, cacheKey) .header(HttpCache.CACHE_FETCH_STRATEGY_HEADER, cachePolicy.fetchStrategy.name()) .header(HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER, String.valueOf(cachePolicy.expireTimeoutMs())) .header(HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER, Boolean.toString(cachePolicy.expireAfterRead)) .header(HttpCache.CACHE_PREFETCH_HEADER, Boolean.toString(prefetch)) .header(HttpCache.CACHE_DO_NOT_STORE, Boolean.toString(skipCacheHttpResponse)); } return httpCallFactory.newCall(requestBuilder.build()); }
@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(); } });
static ByteString httpRequestBody(Operation operation, ScalarTypeAdapters scalarTypeAdapters, boolean writeQueryDocument) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.setSerializeNulls(true); jsonWriter.beginObject(); jsonWriter.name("operationName").value(operation.name().name()); jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.name("extensions") .beginObject() .name("persistedQuery") .beginObject() .name("version").value(1) .name("sha256Hash").value(operation.operationId()) .endObject() .endObject(); if (writeQueryDocument) { jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); } jsonWriter.endObject(); jsonWriter.close(); return buffer.readByteString(); } }
@Override public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) { if(mutationMap.containsKey(request.operation.operationId())) { // already in map } }
private Call httpCall(Operation operation) throws IOException { RequestBody requestBody = httpRequestBody(operation); Request.Builder requestBuilder = new Request.Builder() .url(serverUrl) .post(requestBody) .header(HEADER_ACCEPT_TYPE, ACCEPT_TYPE) .header(HEADER_CONTENT_TYPE, CONTENT_TYPE) .header(HEADER_APOLLO_OPERATION_ID, operation.operationId()); if (cachePolicy.isPresent()) { HttpCachePolicy.Policy cachePolicy = this.cachePolicy.get(); String cacheKey = cacheKey(requestBody); requestBuilder = requestBuilder .header(HttpCache.CACHE_KEY_HEADER, cacheKey) .header(HttpCache.CACHE_FETCH_STRATEGY_HEADER, cachePolicy.fetchStrategy.name()) .header(HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER, String.valueOf(cachePolicy.expireTimeoutMs())) .header(HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER, Boolean.toString(cachePolicy.expireAfterRead)) .header(HttpCache.CACHE_PREFETCH_HEADER, Boolean.toString(prefetch)); } return httpCallFactory.newCall(requestBuilder.build()); }
private Call httpCall(Operation operation) throws IOException { RequestBody requestBody = httpRequestBody(operation); Request.Builder requestBuilder = new Request.Builder() .url(serverUrl) .post(requestBody) .header(HEADER_ACCEPT_TYPE, ACCEPT_TYPE) .header(HEADER_CONTENT_TYPE, CONTENT_TYPE) .header(HEADER_APOLLO_OPERATION_ID, operation.operationId()); if (cachePolicy.isPresent()) { HttpCachePolicy.Policy cachePolicy = this.cachePolicy.get(); String cacheKey = cacheKey(requestBody); requestBuilder = requestBuilder .header(HttpCache.CACHE_KEY_HEADER, cacheKey) .header(HttpCache.CACHE_FETCH_STRATEGY_HEADER, cachePolicy.fetchStrategy.name()) .header(HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER, String.valueOf(cachePolicy.expireTimeoutMs())) .header(HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER, Boolean.toString(cachePolicy.expireAfterRead)) .header(HttpCache.CACHE_PREFETCH_HEADER, Boolean.toString(prefetch)); } return httpCallFactory.newCall(requestBuilder.build()); }
private RequestBody httpRequestBody(Operation operation) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.beginObject(); if (sendOperationIdentifiers) { jsonWriter.name("id").value(operation.operationId()); } else { jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); } jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.endObject(); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); }
private RequestBody httpRequestBody(Operation operation) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.beginObject(); if (sendOperationIdentifiers) { jsonWriter.name("id").value(operation.operationId()); } else { jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); } jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.endObject(); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); }