我正在寻找一种方法来访问graphql @SchemaMapping中使用的源对象的源对象。
record Children() {}
record Parent(List<Children> childrens, String surname) {}@SchemaMapping
String surname(Children children) {
return "" // should access parent.surname()
}type Parent {
children: [Children]
surname: String
}
type Children {
surname: String
}发布于 2022-11-07 14:38:02
首先,您应该用您在模式中声明的字段(最初是姓字符串)更新您的子女记录:
然后,我建议您提供一种方法,将链接到与姓氏不同的父母(因为我从您的要求中认为,这不是一种选择):例如,parentId。
record Children(Long, parentId, String surname) {}
record Parent(Long id, List<Children> childrens, String surname) {}
type Parent {
id: ID
children: [Children]
surname: String
}
type Children {
parentId: ID
surname: String
}然后,可以使用筛选器或任何业务方法实现模式映射:
@SchemaMapping
String surname(Children children)
return db.stream()
.filter(aParent -> aParent.id()==children.parentId())
.findAny()
.orElseGet(()->...) //etc
.surname();
}https://stackoverflow.com/questions/74345441
复制相似问题