Search in sources :

Example 21 with Attribute

use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.

the class CSVExportFormatterTest method testHeaderWithNonmatchingAlias.

@Test
public void testHeaderWithNonmatchingAlias() {
    CSVExportFormatter formatter = new CSVExportFormatter(elide, false);
    TableExport queryObj = new TableExport();
    String query = "{ tableExport { edges { node { query queryType } } } }";
    String id = "edc4a871-dff2-4054-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
    queryObj.setResultType(ResultType.CSV);
    // Prepare EntityProjection
    Set<Attribute> attributes = new LinkedHashSet<>();
    attributes.add(Attribute.builder().type(TableExport.class).name("query").alias("foo").build());
    attributes.add(Attribute.builder().type(TableExport.class).name("queryType").build());
    EntityProjection projection = EntityProjection.builder().type(TableExport.class).attributes(attributes).build();
    String output = formatter.preFormat(projection, queryObj);
    assertEquals("\"query\",\"queryType\"", output);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TableExport(com.yahoo.elide.async.models.TableExport) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Attribute(com.yahoo.elide.core.request.Attribute) Test(org.junit.jupiter.api.Test)

Example 22 with Attribute

use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.

the class CSVExportFormatterTest method testHeaderSkip.

@Test
public void testHeaderSkip() {
    CSVExportFormatter formatter = new CSVExportFormatter(elide, true);
    TableExport queryObj = new TableExport();
    String query = "{ tableExport { edges { node { query queryType } } } }";
    String id = "edc4a871-dff2-4054-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
    queryObj.setResultType(ResultType.CSV);
    // Prepare EntityProjection
    Set<Attribute> attributes = new LinkedHashSet<>();
    attributes.add(Attribute.builder().type(TableExport.class).name("query").alias("query").build());
    attributes.add(Attribute.builder().type(TableExport.class).name("queryType").build());
    EntityProjection projection = EntityProjection.builder().type(TableExport.class).attributes(attributes).build();
    String output = formatter.preFormat(projection, queryObj);
    assertNull(output);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TableExport(com.yahoo.elide.async.models.TableExport) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Attribute(com.yahoo.elide.core.request.Attribute) Test(org.junit.jupiter.api.Test)

Example 23 with Attribute

use of com.yahoo.elide.core.request.Attribute in project elide by yahoo.

the class GraphQLEntityProjectionMaker method addAttributeField.

/**
 * Add an attribute to an entity projection.
 *
 * @param attributeField graphQL field for an attribute
 * @param projectionBuilder projection that is being built
 */
private void addAttributeField(Field attributeField, EntityProjectionBuilder projectionBuilder) {
    Type<?> parentType = projectionBuilder.getType();
    String attributeName = attributeField.getName();
    String attributeAlias = attributeField.getAlias() == null ? attributeName : attributeField.getAlias();
    Type<?> attributeType = entityDictionary.getType(parentType, attributeName);
    if (attributeType != null) {
        Attribute attribute = Attribute.builder().type(attributeType).name(attributeName).alias(attributeAlias).arguments(getArguments(attributeField, entityDictionary.getAttributeArguments(parentType, attributeName))).build();
        projectionBuilder.attribute(attribute);
        attributeMap.put(attributeField.getSourceLocation(), attribute);
    } else {
        throw new InvalidEntityBodyException(String.format("Unknown attribute field {%s.%s}.", entityDictionary.getJsonAliasFor(projectionBuilder.getType()), attributeName));
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) Attribute(com.yahoo.elide.core.request.Attribute)

Example 24 with Attribute

use of com.yahoo.elide.core.request.Attribute 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)

Aggregations

Attribute (com.yahoo.elide.core.request.Attribute)24 Test (org.junit.jupiter.api.Test)19 EntityProjection (com.yahoo.elide.core.request.EntityProjection)11 TableExport (com.yahoo.elide.async.models.TableExport)9 LinkedHashSet (java.util.LinkedHashSet)8 LinkedHashMap (java.util.LinkedHashMap)5 Argument (com.yahoo.elide.core.request.Argument)4 PersistentResource (com.yahoo.elide.core.PersistentResource)3 Resource (com.yahoo.elide.jsonapi.models.Resource)3 PersistentResourceFetcherTest (com.yahoo.elide.graphql.PersistentResourceFetcherTest)2 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)2 Parent (example.Parent)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Path (com.yahoo.elide.core.Path)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 InvalidParameterizedAttributeException (com.yahoo.elide.core.exceptions.InvalidParameterizedAttributeException)1