我有这样的查询:
query {
organizations {
id
name
itemA {
fieldA
fieldB
}
}
}返回
"data": {
"organizations": [
{
"id": 123,
"name": "first org",
"itemA": {
"fieldA": "some value A",
"fieldB": "other value B",
}
},
{
"id": 321,
"name": "other org",
"itemA": {
"fieldA": "other value A",
"fieldB": "value B",
}
}
]
}一个用户可以访问多个组织,但对每个组织具有不同的访问权限。
当解析fieldA和fieldB以验证访问时,我需要有organization.id。
我尝试在字段的解析器中使用context.merge_scoped!(organiozation_id: org.id),它返回单个组织。看起来它做了我需要的事情,子字段在context中收到了正确的值,但我不确定。一般情况下,没有该方法和scoped_context的文档。
另外,如果scoped_context是我需要的,我如何为项目列表设置它?
更新:示例查询
query {
organizations { // I need to pass item of this list to resolver of ItemA
someModel{
otherModel {
itemA // access depened on organization`
}
}
}
}发布于 2021-08-07 11:32:52
我不是100%确定我理解了你的问题,但我认为你需要的东西对GQL来说应该是“透明的”。
你需要两件事才能正确地列出一个组织的“项目”:1.哪个组织和2.谁在问:
# type organization
def itemA
object # this is 1, the organization, current node in the graph
.get_fields_accessible_by(context[:who_is_asking]) # this is requirement 2
end正如你所看到的,似乎根本没有理由操纵上下文。(除非我完全没有抓住要点,所以请随时修改您的问题以澄清)。
发布于 2021-10-08 20:44:06
你能不能试试:
context[:organisation] = object #set this value in your organisation model使用current[:organisation]在另一个模型中访问它
此外,还可以创建帮助器方法
就像这样
def current_organisation
context[:current_organisation]
end谢谢
https://stackoverflow.com/questions/68483323
复制相似问题