Search in sources :

Example 1 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class PersistentResourceFetcher method removeObjects.

/**
 * Removes a relationship, or deletes a root level resource.
 * @param context Environment encapsulating graphQL's request environment
 * @return set of removed {@link PersistentResource} object(s)
 */
private ConnectionContainer removeObjects(Environment context) {
    /* sanity check for id and data argument w REPLACE */
    if (context.data.isPresent()) {
        throw new BadRequestException("REPLACE must not include data argument");
    }
    if (!context.ids.isPresent()) {
        throw new BadRequestException("REPLACE must include ids argument");
    }
    ConnectionContainer connection = (ConnectionContainer) fetchObjects(context);
    Set<PersistentResource> toRemove = connection.getPersistentResources();
    if (!context.isRoot()) {
        /* has parent */
        toRemove.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item));
    } else {
        /* is root */
        toRemove.forEach(PersistentResource::deleteResource);
    }
    return new ConnectionContainer(Collections.emptySet(), Optional.empty(), connection.getTypeName());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 2 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class PersistentResourceFetcher method fetchObject.

/**
 * Fetches a root-level entity.
 * @param requestScope Request scope
 * @param projection constructed entityProjection for a class
 * @param ids List of ids (can be NULL)
 * @return {@link PersistentResource} object(s)
 */
public static ConnectionContainer fetchObject(RequestScope requestScope, EntityProjection projection, Optional<List<String>> ids) {
    EntityDictionary dictionary = requestScope.getDictionary();
    String typeName = dictionary.getJsonAliasFor(projection.getType());
    /* fetching a collection */
    Observable<PersistentResource> records = ids.map((idList) -> {
        /* handle empty list of ids */
        if (idList.isEmpty()) {
            throw new BadRequestException("Empty list passed to ids");
        }
        return PersistentResource.loadRecords(projection, idList, requestScope);
    }).orElseGet(() -> PersistentResource.loadRecords(projection, new ArrayList<>(), requestScope));
    return new ConnectionContainer(records.toList(LinkedHashSet::new).blockingGet(), Optional.ofNullable(projection.getPagination()), typeName);
}
Also used : MapEntryContainer(com.yahoo.elide.graphql.containers.MapEntryContainer) OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) ClassType(com.yahoo.elide.core.type.ClassType) ArrayList(java.util.ArrayList) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataFetcher(graphql.schema.DataFetcher) Relationship(com.yahoo.elide.core.request.Relationship) Observable(io.reactivex.Observable) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) ARGUMENT_OPERATION(com.yahoo.elide.graphql.ModelBuilder.ARGUMENT_OPERATION) FETCH(com.yahoo.elide.graphql.RelationshipOp.FETCH) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Type(com.yahoo.elide.core.type.Type) Optional(java.util.Optional) GraphQLContainer(com.yahoo.elide.graphql.containers.GraphQLContainer) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) ArrayList(java.util.ArrayList) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 3 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class PersistentResourceFetcher method replaceObjects.

/**
 * Replaces a resource, updates given resource and deletes the rest
 * belonging to the the same type/relationship family.
 * @param context Environment encapsulating graphQL's request environment
 * @return set of replaced {@link PersistentResource} object(s)
 */
private ConnectionContainer replaceObjects(Environment context) {
    /* sanity check for id and data argument w REPLACE */
    if (!context.data.isPresent()) {
        throw new BadRequestException("REPLACE must include data argument");
    }
    if (context.ids.isPresent()) {
        throw new BadRequestException("REPLACE must not include ids argument");
    }
    ConnectionContainer existingObjects = (ConnectionContainer) context.container.processFetch(context);
    ConnectionContainer upsertedObjects = upsertObjects(context);
    Set<PersistentResource> toDelete = Sets.difference(existingObjects.getPersistentResources(), upsertedObjects.getPersistentResources());
    if (!context.isRoot()) {
        /* has parent */
        toDelete.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item));
    } else {
        /* is root */
        toDelete.forEach(PersistentResource::deleteResource);
    }
    return upsertedObjects;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 4 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class PersistentResourceFetcher method deleteObjects.

/**
 * Deletes a resource.
 * @param context Environment encapsulating graphQL's request environment
 * @return set of deleted {@link PersistentResource} object(s)
 */
private ConnectionContainer deleteObjects(Environment context) {
    /* sanity check for id and data argument w DELETE */
    if (context.data.isPresent()) {
        throw new BadRequestException("DELETE must not include data argument");
    }
    if (!context.ids.isPresent()) {
        throw new BadRequestException("DELETE must include ids argument");
    }
    ConnectionContainer connection = (ConnectionContainer) fetchObjects(context);
    Set<PersistentResource> toDelete = connection.getPersistentResources();
    toDelete.forEach(PersistentResource::deleteResource);
    return new ConnectionContainer(Collections.emptySet(), Optional.empty(), connection.getTypeName());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 5 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class PersistentResourceFetcher method get.

/**
 * Override graphql-java's {@link DataFetcher} get method to execute
 * the mutation and return some sensible output values.
 * @param environment Graphql-java's execution environment
 * @return a collection of {@link PersistentResource} objects
 */
@Override
public Object get(DataFetchingEnvironment environment) {
    /* fetch arguments in mutation/query */
    Map<String, Object> args = environment.getArguments();
    /* fetch current operation */
    RelationshipOp operation = (RelationshipOp) args.getOrDefault(ARGUMENT_OPERATION, FETCH);
    /* build environment object, extracts required fields */
    Environment context = new Environment(environment, nonEntityDictionary);
    /* safe enable debugging */
    if (log.isDebugEnabled()) {
        logContext(log, operation, context);
    }
    if (operation != FETCH) {
        /* Don't allow write operations in a non-mutation request. */
        if (environment.getOperationDefinition().getOperation() != OperationDefinition.Operation.MUTATION) {
            throw new BadRequestException("Data model writes are only allowed in mutations");
        }
        /* sanity check for pagination/filtering/sorting arguments w any operation other than FETCH */
        filterSortPaginateSanityCheck(context);
    }
    GraphQLContainer container;
    /* delegate request */
    switch(operation) {
        case FETCH:
            {
                return fetchObjects(context);
            }
        case UPSERT:
            {
                container = upsertObjects(context);
                break;
            }
        case UPDATE:
            {
                container = updateObjects(context);
                break;
            }
        case DELETE:
            {
                container = deleteObjects(context);
                break;
            }
        case REMOVE:
            {
                container = removeObjects(context);
                break;
            }
        case REPLACE:
            {
                container = replaceObjects(context);
                break;
            }
        default:
            throw new UnsupportedOperationException("Unknown operation: " + operation);
    }
    if (operation != FETCH) {
        context.requestScope.runQueuedPreSecurityTriggers();
        context.requestScope.runQueuedPreFlushTriggers();
    }
    return container;
}
Also used : GraphQLContainer(com.yahoo.elide.graphql.containers.GraphQLContainer) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Aggregations

BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)28 PersistentResource (com.yahoo.elide.core.PersistentResource)8 Map (java.util.Map)8 RequestScope (com.yahoo.elide.core.RequestScope)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)7 List (java.util.List)7 EntityProjection (com.yahoo.elide.core.request.EntityProjection)5 Type (com.yahoo.elide.core.type.Type)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)4 ConnectionContainer (com.yahoo.elide.graphql.containers.ConnectionContainer)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Test (org.junit.jupiter.api.Test)4 Preconditions (com.google.common.base.Preconditions)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)3