Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested update #978

Merged
merged 2 commits into from
Sep 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* Invoked by GraphQL Java to fetch/mutate data from Elide.
*/
@Slf4j
public class PersistentResourceFetcher implements DataFetcher {
public class PersistentResourceFetcher implements DataFetcher<Object> {
private final ElideSettings settings;

public PersistentResourceFetcher(ElideSettings settings) {
Expand Down Expand Up @@ -166,7 +166,7 @@ private Object fetchObjects(Environment context) {
* @param generateTotals True if page totals should be generated for this type, false otherwise
* @return {@link PersistentResource} object(s)
*/
public ConnectionContainer fetchObject(Environment context, RequestScope requestScope, Class entityClass,
public ConnectionContainer fetchObject(Environment context, RequestScope requestScope, Class<?> entityClass,
Optional<List<String>> ids, Optional<String> sort,
Optional<String> offset, Optional<String> first,
Optional<String> filters, boolean generateTotals) {
Expand Down Expand Up @@ -207,7 +207,7 @@ public ConnectionContainer fetchObject(Environment context, RequestScope request
* @return persistence resource object(s)
*/
public Object fetchRelationship(Environment context,
PersistentResource parentResource,
PersistentResource<?> parentResource,
String fieldName,
Optional<List<String>> ids,
Optional<String> offset,
Expand All @@ -216,7 +216,7 @@ public Object fetchRelationship(Environment context,
Optional<String> filters,
boolean generateTotals) {
EntityDictionary dictionary = parentResource.getRequestScope().getDictionary();
Class entityClass = dictionary.getParameterizedType(parentResource.getObject(), fieldName);
Class<?> entityClass = dictionary.getParameterizedType(parentResource.getObject(), fieldName);
String typeName = dictionary.getJsonAliasFor(entityClass);

Optional<Pagination> pagination = buildPagination(first, offset, generateTotals);
Expand Down Expand Up @@ -255,7 +255,7 @@ private ConnectionContainer updateObjects(Environment context) {
* @return Connection object.
*/
private ConnectionContainer upsertOrUpdateObjects(Environment context,
Executor updateFunc,
Executor<?> updateFunc,
RelationshipOp operation) {
/* sanity check for id and data argument w UPSERT/UPDATE */
if (context.ids.isPresent()) {
Expand Down Expand Up @@ -298,10 +298,10 @@ private ConnectionContainer upsertOrUpdateObjects(Environment context,
/* fixup relationships */
for (Entity entity : entitySet) {
graphWalker(entity, this::updateRelationship);
PersistentResource childResource = entity.toPersistentResource();
if (!context.isRoot() && childResource.isNewlyCreated()) {
PersistentResource<?> childResource = entity.toPersistentResource();
if (!context.isRoot() && (childResource.isNewlyCreated() || context.parentResource.isNewlyCreated())) {
/* add relation between parent and nested entity */
context.parentResource.addRelation(context.field.getName(), entity.toPersistentResource());
context.parentResource.addRelation(context.field.getName(), childResource);
}
}

Expand All @@ -328,18 +328,17 @@ private interface Executor<T> {
* @param function Function to process nodes
* @return set of {@link PersistentResource} objects
*/
private void graphWalker(Entity entity, Executor function) {
Queue<Entity> toVisit = new ArrayDeque();
private void graphWalker(Entity entity, Executor<?> function) {
Queue<Entity> toVisit = new ArrayDeque<>();
Set<Entity> visited = new LinkedHashSet<>();
toVisit.add(entity);

while (!toVisit.isEmpty()) {
Entity currentEntity = toVisit.remove();
if (visited.contains(currentEntity)) {
continue;
} else {
visited.add(currentEntity);
}
visited.add(currentEntity);
function.execute(currentEntity);
Set<Entity.Relationship> relationshipEntities = currentEntity.getRelationships();
/* loop over relationships */
Expand All @@ -354,9 +353,9 @@ private void graphWalker(Entity entity, Executor function) {
* @param entity Resource entity
* @return {@link PersistentResource} object
*/
private PersistentResource updateRelationship(Entity entity) {
private PersistentResource<?> updateRelationship(Entity entity) {
Set<Entity.Relationship> relationshipEntities = entity.getRelationships();
PersistentResource resource = entity.toPersistentResource();
PersistentResource<?> resource = entity.toPersistentResource();
Set<PersistentResource> toUpdate;

/* loop over each relationship */
Expand All @@ -376,12 +375,12 @@ private PersistentResource updateRelationship(Entity entity) {
* @param entity Resource entity
* @return {@link PersistentResource} object
*/
private PersistentResource upsertObject(Environment context, Entity entity) {
private PersistentResource<?> upsertObject(Environment context, Entity entity) {
Set<Entity.Attribute> attributes = entity.getAttributes();
Optional<String> id = entity.getId();
RequestScope requestScope = entity.getRequestScope();
PersistentResource upsertedResource;
PersistentResource parentResource;
PersistentResource<?> upsertedResource;
PersistentResource<?> parentResource;
if (!entity.getParentResource().isPresent()) {
parentResource = null;
} else {
Expand Down Expand Up @@ -418,24 +417,23 @@ private PersistentResource upsertObject(Environment context, Entity entity) {
return updateAttributes(upsertedResource, entity, attributes);
}

private PersistentResource updateObject(Environment context, Entity entity) {
private PersistentResource<?> updateObject(Environment context, Entity entity) {
Set<Entity.Attribute> attributes = entity.getAttributes();
Optional<String> id = entity.getId();
RequestScope requestScope = entity.getRequestScope();
PersistentResource updatedResource;
PersistentResource<?> updatedResource;

if (!id.isPresent()) {
throw new BadRequestException("UPDATE data objects must include ids");
} else {
Set<PersistentResource> loadedResource = fetchObject(context, requestScope, entity.getEntityClass(),
Optional.of(Arrays.asList(id.get())),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
false).getPersistentResources();
updatedResource = loadedResource.iterator().next();
}
Set<PersistentResource> loadedResource = fetchObject(context, requestScope, entity.getEntityClass(),
Optional.of(Arrays.asList(id.get())),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
false).getPersistentResources();
updatedResource = loadedResource.iterator().next();

return updateAttributes(updatedResource, entity, attributes);
}
Expand All @@ -447,7 +445,7 @@ private PersistentResource updateObject(Environment context, Entity entity) {
* @param attributes Set of entity attributes
* @return Persistence Resource object
*/
private PersistentResource updateAttributes(PersistentResource toUpdate,
private PersistentResource<?> updateAttributes(PersistentResource<?> toUpdate,
Entity entity,
Set<Entity.Attribute> attributes) {
EntityDictionary dictionary = entity.getRequestScope().getDictionary();
Expand Down