Search in sources :

Example 1 with QueryPredicateGroup

use of com.amplifyframework.core.model.query.predicate.QueryPredicateGroup in project amplify-android by aws-amplify.

the class AppSyncRequestFactory method parsePredicate.

static Map<String, Object> parsePredicate(QueryPredicate queryPredicate) throws DataStoreException {
    if (QueryPredicates.all().equals(queryPredicate)) {
        return Collections.singletonMap("id", Collections.singletonMap("ne", null));
    }
    if (QueryPredicates.none().equals(queryPredicate)) {
        // id cannot be null, so match none
        return Collections.singletonMap("id", Collections.singletonMap("eq", null));
    }
    if (queryPredicate instanceof QueryPredicateOperation) {
        QueryPredicateOperation<?> qpo = (QueryPredicateOperation<?>) queryPredicate;
        QueryOperator<?> op = qpo.operator();
        return Collections.singletonMap(qpo.field(), Collections.singletonMap(appSyncOpType(op.type()), appSyncOpValue(op)));
    } else if (queryPredicate instanceof QueryPredicateGroup) {
        QueryPredicateGroup qpg = (QueryPredicateGroup) queryPredicate;
        if (QueryPredicateGroup.Type.NOT.equals(qpg.type())) {
            try {
                return Collections.singletonMap("not", parsePredicate(qpg.predicates().get(0)));
            } catch (IndexOutOfBoundsException exception) {
                throw new DataStoreException("Predicate group of type NOT must include a value to negate.", exception, "Check if you created a NOT condition in your Predicate with no included value.");
            }
        } else {
            List<Map<String, Object>> predicates = new ArrayList<>();
            for (QueryPredicate predicate : qpg.predicates()) {
                predicates.add(parsePredicate(predicate));
            }
            return Collections.singletonMap(qpg.type().toString().toLowerCase(Locale.getDefault()), predicates);
        }
    } else {
        throw new DataStoreException("Tried to parse an unsupported QueryPredicate", "Try changing to one of the supported values: QueryPredicateOperation, QueryPredicateGroup.");
    }
}
Also used : QueryPredicateOperation(com.amplifyframework.core.model.query.predicate.QueryPredicateOperation) DataStoreException(com.amplifyframework.datastore.DataStoreException) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) QueryPredicateGroup(com.amplifyframework.core.model.query.predicate.QueryPredicateGroup) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with QueryPredicateGroup

use of com.amplifyframework.core.model.query.predicate.QueryPredicateGroup in project amplify-android by aws-amplify.

the class AppSyncRequestFactory method buildSyncRequest.

/**
 * Builds the query document for base and delta sync.
 * If you provide lastSyncTime, it builds a delta sync, where the delta is computed
 * against the provided time. Otherwise, if you provide a null lastSyncTime, a
 * request doc is generated for a base sync.
 * @param modelSchema Schema Class for which we want to sync.
 * @param lastSync The last time synced. If not provided, do a base query.
 *                 If provided, do a delta query.
 * @param <T> The type of objects we are syncing
 * @return A string which contains a GraphQL query doc for an base/delta sync
 * @throws DataStoreException On Failure to inspect
 */
@NonNull
static <T> AppSyncGraphQLRequest<T> buildSyncRequest(@NonNull final ModelSchema modelSchema, @Nullable final Long lastSync, @Nullable final Integer limit, @NonNull final QueryPredicate predicate, @NonNull final AuthModeStrategyType strategyType) throws DataStoreException {
    try {
        AppSyncGraphQLRequest.Builder builder = AppSyncGraphQLRequest.builder().modelClass(modelSchema.getModelClass()).modelSchema(modelSchema).operation(QueryType.SYNC).requestAuthorizationStrategyType(strategyType).requestOptions(new DataStoreGraphQLRequestOptions()).responseType(TypeMaker.getParameterizedType(PaginatedResult.class, ModelWithMetadata.class, modelSchema.getModelClass()));
        if (lastSync != null) {
            builder.variable("lastSync", "AWSTimestamp", lastSync);
        }
        if (limit != null) {
            builder.variable("limit", "Int", limit);
        }
        if (!QueryPredicates.all().equals(predicate)) {
            String filterType = "Model" + Casing.capitalizeFirst(modelSchema.getName()) + "FilterInput";
            QueryPredicate syncPredicate = predicate;
            if (!(syncPredicate instanceof QueryPredicateGroup)) {
                // When a filter is provided, wrap it with a predicate group of type AND.  By doing this, it enables
                // AppSync to optimize the request by performing a DynamoDB query instead of a scan.  If the
                // provided syncPredicate is already a QueryPredicateGroup, this is not needed.  If the provided
                // group is of type AND, the optimization will occur.  If the top level group is OR or NOT, the
                // optimization is not possible anyway.
                syncPredicate = QueryPredicateGroup.andOf(syncPredicate);
            }
            builder.variable("filter", filterType, parsePredicate(syncPredicate));
        }
        return builder.build();
    } catch (AmplifyException amplifyException) {
        throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
    }
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) AmplifyException(com.amplifyframework.AmplifyException) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) AppSyncGraphQLRequest(com.amplifyframework.api.aws.AppSyncGraphQLRequest) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicateGroup(com.amplifyframework.core.model.query.predicate.QueryPredicateGroup) NonNull(androidx.annotation.NonNull)

Aggregations

QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)2 QueryPredicateGroup (com.amplifyframework.core.model.query.predicate.QueryPredicateGroup)2 DataStoreException (com.amplifyframework.datastore.DataStoreException)2 NonNull (androidx.annotation.NonNull)1 AmplifyException (com.amplifyframework.AmplifyException)1 AppSyncGraphQLRequest (com.amplifyframework.api.aws.AppSyncGraphQLRequest)1 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)1 QueryPredicateOperation (com.amplifyframework.core.model.query.predicate.QueryPredicateOperation)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1