无法使用我的突变在 Hasura / GraphQL 中插入数组关系
Posted
技术标签:
【中文标题】无法使用我的突变在 Hasura / GraphQL 中插入数组关系【英文标题】:Unable to insert array relationship in Hasura / GraphQL with my mutation 【发布时间】:2020-07-13 10:29:33 【问题描述】:我正在使用 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($0.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
【问题讨论】:
【参考方案1】:在相关资源 (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
相关文档:https://hasura.io/docs/1.0/graphql/manual/mutations/insert.html#insert-an-object-along-with-its-related-objects-through-relationships
我绝对推荐使用 GraphiQL 界面来调试查询(与您的代码相比),因为它提供了 graphql 类型建议和检查,可以通过访问 http://localhost:9695/api-explorer 或 http://localhost:8080/api-explorer 找到。
【讨论】:
感谢您的帮助。像魅力一样工作。附:访问 api-explorer 给了我一个 ""path":"$","error":"resource does not exist","code":"not-found"" 警告.... 太棒了!嗯@TychoPandelaar 也许会提出一个新问题?可能与 Hasura 在本地运行的方式有关 - 也存在一些围绕该错误的 Github 问题。以上是关于无法使用我的突变在 Hasura / GraphQL 中插入数组关系的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 graphq-request 表示嵌套对象输入类型的数组
运行突变以存储新用户的帖子返回错误:变量“$data”类型的预期值