graphene icon indicating copy to clipboard operation
graphene copied to clipboard

How to get access to the queried object instance to get its attributes

Open ptomask opened this issue 10 years ago • 1 comments

Hi, Having this graphql schema:

class GQUser(graphene.ObjectType):
  id = graphene.Int()

  def resolve_id(self, args, info):
    return None #... ? Return what ?

class GQQuery(graphene.ObjectType):
  user = graphene.List(GQUser)

  def resolve_user(self, args, info):
    return [User(id = 3, username = "paul"), User(id = 4, username = "peter")]

and this query:

    query Users {
      user {
        id
      }
    }

How can I gain access to the particular User instance (corresponding item of the list returned by resolve_user) to get its id attribute in GQUser.resolve_id method?

ptomask avatar Apr 11 '16 15:04 ptomask

If you remove the resolve_id method in the GQUser class it will automatically took it. However if you still want to get it you can do return self.id

syrusakbary avatar Apr 13 '16 04:04 syrusakbary