use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class MapEntryContainer method translateFromGraphQLMap.
/**
* Converts an attribute which is a list of maps - each containing a KEY and a VALUE
* into a HashMap with the value of KEY as key and the value of VALUE as the value.
* @param attribute The attribute to convert.
* @return The converted map.
*/
public static Map translateFromGraphQLMap(Entity.Attribute attribute) {
Map returnMap = new HashMap();
Object collection = attribute.getValue();
if (collection == null) {
return null;
}
if (!(collection instanceof Collection)) {
throw new BadRequestException("Invalid map format for GraphQL request");
}
((Collection) collection).stream().forEach((entry -> {
if (!(entry instanceof Map)) {
throw new BadRequestException("Invalid map format for GraphQL request");
}
if (!((Map) entry).containsKey(KEY) && ((Map) entry).containsKey(VALUE)) {
throw new BadRequestException("Invalid map format for GraphQL request");
}
returnMap.put(((Map) entry).get(KEY), ((Map) entry).get(VALUE));
}));
return returnMap;
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class NodeContainer method processFetch.
@Override
public Object processFetch(Environment context) {
EntityDictionary entityDictionary = context.requestScope.getDictionary();
NonEntityDictionary nonEntityDictionary = context.nonEntityDictionary;
Type parentClass = context.parentResource.getResourceType();
String fieldName = context.field.getName();
String idFieldName = entityDictionary.getIdFieldName(parentClass);
if (entityDictionary.isAttribute(parentClass, fieldName)) {
/* fetch attribute properties */
Attribute requested = context.requestScope.getProjectionInfo().getAttributeMap().getOrDefault(context.field.getSourceLocation(), null);
Object attribute = context.parentResource.getAttribute(requested);
if (attribute != null && nonEntityDictionary.hasBinding(EntityDictionary.getType(attribute))) {
return new NonEntityContainer(attribute);
}
if (attribute instanceof Map) {
return ((Map<Object, Object>) attribute).entrySet().stream().map(MapEntryContainer::new).collect(Collectors.toList());
}
if (attribute instanceof Collection) {
Type<?> innerType = entityDictionary.getParameterizedType(parentClass, fieldName);
if (nonEntityDictionary.hasBinding(innerType)) {
return ((Collection) attribute).stream().map(NonEntityContainer::new).collect(Collectors.toList());
}
}
return attribute;
}
if (entityDictionary.isRelation(parentClass, fieldName)) {
/* fetch relationship properties */
// get the relationship from constructed projections
Relationship relationship = context.requestScope.getProjectionInfo().getRelationshipMap().getOrDefault(context.field.getSourceLocation(), null);
if (relationship == null) {
throw new BadRequestException("Relationship doesn't have projection " + context.parentResource.getTypeName() + "." + fieldName);
}
return fetchRelationship(context, relationship);
}
if (Objects.equals(idFieldName, fieldName)) {
return new DeferredId(context.parentResource);
}
throw new BadRequestException("Unrecognized object: " + fieldName + " for: " + parentClass.getName() + " in node");
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class PageInfoContainer method processFetch.
@Override
public Object processFetch(Environment context) {
String fieldName = context.field.getName();
ConnectionContainer connectionContainer = getConnectionContainer();
Optional<Pagination> pagination = connectionContainer.getPagination();
List<String> ids = connectionContainer.getPersistentResources().stream().map(PersistentResource::getId).sorted().collect(Collectors.toList());
return pagination.map(pageValue -> {
switch(KeyWord.byName(fieldName)) {
case PAGE_INFO_HAS_NEXT_PAGE:
{
int numResults = ids.size();
int nextOffset = numResults + pageValue.getOffset();
return nextOffset < pageValue.getPageTotals();
}
case PAGE_INFO_START_CURSOR:
return pageValue.getOffset();
case PAGE_INFO_END_CURSOR:
return pageValue.getOffset() + ids.size();
case PAGE_INFO_TOTAL_RECORDS:
return pageValue.getPageTotals();
default:
break;
}
throw new BadRequestException("Invalid request. Looking for field: " + fieldName + " in an pageInfo object.");
}).orElseThrow(() -> new BadRequestException("Could not generate pagination information for type: " + connectionContainer.getTypeName()));
}
Aggregations