Skip to content

Commit

Permalink
Fix graphql mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Nov 22, 2024
1 parent 344d121 commit 8265777
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,20 @@ private void modifyActivityDirectives(
{
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 @@ -669,13 +675,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 @@ -689,9 +691,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 8265777

Please sign in to comment.