Skip to content

ObjectType resolving fields from dicts or objects #798

Closed
@ilyakitaev

Description

@ilyakitaev

Hi! There is an example code:

class Photo(graphene.ObjectTypet):
  
      id = graphene.ID()
      url = graphene.String()
      fileName = graphene.String()
      extension = graphene.String()
  
  
class User(graphene.ObjectTypet):
  
      _id = graphene.ID()
      name = graphene.String()
      photos = graphene.List(Photo)

class Query(graphene.ObjectType):
      user = graphene.Field(User, dict(_id=graphene.String(required=True)))
  
      async def resolve_user(self, info, _id):
          user = await db.users.find_one(dict(_id=_id))
          # do the simple filted
          resolved_fileds = {f: user[f]
                           for f in User._meta.fields}
          user = User(**resolved_fileds) if user else None
          return user

The result is

{
  "data": {
    "user": {
      "Id": "1",
      "name": "Test User",
      "photos": [
        {
          "id": null,
          "fileName": null
        }        
      ]
    }
  }
}

Field "photos" not initialized in init. In argument "photos" I got just list with dicts. I know that I can to this serialization by myself, but maybe it's already implemented?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions