Search in sources :

Example 6 with ConnectionContainer

use of com.yahoo.elide.graphql.containers.ConnectionContainer in project elide by yahoo.

the class PersistentResourceFetcher method upsertOrUpdateObjects.

/**
 * handle UPSERT or UPDATE operation.
 * @param context Environment encapsulating graphQL's request environment
 * @param updateFunc controls the behavior of how the update (or upsert) is performed.
 * @return Connection object.
 */
private ConnectionContainer upsertOrUpdateObjects(Environment context, Executor<?> updateFunc, RelationshipOp operation) {
    /* sanity check for id and data argument w UPSERT/UPDATE */
    if (context.ids.isPresent()) {
        throw new BadRequestException(operation + " must not include ids");
    }
    if (!context.data.isPresent()) {
        throw new BadRequestException(operation + " must include data argument");
    }
    Type<?> entityClass;
    EntityDictionary dictionary = context.requestScope.getDictionary();
    if (context.isRoot()) {
        entityClass = dictionary.getEntityClass(context.field.getName(), context.requestScope.getApiVersion());
    } else {
        assert context.parentResource != null;
        entityClass = dictionary.getParameterizedType(context.parentResource.getResourceType(), context.field.getName());
    }
    /* form entities */
    Optional<Entity> parentEntity;
    if (!context.isRoot()) {
        assert context.parentResource != null;
        parentEntity = Optional.of(new Entity(Optional.empty(), null, context.parentResource.getResourceType(), context.requestScope));
    } else {
        parentEntity = Optional.empty();
    }
    LinkedHashSet<Entity> entitySet = new LinkedHashSet<>();
    for (Map<String, Object> input : context.data.orElseThrow(IllegalStateException::new)) {
        entitySet.add(new Entity(parentEntity, input, entityClass, context.requestScope));
    }
    /* apply function to upsert/update the object */
    for (Entity entity : entitySet) {
        graphWalker(entity, updateFunc, context);
    }
    /* fixup relationships */
    for (Entity entity : entitySet) {
        graphWalker(entity, this::updateRelationship, context);
        PersistentResource<?> childResource = entity.toPersistentResource();
        if (!context.isRoot()) {
            /* add relation between parent and nested entity */
            assert context.parentResource != null;
            context.parentResource.addRelation(context.field.getName(), childResource);
        }
    }
    String entityName = dictionary.getJsonAliasFor(entityClass);
    Set<PersistentResource> resources = entitySet.stream().map(Entity::toPersistentResource).collect(Collectors.toCollection(LinkedHashSet::new));
    return new ConnectionContainer(resources, Optional.empty(), entityName);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)6 ConnectionContainer (com.yahoo.elide.graphql.containers.ConnectionContainer)6 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)5 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)3 LinkedHashSet (java.util.LinkedHashSet)3 ClassType (com.yahoo.elide.core.type.ClassType)2 Type (com.yahoo.elide.core.type.Type)2 Sets (com.google.common.collect.Sets)1 RequestScope (com.yahoo.elide.core.RequestScope)1 InvalidObjectIdentifierException (com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException)1 InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)1 EntityProjection (com.yahoo.elide.core.request.EntityProjection)1 Relationship (com.yahoo.elide.core.request.Relationship)1 ARGUMENT_OPERATION (com.yahoo.elide.graphql.ModelBuilder.ARGUMENT_OPERATION)1 FETCH (com.yahoo.elide.graphql.RelationshipOp.FETCH)1 GraphQLContainer (com.yahoo.elide.graphql.containers.GraphQLContainer)1 MapEntryContainer (com.yahoo.elide.graphql.containers.MapEntryContainer)1 OperationDefinition (graphql.language.OperationDefinition)1 DataFetcher (graphql.schema.DataFetcher)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1