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);
}
Aggregations