【发布时间】:2020-12-07 16:45:51
【问题描述】:
我正在尝试创建一个简单的应用程序(使用 django 和 python 创建一个 graphql api)。进展顺利,但我遇到了 1 个问题,我不知道如何解决(我一直在寻找几天,但我只是放弃了)所以我希望这里有人可以提供帮助(见下文我有)。
我将 Django 与 graphene-django 和 neo4j 一起用作(图)数据库。我也在使用中继模式。
使用graphql,我可以查询产品的所有属性,也可以查询关系(哪个产品属于哪个类别,反之亦然)。
现在我的问题如下:在 neo4j 中,关系也有自己的属性,但我不知道如何获取这些属性(例如,我想查看关系的创建时间,这是关系上的属性)。
我目前有 2 种节点类型:类别和产品请参阅下面的模型(models.py):
class Category(StructuredNode):
name = StringProperty()
shortname = StringProperty()
product = Relationship('Product','IN_CATEGORY')
class Product(StructuredNode):
name = StringProperty()
price = StringProperty()
description = StringProperty()
category = Relationship('Category','IN_CATEGORY')
下面是我使用的graphene-django代码(schema.py):
class CategoryType(DjangoObjectType):
class Meta:
model = Category
neomodel_filter_fields = {
'name': ['exact', 'icontains'],
'shortname': ['exact'],
}
interfaces = (relay.Node, )
class ProductType(DjangoObjectType):
class Meta:
model = Product
neomodel_filter_fields = {
'name': ['exact', 'icontains'],
'price': ['exact'],
}
interfaces = (relay.Node, )
我的想法是,也许我可以尝试创建一个额外的字段并编写一个解析器来执行原始查询以获取关系和属性(如下所示)
class ProductType(DjangoObjectType):
relationship = graphene.String()
class Meta:
model = Product
neomodel_filter_fields = {
'name': ['exact', 'icontains'],
'price': ['exact'],
}
interfaces = (relay.Node, )
def resolve_relationship(root,info, args):
#perform the raw query here
现在要执行关系,我需要偏离 2 个 id 来进行这样的查询: match(n:Category)-[r]->(x:Product) 其中 id(n) = 2358 AND id(x) = 187 返回 n,x,r 所以我可以得到r的关系属性。
我的问题是我无法获取连接节点的 ID(只有 root.id)。有没有办法获取父 ID?还是我的整个思维方式都是错误的,并且有一个功能(我不知道,石墨烯中的可能?)可以以不同(希望更好)的方式做我想做的事?
感谢您的帮助。
【问题讨论】:
标签: django neo4j graphql graphene-python graphene-django