Search code examples
djangographene-django

How to access authenticated user in graphene-django resolve method?


I have added this to my Query class and it's returning null in response.

me = graphene.Field(UserType)

def resolve_user(root, info):
    logger.info("***** Inside resolve ****")
    return info.context.user

and my UserType is defined like this.

class UserType(DjangoObjectType):
    fields = ["id", "name", "email", "username"]

    class Meta:
        model = User

I'm on Django==3.0 if it helps

I'm authenticated and the cookies are present. It's not even printing the log which is confusing me.


Solution

  • Graphene fields use resolve_<field> pattern to resolve the values. Check more here

    me = graphene.Field(UserType)
    ^^
    
    def resolve_me(root, info):
        logger.info("***** Inside resolve ****")
        return info.context.user