list of self-references
kjstaring opened this issue · 3 comments
Hello, it might not be a real issue, but more of a question, but
here,
graphene-mongo/graphene_mongo/types.py
Line 15 in cfd6b0d
I have referenced the fields parents and children with manual references (a list of ids corresponding to the '_id' field)
In Models:
class CityObject(DynamicDocument):
_id = StringField()
type = StringField()
attributes = EmbeddedDocumentField(Attributes)
geometry = ListField(EmbeddedDocumentField(Geometry))
parents = ListField(ReferenceField('CityObject'))
children = ListField(ReferenceField('CityObject'))
meta = {'collection':'CityObjects'}
In Schema:
class CityObject(MongoengineObjectType):
class Meta:
model = CityObjectModel
With this code, the error is:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/graphene_mongo/types.py", line 205, in is_type_of raise Exception(('Received incompatible instance "{}".').format(root)) Exception: Received incompatible instance "DBRef('CityObjects', 'UUID_LOD2_011632-7908e457-f911-495e-9124')".
and when I replace 'CityObject' with 'self',
the error is:
"Cannot query field \"parents\" on type \"CityObject\"
@kjstaring : I can't reproduce your error, since following 2 cases both work:
- Relay
import graphene
import json
from mongoengine import *
from graphene_mongo import (
MongoengineObjectType,
MongoengineConnectionField
)
connect(
"graphene-mongo-test", host="mongomock://localhost", alias="default"
)
class CityObject(DynamicDocument):
# _id = StringField()
type = StringField()
# attributes = EmbeddedDocumentField(Attributes)
# geometry = ListField(EmbeddedDocumentField(Geometry))
parents = ListField(ReferenceField('CityObject'))
children = ListField(ReferenceField('CityObject'))
meta = {'collection':'CityObjects'}
CityObject.drop_collection()
c1 = CityObject(type='r_1')
c1.save()
c2 = CityObject(type='r_2')
c2.save()
c3 = CityObject(
type='r_3',
parents=[c1, c2],
children=[c1],
)
c3.save()
query = """
query Query {
cites {
edges {
node {
id
parents {
edges {
node {
type
}
}
}
children {
edges {
node {
type
}
}
}
}
}
}
}
"""
schema = graphene.Schema(query=Query)
result = schema.execute(query)
print(result.errors)
print(json.dumps(result.data, indent=2))
- Non-relay
import graphene
import json
from mongoengine import *
from graphene_mongo import (
MongoengineObjectType,
MongoengineConnectionField
)
connect(
"graphene-mongo-test", host="mongomock://localhost", alias="default"
)
class CityObject(DynamicDocument):
# _id = StringField()
type = StringField()
# attributes = EmbeddedDocumentField(Attributes)
# geometry = ListField(EmbeddedDocumentField(Geometry))
parents = ListField(ReferenceField('CityObject'))
children = ListField(ReferenceField('CityObject'))
meta = {'collection':'CityObjects'}
CityObject.drop_collection()
c1 = CityObject(type='r_1')
c1.save()
c2 = CityObject(type='r_2')
c2.save()
c3 = CityObject(
type='r_3',
parents=[c1, c2],
children=[c1],
)
c3.save()
class C(MongoengineObjectType):
class Meta:
model = CityObject
query = """
query Query {
cites {
id
parents {
type
}
children {
type
}
}
}
"""
schema = graphene.Schema(query=Query)
result = schema.execute(query)
print(result.errors)
print(json.dumps(result.data, indent=2))
Thank you! Yes, I think that the problem is how we stored the references in the collection, namely as a string in the '_id' field. We solved it by resolving them manually.
@kjstaring : Thanks. I will close this one, and feel free to re-open it if any further issue.