Not able to request EmbeddedDocumentField in query
Closed this issue ยท 3 comments
Hi,
I have the following models:
class ProfessorMetadata(EmbeddedDocument):
id = StringField()
first_name = StringField()
last_name = StringField()
departments = ListField(StringField())
class ProfessorVector(Document):
meta = {'collection': 'professorVectors'}
vec = ListField(FloatField())
metadata = EmbeddedDocumentField(ProfessorMetadata)
And setup a schema like so
class Query(graphene.ObjectType):
professor_vector = graphene.Field(ProfessorVector, id=graphene.String())
def resolve_professor_vector(self, info, id):
print(id)
return ProfessorVectorModel.objects(metadata__id=id).first()
schema = graphene.Schema(query=Query, types=[ProfessorVector])
And a sample of the object in the database:
{
"_id" : ObjectId("5b0c4c9628086740a872dd4d"),
"id" : "5e06aa20-6805-4eef-a144-5615dedbe32b",
"vec" : [
-1.9864423274993896,
-0.6392910480499268
],
"metadata" : {
"id" : "5e06aa20-6805-4eef-a144-5615dedbe32b",
"first_name" : "Nigel S",
"last_name" : "Paneth",
"departments" : [
"Epidemiology and Biostatistics"
]
}
}
However with this setup, I am not able to run the following query in graphiql:
query {
professorVector(id: "5e06aa20-6805-4eef-a144-5615dedbe32b") {
id
vec
metadata {
first_name
}
}
}
I get the following error:
{
"errors": [
{
"message": "Cannot query field \"metadata\" on type \"ProfessorVector\".",
"locations": [
{
"line": 5,
"column": 5
}
]
}
]
}
What's the issue with EmbeddedDocumentField
? Does graphene-mongo
support this kind of embedded document? Any help would be appreciated.
Thanks.
@atbe : Thanks for the feedback ๐
I think I have covered embedded document query already, you can refer following test case I just finished (and it's merged into master just as well)
https://github.com/graphql-python/graphene-mongo/pull/33/files#diff-ebf89c260b02c988b3c36c1cdd0d8463R232
Ping me if you have any question ๐
@atbe: I think I know why your query did not work :(
We have to declare ProfessorMetadata
before ProfessorVector
, say in your schema:
class ProfessorMetadata(MongoengineObjectType):
class Meta:
model = ProfessorMetadataModel
class ProfessorVector(MongoengineObjectType):
class Meta:
model = ProfessorVectorModel
class Query(graphene.ObjectType):
professor_vector = graphene.Field(ProfessorVector, id=graphene.String())
def resolve_professor_vector(self, info, id):
print(id)
return ProfessorVectorModel.objects(metadata__id=id).first()
schema = graphene.Schema(query=Query, types=[ProfessorVector])
only that metadata could be resolved.
You can say it is by-design or the limitation of graphene-mongo
, since currently it can't resolve reference model if not declared before.