Open sVerentsov opened 4 years ago
@sVerentsov : I will take a look, thanks.
I managed to solve this by explicitly stating posts
field in User
:
class User(MongoengineObjectType):
class Meta:
model = UserModel
posts = graphene.Field(graphene.List(lambda: Post))
class Post(graphene.Union):
class Meta:
types = (Image, Text)
Full working code here
It is quite suprising that graphene-mongo resolves posts
as references of ImageModel
and TextModel
automatically.
generic_reference = mongoengine.GenericReferenceField(
choices=[Article, Editor, ]
)
or you can use GenericReferenceField
with choices
to do it, but I have not verified ListField(mongoengine.GenericReferenceField())
works or not.
Here is the schema that i am trying to implement:
and execute this query:
What I have tried:
Mongoengine models: make PostModel as separate Document, inherit TextModel and ImageModel from it and reference PostModel from UserModel.
Graphene objects: make types for Text, Image, User and Post as MongoengineObjectType.
(full code here) This returns errors
Unknown type "Image".
andUnknown type "Text".
In this case Post type has only fieldsCls: String, id: ID
.Mongoengine models: same.
Graphene objects: inherit Post from graphene.Union and set Image and Text as types of that union. (full code here) This returns
Cannot query field "posts" on type "User".
. I suppose this happens because now graphene_mongo does not state thatUser.posts
referencePost
So how should such schema be implemented?