use of com.yahoo.elide.graphql.containers.ConnectionContainer 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());
}
use of com.yahoo.elide.graphql.containers.ConnectionContainer 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);
}
use of com.yahoo.elide.graphql.containers.ConnectionContainer 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;
}
use of com.yahoo.elide.graphql.containers.ConnectionContainer 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());
}
use of com.yahoo.elide.graphql.containers.ConnectionContainer in project elide by yahoo.
the class PersistentResourceFetcher method fetchRelationship.
/**
* Fetches a relationship for a top-level entity.
*
* @param parentResource Parent object
* @param relationship constructed relationship object with entityProjection
* @param ids List of ids
* @return persistence resource object(s)
*/
public static ConnectionContainer fetchRelationship(PersistentResource<?> parentResource, @NotNull Relationship relationship, Optional<List<String>> ids) {
EntityDictionary dictionary = parentResource.getRequestScope().getDictionary();
Type relationshipClass = dictionary.getParameterizedType(parentResource.getObject(), relationship.getName());
String relationshipType = dictionary.getJsonAliasFor(relationshipClass);
Set<PersistentResource> relationResources;
if (ids.isPresent()) {
relationResources = parentResource.getRelation(ids.get(), relationship).toList(LinkedHashSet::new).blockingGet();
} else {
relationResources = parentResource.getRelationCheckedFiltered(relationship).toList(LinkedHashSet::new).blockingGet();
}
return new ConnectionContainer(relationResources, Optional.ofNullable(relationship.getProjection().getPagination()), relationshipType);
}
Aggregations