use of com.yahoo.elide.graphql.DeferredId 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");
}
Aggregations