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));
}
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;
}
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());
}
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");
}
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;
}
Aggregations