Search in sources :

Example 1 with NonEntityDictionary

use of com.yahoo.elide.graphql.NonEntityDictionary in project elide by yahoo.

the class SubscriptionModelBuilderTest method testRootType.

@Test
public void testRootType() {
    DataFetcher fetcher = mock(DataFetcher.class);
    SubscriptionModelBuilder builder = new SubscriptionModelBuilder(dictionary, new NonEntityDictionary(DefaultClassScanner.getInstance(), CoerceUtil::lookup), fetcher, NO_VERSION);
    GraphQLSchema schema = builder.build();
    GraphQLObjectType subscriptionType = (GraphQLObjectType) schema.getType("Subscription");
    // Book Type
    GraphQLObjectType bookType = (GraphQLObjectType) schema.getType(TYPE_BOOK);
    GraphQLFieldDefinition bookField = subscriptionType.getFieldDefinition(BOOK);
    GraphQLEnumType bookTopicType = (GraphQLEnumType) bookField.getArgument(TOPIC).getType();
    assertEquals("BookTopic", bookTopicType.getName());
    assertNotNull(bookTopicType.getValue("ADDED"));
    assertNotNull(bookTopicType.getValue("UPDATED"));
    assertNull(bookTopicType.getValue("DELETED"));
    assertEquals(bookType, subscriptionType.getFieldDefinition(BOOK).getType());
    assertNotNull(subscriptionType.getFieldDefinition(BOOK).getArgument(FILTER));
    // Author Type
    GraphQLObjectType authorType = (GraphQLObjectType) schema.getType(TYPE_AUTHOR);
    assertEquals(authorType, subscriptionType.getFieldDefinition(AUTHOR).getType());
    assertNotNull(subscriptionType.getFieldDefinition(AUTHOR).getArgument(FILTER));
    GraphQLFieldDefinition authorField = subscriptionType.getFieldDefinition(AUTHOR);
    GraphQLEnumType authorTopicType = (GraphQLEnumType) authorField.getArgument(TOPIC).getType();
    assertEquals("AuthorTopic", authorTopicType.getName());
    assertNotNull(authorTopicType.getValue("ADDED"));
    assertNotNull(authorTopicType.getValue("UPDATED"));
    assertNotNull(authorTopicType.getValue("DELETED"));
    // Publisher Type
    assertNull(subscriptionType.getFieldDefinition("publisher"));
    // Preview Type (Custom Subscription)
    GraphQLObjectType previewType = (GraphQLObjectType) schema.getType(TYPE_PREVIEW);
    GraphQLFieldDefinition previewField = subscriptionType.getFieldDefinition(PREVIEW);
    assertEquals(previewType, previewField.getType());
    assertNull(previewField.getArgument(TOPIC));
}
Also used : GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) Test(org.junit.jupiter.api.Test)

Example 2 with NonEntityDictionary

use of com.yahoo.elide.graphql.NonEntityDictionary in project elide by yahoo.

the class MapEntryContainer method processFetch.

@Override
public Object processFetch(Environment context) {
    NonEntityDictionary nonEntityDictionary = context.nonEntityDictionary;
    String fieldName = context.field.getName();
    Object returnObject;
    if (KEY.equalsIgnoreCase(fieldName)) {
        returnObject = entry.getKey();
    } else if (VALUE.equalsIgnoreCase(fieldName)) {
        returnObject = entry.getValue();
    } else {
        throw new BadRequestException("Invalid field: '" + fieldName + "'. Maps only contain fields 'key' and 'value'");
    }
    if (nonEntityDictionary.hasBinding(EntityDictionary.getType(returnObject))) {
        return new NonEntityContainer(returnObject);
    }
    return returnObject;
}
Also used : BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary)

Example 3 with NonEntityDictionary

use of com.yahoo.elide.graphql.NonEntityDictionary in project elide by yahoo.

the class SubscriptionModelBuilderTest method testModelTypes.

@Test
public void testModelTypes() {
    DataFetcher fetcher = mock(DataFetcher.class);
    SubscriptionModelBuilder builder = new SubscriptionModelBuilder(dictionary, new NonEntityDictionary(DefaultClassScanner.getInstance(), CoerceUtil::lookup), fetcher, NO_VERSION);
    GraphQLSchema schema = builder.build();
    GraphQLObjectType bookType = (GraphQLObjectType) schema.getType(TYPE_BOOK);
    GraphQLObjectType authorType = (GraphQLObjectType) schema.getType(TYPE_AUTHOR);
    GraphQLObjectType previewType = (GraphQLObjectType) schema.getType(TYPE_PREVIEW);
    GraphQLObjectType publisherType = (GraphQLObjectType) schema.getType("publisher");
    assertNotNull(bookType);
    assertNotNull(authorType);
    assertNotNull(previewType);
    assertNull(publisherType);
    assertNotNull(schema.getType(SUBSCRIPTION));
    // Test root type description fields.
    assertEquals("A GraphQL Book", bookType.getDescription());
    assertNull(authorType.getDescription());
    // Verify Book Fields
    assertEquals(Scalars.GraphQLString, bookType.getFieldDefinition(FIELD_TITLE).getType());
    assertEquals(Scalars.GraphQLString, bookType.getFieldDefinition(FIELD_GENRE).getType());
    assertEquals(GraphQLScalars.GRAPHQL_DEFERRED_ID, previewType.getFieldDefinition(FIELD_ID).getType());
    assertEquals(previewType, ((GraphQLList) bookType.getFieldDefinition("previews").getType()).getWrappedType());
    assertEquals(authorType, ((GraphQLList) bookType.getFieldDefinition(FIELD_AUTHORS).getType()).getWrappedType());
    assertEquals(5, bookType.getFieldDefinitions().size());
    // Verify fields without SubscriptionField are missing.
    assertNull(bookType.getFieldDefinition(FIELD_LANGUAGE));
    assertNull(bookType.getFieldDefinition(FIELD_PUBLISH_DATE));
    assertNull(bookType.getFieldDefinition("publisher"));
    // Verify Author Fields
    assertEquals(GraphQLScalars.GRAPHQL_DEFERRED_ID, authorType.getFieldDefinition(FIELD_ID).getType());
    GraphQLObjectType addressType = (GraphQLObjectType) authorType.getFieldDefinition("homeAddress").getType();
    assertEquals(Scalars.GraphQLString, addressType.getFieldDefinition("street1").getType());
    assertEquals(Scalars.GraphQLString, addressType.getFieldDefinition("street2").getType());
    assertEquals(Scalars.GraphQLString, authorType.getFieldDefinition(FIELD_NAME).getType());
    assertTrue(validateEnum(Author.AuthorType.class, (GraphQLEnumType) authorType.getFieldDefinition(FIELD_TYPE).getType()));
    assertEquals(4, authorType.getFieldDefinitions().size());
    // Verify fields without SubscriptionField are missing.
    assertNull(bookType.getFieldDefinition("books"));
    // Verify Preview Fields
    assertEquals(GraphQLScalars.GRAPHQL_DEFERRED_ID, previewType.getFieldDefinition(FIELD_ID).getType());
    assertEquals(2, previewType.getFieldDefinitions().size());
}
Also used : GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLObjectType(graphql.schema.GraphQLObjectType) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) Test(org.junit.jupiter.api.Test)

Example 4 with NonEntityDictionary

use of com.yahoo.elide.graphql.NonEntityDictionary 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 5 with NonEntityDictionary

use of com.yahoo.elide.graphql.NonEntityDictionary in project elide by yahoo.

the class NonEntityContainer method processFetch.

@Override
public Object processFetch(Environment context) {
    NonEntityDictionary nonEntityDictionary = context.nonEntityDictionary;
    String fieldName = context.field.getName();
    // There is no Elide security for models not managed by Elide.
    Object object = nonEntityDictionary.getValue(nonEntity, fieldName, context.requestScope);
    if (object == null) {
        return null;
    }
    if (nonEntityDictionary.hasBinding(EntityDictionary.getType(object))) {
        return new NonEntityContainer(object);
    }
    if (object instanceof Map) {
        return ((Map<Object, Object>) object).entrySet().stream().map(MapEntryContainer::new).collect(Collectors.toList());
    }
    if (object instanceof Collection) {
        Type<?> innerType = nonEntityDictionary.getParameterizedType(nonEntity.getClass(), fieldName);
        if (nonEntityDictionary.hasBinding(innerType)) {
            return ((Collection) object).stream().map(NonEntityContainer::new).collect(Collectors.toList());
        }
    }
    return object;
}
Also used : Collection(java.util.Collection) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) Map(java.util.Map)

Aggregations

NonEntityDictionary (com.yahoo.elide.graphql.NonEntityDictionary)5 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)2 DataFetcher (graphql.schema.DataFetcher)2 GraphQLEnumType (graphql.schema.GraphQLEnumType)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 Attribute (com.yahoo.elide.core.request.Attribute)1 Relationship (com.yahoo.elide.core.request.Relationship)1 Type (com.yahoo.elide.core.type.Type)1 DeferredId (com.yahoo.elide.graphql.DeferredId)1 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)1