Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware example fails in doc #812

Closed
leebenson opened this issue Aug 15, 2018 · 4 comments
Closed

Middleware example fails in doc #812

leebenson opened this issue Aug 15, 2018 · 4 comments

Comments

@leebenson
Copy link

leebenson commented Aug 15, 2018

From http://docs.graphene-python.org/en/latest/execution/middleware/#example:

class AuthorizationMiddleware(object):
    def resolve(self, next, root, info, **args):
        if info.field_name == 'user':
            return None
        return next(root, info, **args)

Results in:

graphql.error.located_error.GraphQLLocatedError: resolve() missing 1 required positional argument: 'info'

It was only due to stumbling across #612 that I read this:

self not being a class instance in resolver 'methods'. I don't understand the architectural reasons why this can't be the case, but in Python an instance method (like the resolver methods) is expected to take a self first argument. The documentation says that this isn't the case with Graphene (which is unexpected), but it still calls the argument self in some cases which further confuses the issue. It is mentioned that these methods act like static methods, however it's unexpected that they aren't decorated with the @staticmethod decorator.

Which works:

class AuthorizationMiddleware(object):
    def resolve(next, root, info, **args): # remove `self` as first param
        if info.field_name == 'user':
            return None
        return next(root, info, **args)
@jkimbo
Copy link
Member

jkimbo commented Aug 25, 2018

@leebenson good spot! Could you submit a PR that fixes it? You can find instructions on how to build the docs in the README: https://github.com/graphql-python/graphene#documentation

@dani0805
Copy link

just made a PR for the middleware documentation covering its configuration in settings.py #820
Placing it here for lack of better ideas

jkimbo added a commit that referenced this issue Mar 9, 2019
jkimbo added a commit that referenced this issue Mar 10, 2019
dan98765 pushed a commit to dan98765/graphene that referenced this issue Mar 12, 2019
@riverfr0zen
Copy link

Doesn't look like this has made it into the docs yet at https://docs.graphene-python.org/en/latest/execution/middleware/

@AlexEshoo
Copy link

AlexEshoo commented Aug 5, 2020

The documentation still shows the old signature for the resolve method on middleware classes.
I also find the signature as defined in the PR here to be a bit strange, does the first parameter of the method get overloaded? Having it not be self (not just not calling it that, but also it appears to replace the argument with the next partial function instead of it being a reference to the instance of the class) just does not feel intuitive, and as far as I can tell it completely nullifies any benefit to using a class rather than a function for the middleware.

EDIT:
After looking more into the issue, it turns out I was confused on instantiating the middleware when creating the view function.
Before I was doing:

api.add_url_rule(
    "",
    view_func=GraphQLView.as_view(
        name="graphql",
        schema=schema,
        middleware=[ErrorMiddleware]
    )
)

Once I actually called the constructor for the class within the middleware list argument (ie: middleware=[ErrorMiddleware()]) I could use the method signature shown in the docs. Not sure anything needs to change now, but I'll leave this here in case anyone else end up here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants