Skip to content

Commit

Permalink
remove projection in entity
Browse files Browse the repository at this point in the history
  • Loading branch information
hchen04 committed Sep 25, 2019
1 parent 1c0f8ae commit 54cf3ab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 47 deletions.
31 changes: 14 additions & 17 deletions elide-graphql/src/main/java/com/yahoo/elide/graphql/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,22 @@ public class Entity {
@Getter private RequestScope requestScope;
@Getter private Set<Attribute> attributes;
@Getter private Set<Relationship> relationships;
@Getter private EntityProjection projection;

/**
* Class constructor.
* @param parentResource parent entity
* @param data entity data
* @param projection projection for this entity
* @param entityClass entity class
* @param requestScope the request context object
*/
public Entity(
Optional<Entity> parentResource,
Map<String, Object> data,
EntityProjection projection,
Class<?> entityClass,
RequestScope requestScope) {
this.parentResource = parentResource;
this.data = data;
this.projection = projection;
this.entityClass = projection.getType();
this.entityClass = entityClass;
this.requestScope = requestScope;
setAttributes();
setRelationships();
Expand Down Expand Up @@ -103,30 +101,21 @@ private void setRelationships() {
Class<?> relationshipClass =
dictionary.getParameterizedType(this.entityClass, relationshipName);

// if this data contains a relationship that is not in the projection tree, create a temporary
// projection for that relationship
EntityProjection relationshipProjection =
projection.getRelationship(relationshipName).isPresent()
? projection.getRelationship(relationshipName).get().getProjection()
: EntityProjection.builder()
.type(relationshipClass)
.build();

Set<Entity> relationshipEntities = new LinkedHashSet<>();

// if the relationship is ToOne, entry.getValue() should be a single map
if (dictionary.getRelationshipType(this.entityClass, relationshipName).isToOne()) {
relationshipEntities.add(new Entity(
Optional.of(this),
((Map<String, Object>) entry.getValue()),
relationshipProjection,
relationshipClass,
this.requestScope));
} else {
for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) {
relationshipEntities.add(new Entity(
Optional.of(this),
row,
relationshipProjection,
relationshipClass,
this.requestScope));
}
}
Expand Down Expand Up @@ -199,6 +188,14 @@ public void setId() {
public PersistentResource toPersistentResource() {
return this.data == null
? null
: PersistentResource.loadRecord(projection, getId().orElse(null), this.requestScope);
: PersistentResource.loadRecord(getProjection(), getId().orElse(null), this.requestScope);
}

/**
* Get a projection for this entity class. Used for querying inserted entities.
* @return {@link EntityProjection} object
*/
public EntityProjection getProjection() {
return EntityProjection.builder().type(entityClass).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
import com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException;
import com.yahoo.elide.core.exceptions.InvalidValueException;
import com.yahoo.elide.graphql.containers.ConnectionContainer;
import com.yahoo.elide.graphql.parser.GraphQLProjectionInfo;
import com.yahoo.elide.request.EntityProjection;
import com.yahoo.elide.request.Relationship;

import com.google.common.collect.Sets;

import graphql.language.Field;
import graphql.language.SourceLocation;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLType;
Expand Down Expand Up @@ -251,33 +249,16 @@ private ConnectionContainer upsertOrUpdateObjects(
context.field.getName());
}

// Try to get projection for the entity to upsert/update
SourceLocation selectionLocation = context.field.getSourceLocation();
GraphQLProjectionInfo projectionInfo = context.requestScope.getProjectionInfo();
EntityProjection entityProjection;
if (selectionLocation.equals(projectionInfo.getRootLocation())) {
// if this is the root selection
entityProjection = context.requestScope.getEntityProjection();
} else if (projectionInfo.getRelationshipMap().containsKey(selectionLocation)) {
// if this is a relationship in the projection
entityProjection = projectionInfo.getRelationshipMap().get(selectionLocation).getProjection();
} else {
// if there is not matched projection, create a new temporary projection
entityProjection = EntityProjection.builder()
.type(entityClass)
.build();
}

/* form entities */
Optional<Entity> parentEntity;
if (!context.isRoot()) {
parentEntity = Optional.of(new Entity(Optional.empty(), null, entityProjection, context.requestScope));
parentEntity = Optional.of(new Entity(Optional.empty(), null, entityClass, context.requestScope));
} else {
parentEntity = Optional.empty();
}
LinkedHashSet<Entity> entitySet = new LinkedHashSet<>();
for (Map<String, Object> input : context.data.get()) {
entitySet.add(new Entity(parentEntity, input, entityProjection, context.requestScope));
entitySet.add(new Entity(parentEntity, input, entityClass, context.requestScope));
}

/* apply function to upsert/update the object */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public GraphQLProjectionInfo make(String query) {
}
});

return new GraphQLProjectionInfo(rootLocation, rootProjections, relationshipMap);
return new GraphQLProjectionInfo(rootProjections, relationshipMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import com.yahoo.elide.request.EntityProjection;
import com.yahoo.elide.request.Relationship;

import graphql.language.SourceLocation;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collection;
import java.util.Map;

Expand All @@ -21,12 +21,7 @@
*/
@AllArgsConstructor
public class GraphQLProjectionInfo {
@Getter
private final SourceLocation rootLocation;

@Getter
private final Collection<EntityProjection> projections;
@Getter private final Collection<EntityProjection> projections;

@Getter
private final Map<SourceLocation, Relationship> relationshipMap;
@Getter private final Map<SourceLocation, Relationship> relationshipMap;
}

0 comments on commit 54cf3ab

Please sign in to comment.