@Override @NotNull public ApolloStoreOperation<Set<String>> write(@NotNull final GraphqlFragment fragment, @NotNull final CacheKey cacheKey, @NotNull final Operation.Variables variables) { checkNotNull(fragment, "fragment == null"); checkNotNull(cacheKey, "cacheKey == null"); checkNotNull(variables, "operation == null"); if (cacheKey.equals(CacheKey.NO_KEY)) { throw new IllegalArgumentException("undefined cache key"); } return new ApolloStoreOperation<Set<String>>(dispatcher) { @Override protected Set<String> perform() { return writeTransaction(new Transaction<WriteableStore, Set<String>>() { @Override public Set<String> execute(WriteableStore cache) { return doWrite(fragment, cacheKey, variables); } }); } }; }
@Override public void willResolveObject(ResponseField field, Optional<R> objectSource) { pathStack.push(path); CacheKey cacheKey = objectSource.isPresent() ? resolveCacheKey(field, objectSource.get()) : CacheKey.NO_KEY; String cacheKeyValue = cacheKey.key(); if (cacheKey.equals(CacheKey.NO_KEY)) { cacheKeyValue = pathToString(); } else { path = new ArrayList<>(); path.add(cacheKeyValue); } recordStack.push(currentRecordBuilder.build()); currentRecordBuilder = Record.builder(cacheKeyValue); }
private Record valueForObject(Record record, ResponseField field) { CacheReference cacheReference; CacheKey fieldCacheKey = cacheKeyResolver.fromFieldArguments(field, variables); if (!fieldCacheKey.equals(CacheKey.NO_KEY)) { cacheReference = new CacheReference(fieldCacheKey.key()); } else { cacheReference = fieldValue(record, field); } if (cacheReference != null) { Record referencedRecord = readableCache.read(cacheReference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } return referencedRecord; } return null; }