What is intended way to implement Union?
Here is the schema that i am trying to implement:
type Text {
id: ID
content: String
}
type Image {
id: ID
image_url: String
thumb_url: String
}
union Post = Text | Image
type User {
id: ID
posts: [Post]
}
type Query {
users: [User]
}
and execute this query:
{
users {
posts {
... on Image {
image_url
}
... on Text {
content
}
}
}
}
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 errorsUnknown 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 returnsCannot query field "posts" on type "User".. I suppose this happens because now graphene_mongo does not state thatUser.postsreferencePost
So how should such schema be implemented?
@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.
https://github.com/graphql-python/graphene-mongo/blob/33be6067a2c451428064d9e4338269dd50744715/graphene_mongo/tests/models.py#L61-L74
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.