Skip to content

Commit

Permalink
Added support for Graphene v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mongkok committed Aug 2, 2020
1 parent 1229f4b commit d50a533
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
7 changes: 5 additions & 2 deletions graphql_jwt/decorators.py
Expand Up @@ -7,7 +7,7 @@
from django.utils.translation import gettext as _

from graphene.utils.thenables import maybe_thenable
from graphql.execution.base import ResolveInfo
from graphql.execution.execute import GraphQLResolveInfo

from . import exceptions, signals
from .refresh_token.shortcuts import create_refresh_token, refresh_token_lazy
Expand All @@ -31,7 +31,10 @@
def context(f):
def decorator(func):
def wrapper(*args, **kwargs):
info = next(arg for arg in args if isinstance(arg, ResolveInfo))
info = next(
arg for arg in args
if isinstance(arg, GraphQLResolveInfo)
)
return func(info.context, *args, **kwargs)
return wrapper
return decorator
Expand Down
4 changes: 2 additions & 2 deletions graphql_jwt/middleware.py
Expand Up @@ -15,8 +15,8 @@
def allow_any(info, **kwargs):
field = getattr(
info.schema,
'get_{}_type'.format(info.operation.operation),
)().fields.get(info.field_name)
'{}_type'.format(info.operation.operation.name.lower()),
).fields.get(info.field_name)

if field is None:
return False
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Expand Up @@ -36,9 +36,8 @@ def get_version(package):
)),
packages=find_packages(exclude=['tests*']),
install_requires=[
'Django >=1.11',
'graphene-django>=2.1.5',
'graphql-core>=2.1,<3',
'Django>=1.11',
'graphene-django>=3.0.0b1',
'PyJWT>=1.5.0',
],
classifiers=[
Expand Down
14 changes: 4 additions & 10 deletions tests/test_middleware.py
Expand Up @@ -185,17 +185,15 @@ class AllowAnyTests(TestCase):

def info(self, user, **headers):
info_mock = super().info(user, **headers)
info_mock.field_name = 'test_field'
info_mock.operation.operation = 'query'
info_mock.field_name = 'test'
info_mock.operation.operation.name = 'query'
return info_mock

def info_with_field_mock(self, user, field=None):
info_mock = self.info(user)
field_mock = mock.Mock(fields={
'test_field': field,
info_mock.schema.query_type = mock.Mock(fields={
'test': field,
})

info_mock.schema.get_query_type.return_value = field_mock
return info_mock

def info_with_type_mock(self, user, type=None):
Expand All @@ -208,25 +206,21 @@ def test_allow_any(self):
allowed = allow_any(info_mock)

self.assertTrue(allowed)
info_mock.schema.get_query_type.assert_called_once_with()

def test_not_allow_any(self):
info_mock = self.info_with_type_mock(self.user, TestCase)
allowed = allow_any(info_mock)

self.assertFalse(allowed)
info_mock.schema.get_query_type.assert_called_once_with()

def test_unknown_field(self):
info_mock = self.info_with_field_mock(self.user)
allowed = allow_any(info_mock)

self.assertFalse(allowed)
info_mock.schema.get_query_type.assert_called_once_with()

def test_unknown_type(self):
info_mock = self.info_with_type_mock(self.user)
allowed = allow_any(info_mock)

self.assertFalse(allowed)
info_mock.schema.get_query_type.assert_called_once_with()
14 changes: 11 additions & 3 deletions tests/testcases.py
Expand Up @@ -4,8 +4,9 @@
from django.contrib.auth import get_user_model
from django.test import RequestFactory, testcases

import graphene
from graphene_django.views import GraphQLView
from graphql.execution.base import ResolveInfo
from graphql.execution.execute import GraphQLResolveInfo

from graphql_jwt.decorators import jwt_cookie
from graphql_jwt.settings import jwt_settings
Expand Down Expand Up @@ -36,11 +37,18 @@ def info(self, user=None, **headers):
if user is not None:
request.user = user

return mock.Mock(context=request, path=['test'], spec=ResolveInfo)
return mock.Mock(
context=request,
path=['test'],
spec=GraphQLResolveInfo,
)


class SchemaTestCase(TestCase, JSONWebTokenTestCase):
Query = None

class Query(graphene.ObjectType):
test = graphene.String()

Mutation = None

def setUp(self):
Expand Down

0 comments on commit d50a533

Please sign in to comment.