无法通过我的突变在 Hasura / GraphQL 中插入数组关系
Unable to insert array relationship in Hasura / GraphQL with my mutation
我正在使用 Apollo iOS 客户端连接到我们的 Hasura/postgress docker 环境。
我正在尝试创建一个对象并填充它的关系数组。但是,我在 switch result
语句中收到一条错误消息,告诉我:
variable subtasks of type [SubtaskReport_insert_input]! is used in position expecting SubtaskReport_arr_rel_insert_input
我尝试将类型从 SubtaskReport_insert_input
更改为 SubtaskReport_arr_rel_insert_input
,但是代码生成器没有生成 SubtaskReport_arr_rel_insert_input
类型。
如何创建这个数组以便成功执行突变?
我的变异graphql如下:
mutation addTaskReport($category: String!, $duration: Int!, $interval: Int!, $ppgs: String!, $status: String!, $taskDescription: String!, $task_id: uuid!, $zone_name: String!, $subtasks: [SubtaskReport_insert_input]!) {
insert_TaskReport(
objects: {
category: $category,
duration: $duration,
interval: $interval,
ppgs: $ppgs,
status: $status,
taskDescription: $taskDescription,
task_id: $task_id,
zone_name: $zone_name
subtasks: $subtasks
}) {
returning {
id
}
}
}
我用来构建和发送突变的 iOS 代码:
func addTaskReport(for task: Task) {
guard
let category = task.category,
let duration = task.duration,
let interval = task.interval,
let ppgsSet = task.ppgs,
let zoneName = task.zoneOfTask?.name,
let taskDescription = task.summary
else {
return
}
let taskReportId = UUID().uuidString.lowercased()
var subtaskReports = [SubtaskReport_arr_rel_insert_input]()
for subtask in task.subtasks as! Set<Subtask> {
let subtaskReportId = UUID().uuidString.lowercased()
let subtaskReport = SubtaskReport_insert_input(id: subtaskReportId, status: false, taskReportId: taskReportId, title: subtask.title)
subtaskReports.append(subtaskReport)
}
let ppgs = ppgsSet as Set<PersonalProtectionGear>
let ppgsStringArray = ppgs.compactMap({[=12=].name})
let ppgsString = Array(ppgsStringArray).joined(separator:" ")
self.apollo.perform(mutation: AddTaskReportMutation(
category: category,
duration: Int(duration),
interval: Int(interval),
ppgs: ppgsString,
status: "open",
taskDescription: taskDescription,
task_id: taskReportId,
zone_name: zoneName,
subtasks: subtaskReports)
) { (result) in
switch result {
case .success(let graphQLResult):
print("Simon Says graphQLResult \(graphQLResult)")//TODO: Remove
break
case .failure(let error):
print("Simon Says error \(error)")//TODO: Remove
break
}
print("Simon Says BreakHere")//TODO: Remove
}
}
在相关资源 (subtasks
) 前面添加 data
键应该适合您!另外,建议:也使用 [SubtaskReport_insert_input!]!
。这是 *_array_rel_input
.
的预期类型
更新查询如下:
+ mutation addTaskReport($category: String!, $duration: Int!, $interval: Int!, $ppgs: String!, $status: String!, $taskDescription: String!, $task_id: uuid!, $zone_name: String!, $subtasks: [SubtaskReport_insert_input!]!) {
insert_TaskReport(
objects: {
category: $category,
duration: $duration,
interval: $interval,
ppgs: $ppgs,
status: $status,
taskDescription: $taskDescription,
task_id: $task_id,
zone_name: $zone_name
+ subtasks: {
+ data: $subtasks
+ }
}) {
returning {
id
}
}
}
我绝对推荐使用 GraphiQL 界面来调试查询(相对于你的代码),因为它提供了 graphql 类型建议和检查,可以通过访问 http://localhost:9695/api-explorer or http://localhost:8080/api-explorer.
找到
我正在使用 Apollo iOS 客户端连接到我们的 Hasura/postgress docker 环境。
我正在尝试创建一个对象并填充它的关系数组。但是,我在 switch result
语句中收到一条错误消息,告诉我:
variable subtasks of type [SubtaskReport_insert_input]! is used in position expecting SubtaskReport_arr_rel_insert_input
我尝试将类型从 SubtaskReport_insert_input
更改为 SubtaskReport_arr_rel_insert_input
,但是代码生成器没有生成 SubtaskReport_arr_rel_insert_input
类型。
如何创建这个数组以便成功执行突变?
我的变异graphql如下:
mutation addTaskReport($category: String!, $duration: Int!, $interval: Int!, $ppgs: String!, $status: String!, $taskDescription: String!, $task_id: uuid!, $zone_name: String!, $subtasks: [SubtaskReport_insert_input]!) {
insert_TaskReport(
objects: {
category: $category,
duration: $duration,
interval: $interval,
ppgs: $ppgs,
status: $status,
taskDescription: $taskDescription,
task_id: $task_id,
zone_name: $zone_name
subtasks: $subtasks
}) {
returning {
id
}
}
}
我用来构建和发送突变的 iOS 代码:
func addTaskReport(for task: Task) {
guard
let category = task.category,
let duration = task.duration,
let interval = task.interval,
let ppgsSet = task.ppgs,
let zoneName = task.zoneOfTask?.name,
let taskDescription = task.summary
else {
return
}
let taskReportId = UUID().uuidString.lowercased()
var subtaskReports = [SubtaskReport_arr_rel_insert_input]()
for subtask in task.subtasks as! Set<Subtask> {
let subtaskReportId = UUID().uuidString.lowercased()
let subtaskReport = SubtaskReport_insert_input(id: subtaskReportId, status: false, taskReportId: taskReportId, title: subtask.title)
subtaskReports.append(subtaskReport)
}
let ppgs = ppgsSet as Set<PersonalProtectionGear>
let ppgsStringArray = ppgs.compactMap({[=12=].name})
let ppgsString = Array(ppgsStringArray).joined(separator:" ")
self.apollo.perform(mutation: AddTaskReportMutation(
category: category,
duration: Int(duration),
interval: Int(interval),
ppgs: ppgsString,
status: "open",
taskDescription: taskDescription,
task_id: taskReportId,
zone_name: zoneName,
subtasks: subtaskReports)
) { (result) in
switch result {
case .success(let graphQLResult):
print("Simon Says graphQLResult \(graphQLResult)")//TODO: Remove
break
case .failure(let error):
print("Simon Says error \(error)")//TODO: Remove
break
}
print("Simon Says BreakHere")//TODO: Remove
}
}
在相关资源 (subtasks
) 前面添加 data
键应该适合您!另外,建议:也使用 [SubtaskReport_insert_input!]!
。这是 *_array_rel_input
.
更新查询如下:
+ mutation addTaskReport($category: String!, $duration: Int!, $interval: Int!, $ppgs: String!, $status: String!, $taskDescription: String!, $task_id: uuid!, $zone_name: String!, $subtasks: [SubtaskReport_insert_input!]!) {
insert_TaskReport(
objects: {
category: $category,
duration: $duration,
interval: $interval,
ppgs: $ppgs,
status: $status,
taskDescription: $taskDescription,
task_id: $task_id,
zone_name: $zone_name
+ subtasks: {
+ data: $subtasks
+ }
}) {
returning {
id
}
}
}
我绝对推荐使用 GraphiQL 界面来调试查询(相对于你的代码),因为它提供了 graphql 类型建议和检查,可以通过访问 http://localhost:9695/api-explorer or http://localhost:8080/api-explorer.
找到