question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ObjectType resolving fields from dicts or objects

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

29reactions
jkimbocommented, Jul 14, 2018

You could change the default resolver on the Photo Object type like this:

from graphene.types.resolver import dict_resolver

class Photo(graphene.ObjectType):
	class Meta:
		default_resolver = dict_resolver
      
	id = graphene.ID()
    url = graphene.String()
    fileName = graphene.String()
    extension = graphene.String()
0reactions
y0urselfcommented, Feb 25, 2020

Is it correct, that the class Meta: “feature” is only for Python 2?

Is there any other workaround to set the default_resolver when using Python 3?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ObjectType - Graphene-Python
ObjectType ¶. A Graphene ObjectType is the building block used to define the relationship between Fields in your Schema and how their data...
Read more >
Python dictionary from an object's fields - Stack Overflow
I thought I'd take some time to show you how you can translate an object to dict via dict(obj) . class A(object): d...
Read more >
ObjectTypes — Graphene 1.0 documentation - Read the Docs
A resolver is a method that resolves certain fields within an ObjectType . If not specified otherwise, the resolver of a field is...
Read more >
Resolvers - Ariadne GraphQL
To bind resolvers to schema, Ariadne uses a special ObjectType class that is ... a field an alias for a differently-named attribute on...
Read more >
Get a dictionary from an Objects Fields - GeeksforGeeks
There are two approaches to solve the above problem: By using the __dict__ attribute on an object of a class and attaining the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found