Search in sources :

Example 26 with BadRequestException

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;
}
Also used : Entity(com.yahoo.elide.graphql.Entity) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Environment(com.yahoo.elide.graphql.Environment) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) HashMap(java.util.HashMap) Collection(java.util.Collection) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 27 with BadRequestException

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");
}
Also used : Attribute(com.yahoo.elide.core.request.Attribute) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) Type(com.yahoo.elide.core.type.Type) Relationship(com.yahoo.elide.core.request.Relationship) DeferredId(com.yahoo.elide.graphql.DeferredId) Collection(java.util.Collection) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Map(java.util.Map)

Example 28 with BadRequestException

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()));
}
Also used : KeyWord(com.yahoo.elide.graphql.KeyWord) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Pagination(com.yahoo.elide.core.request.Pagination) Getter(lombok.Getter) Environment(com.yahoo.elide.graphql.Environment) PersistentResource(com.yahoo.elide.core.PersistentResource) Optional(java.util.Optional) Collectors(java.util.stream.Collectors) Pagination(com.yahoo.elide.core.request.Pagination) PersistentResource(com.yahoo.elide.core.PersistentResource) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Aggregations

BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)28 PersistentResource (com.yahoo.elide.core.PersistentResource)8 Map (java.util.Map)8 RequestScope (com.yahoo.elide.core.RequestScope)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)7 List (java.util.List)7 EntityProjection (com.yahoo.elide.core.request.EntityProjection)5 Type (com.yahoo.elide.core.type.Type)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)4 ConnectionContainer (com.yahoo.elide.graphql.containers.ConnectionContainer)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Test (org.junit.jupiter.api.Test)4 Preconditions (com.google.common.base.Preconditions)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)3