I'm trying to create a simple application (to create a graphql api using django and python). It's going well but I'm stuck with 1 problem and i have no idea how to go about it (I've been searching for days, but I simply give up) so I was hoping someone here could assist (see below what I have).
I'm using Django with graphene-django and neo4j as (graph)database. Also im using the relay schema.
With graphql I can query all the properties of the products and its also possible to query over the relations (which product belongs to which category and vice versa).
Now my problem is the following: In neo4j relations also have their own properties but I have no idea how to get these properties (for example I want to see when the relation was created, this is a property on the relation).
I currently have 2 node types: Category and Product See models(models.py) below:
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')
Below is the graphene-django code that i use(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, )
What i was thinking is that maybe i can try to create an extra field and write a resolver that performs a raw query to get the relationships and properties (like below)
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
Now to perform the relationship i need off course the 2 id's to make the query like this: match(n:Category)-[r]->(x:Product) where id(n) = 2358 AND id(x) = 187 return n,x,r so i can get the relationproperties of r.
My problem is i cant get the id of connected node (only the root.id).Is there a way to get the parent id? Or is my entire way of thinking wrong and there is a function (of which i dont know off, mayb in graphene?) that can do what i want in a different (hopefully better) way?
Thanks for the help.