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(); } }
@Nullable @Override public T execute(ReadableStore cache) { Record rootRecord = cache.read(CacheKeyResolver.rootKeyForOperation(operation).key(), CacheHeaders.NONE); if (rootRecord == null) { return null; } ResponseFieldMapper<D> responseFieldMapper = operation.responseFieldMapper(); CacheFieldValueResolver fieldValueResolver = new CacheFieldValueResolver(cache, operation.variables(), cacheKeyResolver(), CacheHeaders.NONE, cacheKeyBuilder); //noinspection unchecked RealResponseReader<Record> responseReader = new RealResponseReader<>(operation.variables(), rootRecord, fieldValueResolver, scalarTypeAdapters, ResponseNormalizer.NO_OP_NORMALIZER); return operation.wrapData(responseFieldMapper.map(responseReader)); } });
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()); }
@SuppressWarnings("unchecked") public Response<W> parse(@NotNull Map<String, Object> payload) { checkNotNull(payload, "payload == null"); D data = null; if (payload.containsKey("data")) { Map<String, Object> buffer = (Map<String, Object>) payload.get("data"); RealResponseReader<Map<String, Object>> realResponseReader = new RealResponseReader<>(operation.variables(), buffer, new MapFieldValueResolver(), scalarTypeAdapters, responseNormalizer); data = (D) responseFieldMapper.map(realResponseReader); } List<Error> errors = null; if (payload.containsKey("errors")) { List<Map<String, Object>> errorPayloads = (List<Map<String, Object>>) payload.get("errors"); if (errorPayloads != null) { errors = new ArrayList<>(); for (Map<String, Object> errorPayload : errorPayloads) { errors.add(readError(errorPayload)); } } } return Response.<W>builder(operation) .data(operation.wrapData(data)) .errors(errors) .dependentKeys(responseNormalizer.dependentKeys()) .build(); }
/** * <p>Adds provided {@link ApolloMutationCall} that is currently in progress.</p> * * <p><b>Note</b>: This method needs to be called right before an apolloCall is executed.</p> */ void registerMutationCall(@NotNull ApolloMutationCall apolloMutationCall) { checkNotNull(apolloMutationCall, "apolloMutationCall == null"); OperationName operationName = apolloMutationCall.operation().name(); registerCall(activeMutationCalls, operationName, apolloMutationCall); }
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()); }
return new RealResponseReader<>(EMPTY_OPERATION.variables(), recordSet, new MapFieldValueResolver(), new ScalarTypeAdapters(customTypeAdapters), NO_OP_NORMALIZER);
private String httpRequestBody(Operation operation) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.beginObject(); 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 buffer.readUtf8(); }
@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 } }
@NotNull public ResponseFieldMapper create(@NotNull Operation operation) { checkNotNull(operation, "operation == null"); Class operationClass = operation.getClass(); ResponseFieldMapper mapper = pool.get(operationClass); if (mapper != null) { return mapper; } pool.putIfAbsent(operationClass, operation.responseFieldMapper()); return pool.get(operationClass); } }
.data(operation.wrapData(data)) .errors(errors) .dependentKeys(responseNormalizer.dependentKeys())
/** * <p>Adds provided {@link ApolloPrefetch} that is currently in progress.</p> * * <p><b>Note</b>: This method needs to be called right before a prefetch call is executed.</p> */ void registerPrefetchCall(@NotNull ApolloPrefetch apolloPrefetch) { checkNotNull(apolloPrefetch, "apolloPrefetch == null"); OperationName operationName = apolloPrefetch.operation().name(); registerCall(activePrefetchCalls, operationName, apolloPrefetch); }
@NotNull @Override public Response<T> execute(ReadableStore cache) { Record rootRecord = cache.read(CacheKeyResolver.rootKeyForOperation(operation).key(), cacheHeaders); if (rootRecord == null) { return Response.<T>builder(operation).fromCache(true).build(); } CacheFieldValueResolver fieldValueResolver = new CacheFieldValueResolver(cache, operation.variables(), cacheKeyResolver(), cacheHeaders, cacheKeyBuilder); RealResponseReader<Record> responseReader = new RealResponseReader<>(operation.variables(), rootRecord, fieldValueResolver, scalarTypeAdapters, responseNormalizer); try { responseNormalizer.willResolveRootQuery(operation); T data = operation.wrapData(responseFieldMapper.map(responseReader)); return Response.<T>builder(operation) .data(data) .fromCache(true) .dependentKeys(responseNormalizer.dependentKeys()) .build(); } catch (Exception e) { logger.e(e, "Failed to read cache response"); return Response.<T>builder(operation).fromCache(true).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()); }
@Override public Set<String> execute(WriteableStore cache) { RealResponseWriter responseWriter = new RealResponseWriter(operation.variables(), scalarTypeAdapters); operationData.marshaller().marshal(responseWriter); ResponseNormalizer<Map<String, Object>> responseNormalizer = networkResponseNormalizer(); responseNormalizer.willResolveRootQuery(operation); responseWriter.resolveFields(responseNormalizer); if (optimistic) { List<Record> updatedRecords = new ArrayList<>(); for (Record record : responseNormalizer.records()) { updatedRecords.add(record.toBuilder().mutationId(mutationId).build()); } return optimisticCache.mergeOptimisticUpdates(updatedRecords); } else { return optimisticCache.merge(responseNormalizer.records(), CacheHeaders.NONE); } } });
@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(); } });
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()); }
@Nonnull public ResponseFieldMapper create(@Nonnull Operation operation) { checkNotNull(operation, "operation == null"); Class operationClass = operation.getClass(); ResponseFieldMapper mapper = pool.get(operationClass); if (mapper != null) { return mapper; } pool.putIfAbsent(operationClass, operation.responseFieldMapper()); return pool.get(operationClass); } }
.data(operation.wrapData(data)) .errors(errors) .dependentKeys(responseNormalizer.dependentKeys())
/** * <p>Adds provided {@link ApolloQueryWatcher} that is currently in progress.</p> * * <p><b>Note</b>: This method needs to be called right before * {@link ApolloQueryWatcher#enqueueAndWatch(ApolloCall.Callback)}.</p> */ void registerQueryWatcher(@NotNull ApolloQueryWatcher queryWatcher) { checkNotNull(queryWatcher, "queryWatcher == null"); OperationName operationName = queryWatcher.operation().name(); registerCall(activeQueryWatchers, operationName, queryWatcher); }