Graphene: Self parameter is None in mutate method

Created on 14 Aug 2018  ·  24Comments  ·  Source: graphql-python/graphene

I don't know, maybe it's not a bug and problem caused by rx, but i have this mutation

class ExtendedMutation(graphene.Mutation):

    ok = graphene.Boolean()
    code = graphene.Int()
    message = graphene.String()

    def mutate(self, *_, **__):
        return ExtendedMutation()

class UserLogin(ExtendedMutation):
    class Arguments:
        access_token = graphene.String()

    access_token = graphene.String()

    @login_required
    def mutate(self, info, access_token):
        response = client.login(access_token)
        access_token = response.get('data', dict()).get('access_token', None)

        return UserLogin(
            access_token=access_token,
            ok=response.get('ok'),
            code=response.get('code'),
            message=response.get('message')
        )

and wrote such decorator

def login_required(mutation):
    @wraps(mutation)
    def inner(obj, info, **kwargs):
        access_token = request.headers.get('access_token', None)

        print('obj: {}, info; {}, kwargs: {} '.format(obj, info, kwargs))
        if not access_token:
            return obj(
                ok=False, code=401, message='You must login first'
            )
        return mutation(obj, info, **kwargs)
    return inner

I wait for UserLogin object in self argument of mutation method, but it's equal None. Info is equal ResolveObject. When i wraps mutate method with @classmethod decorator, i got UserLogin login class, info is None and appears third position required argument which is actually is info.

Any ideas?

Most helpful comment

The intended behavior here makes no sense. The point of a library like graphene is fit some abstraction like graphql into the domain of python. When you ignore the conventions of the target domain (namely python's use of self on methods in classes), you have failed to make a good library.

If I wanted the function signatures to be exactly the same as Apollo, I'd use Apollo.

Please reinstate self on resolve_... methods and Mutation classes

All 24 comments

I bet we are doing something wrong here, @symstu .
But I have the same issue. I try to call a method I defined in my Mutation object, it's called validate.
When I try to call this method from my mutate method, the self param is empty. I think the usage will have changed when they removed the @classmethod

First off, I am using graphene-django, so this might be totally unrelated.
As a current work around I keep mutate as a @classmethod.
I also changed the signature from self, info, **kwargs to cls, root, info, **kwargs

This also seems to apply to resolve_* methods too.

I also experience some frustrations with this and I think this is intended. I remember I saw a discussion in another issue related on this and the conclusion was that this design was deliberately implemented like this but I don't remember the justification and I can't seem to find the issue right now.

I think this is a very poor design choice as it doesn't allow any OOP principle to be applied for mutations and queries as I can't access any method or member. What's the point of using classes if you can't use OOP principles anyway?

Any explanation here? This is bizarre.

I think the rationale is that the Mutation class is not a traditional class. Instead serves only as a description of a mutation. I also think the unbounded mutate was to fix a bug. 🤷🏻‍♂️

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Commenting to unstale. This design should be improved, or strongly documented with workarounds/suggestions (and workaround implications).

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Still a valid issue

It's not a issue. it is intended,
for make it simplify to access parent schema, first argument on resolver is "parent schema".
I'm agree with @vladcalin's opinion(it is wired with OOP principle).

grahpene documents help me to understand how it works.
https://docs.graphene-python.org/en/latest/types/objecttypes/#resolver-parameters

Whether it's intended is not the issue, the issue is what's right. :) This behavior breaks convention, reduces functionality, and smells like over-engineering.

The intended behavior here makes no sense. The point of a library like graphene is fit some abstraction like graphql into the domain of python. When you ignore the conventions of the target domain (namely python's use of self on methods in classes), you have failed to make a good library.

If I wanted the function signatures to be exactly the same as Apollo, I'd use Apollo.

Please reinstate self on resolve_... methods and Mutation classes

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This is still a valid open issue.

This has bitten me now as well. This is an unpython and confusing design choice.

This is still a valid issue. Has there been any justification for the design?

I've created a proposal for an alternative API for mutations that should solve this issue: https://github.com/graphql-python/graphene/issues/1226

Feedback welcome!

still seeing this

Just got hit by this, wasted 15 minutes.

So, what`s the solution? How to get a self variable?

This is nothing short of a poor design choice. I have been working around this on the resolver side for a sizable graphql api, now hit by the same issue with mutations. Cant even do something a simply OO as get an inherited variable from a base class.

Just ran into this issue and found this thread via Google search. I'd be interested in learning more about the design decisions around this as its causing some awkward workarounds in my project.

Was this page helpful?
0 / 5 - 0 ratings