Skip to content

Commit

Permalink
WIP Fix graphql mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Nov 22, 2024
1 parent 03b374f commit 5a03889
Showing 1 changed file with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,26 +638,29 @@ mutation createAllPlanActivityDirectives($activities: [activity_directive_insert
return activityToDirectiveId;
}



private void modifyActivityDirectives(
final PlanId planId,
final List<SchedulingActivity> activities,
final Map<SchedulingActivity, GoalId> activityToGoalId
final List<SchedulingActivity> activities
)
throws IOException, NoSuchPlanException, MerlinServiceException
{
if (activities.isEmpty()) return;
ensurePlanExists(planId);
final var request = new StringBuilder();
request.append("mutation updatePlanActivityDirectives($activities: [activity_directive_set_input!]!) {");
var index = 0;
final var activitiesObject = Json.createArrayBuilder();
request.append("mutation updatePlanActivityDirectives(");
request.append(String.join(
",",
activities.stream().map($ -> "$activity_%d: activity_directive_set_input!".formatted($.id().id())).toList()
));
request.append(") {");
final var arguments = Json.createObjectBuilder();
for (final var act : activities) {
final var id = act.id().id();
request.append("""
update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: $activities[%d]) {}
""".formatted(act.id().id(), act.id().id(), planId.id(), index));
index++;
update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: $activity_%d) {
affected_rows
}
""".formatted(id, id, planId.id(), id));

final var activityObject = Json
.createObjectBuilder()
Expand All @@ -670,13 +673,9 @@ private void modifyActivityDirectives(
insertionObjectArguments.add(arg.getKey(), serializedValueP.unparse(arg.getValue()));
}
activityObject.add("arguments", insertionObjectArguments.build());
activitiesObject.add(activityObject);
arguments.add("activity_%d".formatted(id), activityObject);
}
final var arguments = Json
.createObjectBuilder()
.add("activities", activitiesObject.build())
.build();
postRequest(request.toString(), arguments).orElseThrow(() -> new NoSuchPlanException(planId));
postRequest(request.toString(), arguments.build()).orElseThrow(() -> new NoSuchPlanException(planId));
}

private void deleteActivityDirectives(
Expand All @@ -691,9 +690,10 @@ private void deleteActivityDirectives(
request.append("mutation deletePlanActivityDirectives {");
for (final var id : ids) {
request.append("""
delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {}
delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {affected_rows}
""".formatted(id.id(), id.id(), planId.id()));
}
request.append("}");
postRequest(request.toString()).orElseThrow(() -> new NoSuchPlanException(planId));
}

Expand Down

0 comments on commit 5a03889

Please sign in to comment.