Java Code Examples for com.apollographql.apollo.api.Operation#Variables

The following examples show how to use com.apollographql.apollo.api.Operation#Variables . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RealApolloStore.java    From apollo-android with MIT License 6 votes vote down vote up
<D extends Operation.Data, T, V extends Operation.Variables> Set<String> doWrite(
    final Operation<D, T, V> operation, final D operationData, final boolean optimistic,
    final UUID mutationId) {
  return writeTransaction(new Transaction<WriteableStore, Set<String>>() {
    @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);
      }
    }
  });
}
 
Example 2
Source File: RealApolloStore.java    From apollo-android with MIT License 6 votes vote down vote up
@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);
        }
      });
    }
  };
}
 
Example 3
Source File: ResponseNormalizer.java    From apollo-android with MIT License 5 votes vote down vote up
@Override public void didResolve(ResponseField field, Operation.Variables variables) {
  path.remove(path.size() - 1);
  Object value = valueStack.pop();
  String cacheKey = cacheKeyBuilder().build(field, variables);
  String dependentKey = currentRecordBuilder.getKey() + "." + cacheKey;
  dependentKeys.add(dependentKey);
  currentRecordBuilder.addField(cacheKey, value);

  if (recordStack.isEmpty()) {
    recordSet.merge(currentRecordBuilder.build());
  }
}
 
Example 4
Source File: RealApolloStore.java    From apollo-android with MIT License 5 votes vote down vote up
@Override @NotNull public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<T> read(
    @NotNull final Operation<D, T, V> operation) {
  checkNotNull(operation, "operation == null");
  return new ApolloStoreOperation<T>(dispatcher) {
    @Override protected T perform() {
      return doRead(operation);
    }
  };
}
 
Example 5
Source File: RealApolloStore.java    From apollo-android with MIT License 5 votes vote down vote up
Set<String> doWrite(final GraphqlFragment fragment, final CacheKey cacheKey, final Operation.Variables variables) {
  return writeTransaction(new Transaction<WriteableStore, Set<String>>() {
    @Override public Set<String> execute(WriteableStore cache) {
      RealResponseWriter responseWriter = new RealResponseWriter(variables, scalarTypeAdapters);
      fragment.marshaller().marshal(responseWriter);

      ResponseNormalizer<Map<String, Object>> responseNormalizer = networkResponseNormalizer();
      responseNormalizer.willResolveRecord(cacheKey);
      responseWriter.resolveFields(responseNormalizer);

      return merge(responseNormalizer.records(), CacheHeaders.NONE);
    }
  });
}
 
Example 6
Source File: IdFieldCacheKeyResolver.java    From apollo-android with MIT License 5 votes vote down vote up
@NotNull @Override
public CacheKey fromFieldArguments(@NotNull ResponseField field, @NotNull Operation.Variables variables) {
  Object id = field.resolveArgument("id", variables);
  if (id != null) {
    return formatCacheKey(id.toString());
  } else {
    return formatCacheKey(null);
  }
}
 
Example 7
Source File: RealApolloStore.java    From apollo-android with MIT License 5 votes vote down vote up
@NotNull @Override
public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Set<String>>
writeOptimisticUpdates(@NotNull final Operation<D, T, V> operation, @NotNull final D operationData,
    @NotNull final UUID mutationId) {
  return new ApolloStoreOperation<Set<String>>(dispatcher) {
    @Override protected Set<String> perform() {
      return doWrite(operation, operationData, true, mutationId);
    }
  };
}
 
Example 8
Source File: RealApolloStore.java    From apollo-android with MIT License 5 votes vote down vote up
<D extends Operation.Data, T, V extends Operation.Variables> Response<T> doRead(
    final Operation<D, T, V> operation, final ResponseFieldMapper<D> responseFieldMapper,
    final ResponseNormalizer<Record> responseNormalizer, final CacheHeaders cacheHeaders) {
  return readTransaction(new Transaction<ReadableStore, Response<T>>() {
    @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();
      }
    }
  });
}
 
Example 9
Source File: RealApolloStore.java    From apollo-android with MIT License 5 votes vote down vote up
@Override @NotNull public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Boolean>
writeAndPublish(@NotNull final Operation<D, T, V> operation, @NotNull final D operationData) {
  return new ApolloStoreOperation<Boolean>(dispatcher) {
    @Override protected Boolean perform() {
      Set<String> changedKeys = doWrite(operation, operationData, false, null);
      publish(changedKeys);
      return Boolean.TRUE;
    }
  };
}
 
Example 10
Source File: TestQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 11
Source File: TestQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 12
Source File: TestQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 13
Source File: TestQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 14
Source File: ResponseNormalizer.java    From apollo-android with MIT License 4 votes vote down vote up
@Override public void willResolve(ResponseField field, Operation.Variables variables, @Nullable Object value) {
}
 
Example 15
Source File: ResponseNormalizer.java    From apollo-android with MIT License 4 votes vote down vote up
@Override public void didResolve(ResponseField field, Operation.Variables variables) {
}
 
Example 16
Source File: TestQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 17
Source File: TestQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 18
Source File: HeroDetailQuery.java    From apollo-android with MIT License 4 votes vote down vote up
@Override
public Operation.Variables variables() {
  return variables;
}
 
Example 19
Source File: NoOpApolloStore.java    From apollo-android with MIT License 4 votes vote down vote up
@NotNull @Override
public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Response<T>> read(
    @NotNull Operation<D, T, V> operation, @NotNull ResponseFieldMapper<D> responseFieldMapper,
    @NotNull ResponseNormalizer<Record> responseNormalizer, @NotNull CacheHeaders cacheHeaders) {
  return ApolloStoreOperation.emptyOperation(Response.<T>builder(operation).build());
}
 
Example 20
Source File: ApolloPrefetch.java    From apollo-android with MIT License 2 votes vote down vote up
/**
 * Creates the ApolloPrefetch by wrapping the operation object inside.
 *
 * @param operation the operation which needs to be performed
 * @return The ApolloPrefetch object with the wrapped operation object
 */
<D extends Operation.Data, T, V extends Operation.Variables> ApolloPrefetch prefetch(
    @NotNull Operation<D, T, V> operation);