Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to request EmbeddedDocumentField in query #32

Closed
atbe opened this issue May 29, 2018 · 3 comments
Closed

Not able to request EmbeddedDocumentField in query #32

atbe opened this issue May 29, 2018 · 3 comments

Comments

@atbe
Copy link

atbe commented May 29, 2018

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.

@abawchen
Copy link
Collaborator

@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 😃

@abawchen
Copy link
Collaborator

@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.

@atbe
Copy link
Author

atbe commented May 29, 2018

@abawchen eureka! That looks to be what my issue was. I wasn't aware that I should declare embedded documents, thanks for clearing that up.

@atbe atbe closed this as completed May 29, 2018
abhinand-c pushed a commit to strollby/graphene-mongo that referenced this issue Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants