Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions graphene_django_extras/directives/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@


class BaseExtraGraphQLDirective(GraphQLDirective):
default_locations = [
DirectiveLocation.FIELD,
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
]

locations = []

def __init__(self):
registry = get_global_registry()
super(BaseExtraGraphQLDirective, self).__init__(
name=self.get_name(),
description=self.__doc__,
args=self.get_args(),
locations=[
DirectiveLocation.FIELD,
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
locations=self.get_locations(),
)
registry.register_directive(self.get_name(), self)

Expand All @@ -27,3 +31,7 @@ def get_name(cls):
@staticmethod
def get_args():
return {}

@classmethod
def get_locations(cls):
return cls.locations if cls.locations else cls.default_locations
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def pytest_configure(config):
"tests",
),
PASSWORD_HASHERS=("django.contrib.auth.hashers.MD5PasswordHasher",),
GRAPHENE={"SCHEMA": "tests.schema.schema"},
GRAPHENE={"SCHEMA": "tests.schema.schema", "MIDDLEWARE":[
"graphene_django_extras.middleware.ExtraGraphQLDirectiveMiddleware",
]},
AUTHENTICATION_BACKENDS=(
"django.contrib.auth.backends.ModelBackend",
"guardian.backends.ObjectPermissionBackend",
Expand Down
8 changes: 8 additions & 0 deletions tests/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
}
}
"""

ALL_USERS_DIR = """query{
allUsers1{
username @uppercase
}
}
"""

ALL_USERS3 = """query {
allUsers3 {
id
Expand Down
3 changes: 2 additions & 1 deletion tests/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from graphene_django_extras.directives import all_directives
from graphene_django_extras.types import (
DjangoListObjectType,
DjangoSerializerType,
Expand Down Expand Up @@ -93,4 +94,4 @@ class Query(graphene.ObjectType):
user2, users = UserModelType.QueryFields()


schema = graphene.Schema(query=Query)
schema = graphene.Schema(query=Query, directives=all_directives)
5 changes: 5 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ def test_filter_charfield_iexact(self):
)


class DirectiveQueryTest(ParentTest, TestCase):
query = queries.ALL_USERS_DIR
expected_return_payload = {"data": {"allUsers1": [{"username": "GRAPHQL"}]}}


class DjangoSerializerTypeTest(ParentTest, TestCase):
expected_return_payload = {
"data": {
Expand Down