diff --git a/caluma/caluma_core/filters.py b/caluma/caluma_core/filters.py index dd78a7062..5ab8fd20f 100644 --- a/caluma/caluma_core/filters.py +++ b/caluma/caluma_core/filters.py @@ -200,6 +200,36 @@ def _should_include_filter(filt): return filter_coll() +def InterfaceMetaFactory(): + """ + Build a meta class suitable for the schema type classes that represent + "interface" types (those that have concrete subclasses, but could be mixed + in a connection type, such as Question, Answer, and Task). + + Usage: + + >>> class Foo(Node, graphene.Interface): + >>> ... + >>> Meta = InterfaceMetaFactory() + """ + + class _meta(graphene.types.interface.InterfaceOptions): + @classmethod + # This is kinda useless but required as graphene tries to freeze() + # it's meta class objects + def freeze(cls): + cls._frozen = True + + # This is what we're actually "fixing": On non-Interface types, + # this somehow works (or isn't needed), but here, if _meta.registry + # is not set, the whole schema construction fails + registry = get_global_registry() + + # We need a new type (= class) each time it's called, because reuse + # triggers some weird errors + return type("Meta", (), {"_meta": _meta}) + + def CollectionFilterSetFactory(filterset_class, orderset_class=None): """ Build single-filter filterset classes. diff --git a/caluma/caluma_form/schema.py b/caluma/caluma_form/schema.py index c72597ea3..37c6adb02 100644 --- a/caluma/caluma_form/schema.py +++ b/caluma/caluma_form/schema.py @@ -8,6 +8,7 @@ CollectionFilterSetFactory, DjangoFilterConnectionField, DjangoFilterInterfaceConnectionField, + InterfaceMetaFactory, ) from ..caluma_core.mutation import Mutation, UserDefinedPrimaryKeyMixin from ..caluma_core.relay import extract_global_id @@ -159,6 +160,8 @@ def get_queryset(cls, queryset, info): def resolve_type(cls, instance, info): return resolve_question(instance) + Meta = InterfaceMetaFactory() + class Option(FormDjangoObjectType): meta = generic.GenericScalar() @@ -810,6 +813,8 @@ class Answer(Node, graphene.Interface): def resolve_type(cls, instance, info): return resolve_answer(instance) + Meta = InterfaceMetaFactory() + class AnswerQuerysetMixin(object): """Mixin to combine all different answer types into one queryset.""" diff --git a/caluma/caluma_user/views.py b/caluma/caluma_user/views.py index 87d91cea5..3417cd04b 100644 --- a/caluma/caluma_user/views.py +++ b/caluma/caluma_user/views.py @@ -8,6 +8,7 @@ from django.http.response import HttpResponse from django.utils.encoding import force_bytes, smart_str from django.utils.module_loading import import_string +from graphene.validation import DisableIntrospection from graphene_django.views import GraphQLView, HttpError from rest_framework.authentication import get_authorization_header @@ -19,6 +20,9 @@ class HttpResponseUnauthorized(HttpResponse): class AuthenticationGraphQLView(GraphQLView): + if settings.DISABLE_INTROSPECTION: # pragma: no cover + validation_rules = (DisableIntrospection,) + def get_bearer_token(self, request): auth = get_authorization_header(request).split() header_prefix = "Bearer" diff --git a/caluma/caluma_workflow/schema.py b/caluma/caluma_workflow/schema.py index 0b38c2ce8..1d0d07140 100644 --- a/caluma/caluma_workflow/schema.py +++ b/caluma/caluma_workflow/schema.py @@ -10,6 +10,7 @@ CollectionFilterSetFactory, DjangoFilterConnectionField, DjangoFilterInterfaceConnectionField, + InterfaceMetaFactory, ) from ..caluma_core.mutation import Mutation, UserDefinedPrimaryKeyMixin from ..caluma_core.types import ( @@ -97,6 +98,8 @@ def resolve_type(cls, instance, info): return TASK_TYPE[instance.type] + Meta = InterfaceMetaFactory() + class TaskConnection(CountableConnectionBase): class Meta: diff --git a/caluma/settings/caluma.py b/caluma/settings/caluma.py index c063b95f0..6f13c0ecf 100644 --- a/caluma/settings/caluma.py +++ b/caluma/settings/caluma.py @@ -51,6 +51,7 @@ def default(default_dev=env.NOTSET, default_prod=env.NOTSET): "MIDDLEWARE": [], "RELAY_CONNECTION_MAX_LIMIT": None, } +DISABLE_INTROSPECTION = env.bool("DISABLE_INTROSPECTION", default=default(False, True)) # OpenID connect diff --git a/caluma/tests/__snapshots__/test_schema.ambr b/caluma/tests/__snapshots__/test_schema.ambr index cd64e5396..88c6f40e1 100644 --- a/caluma/tests/__snapshots__/test_schema.ambr +++ b/caluma/tests/__snapshots__/test_schema.ambr @@ -831,6 +831,21 @@ type DjangoDebug { """Executed SQL queries for this API query.""" sql: [DjangoDebugSQL] + + """Raise exceptions for this API query.""" + exceptions: [DjangoDebugException] + } + + """Represents a single exception raised.""" + type DjangoDebugException { + """The class of the exception""" + excType: String! + + """The message of the exception""" + message: String! + + """The stack trace""" + stack: String! } """Represents a single database query made to a Django managed DB.""" diff --git a/poetry.lock b/poetry.lock index c57af9c47..11f0a8ec3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -913,13 +913,13 @@ test = ["black", "coverage[toml]", "ddt (>=1.1.1,!=1.4.3)", "mypy", "pre-commit" [[package]] name = "graphene" -version = "3.2.2" +version = "3.3" description = "GraphQL Framework for Python" optional = false python-versions = "*" files = [ - {file = "graphene-3.2.2-py2.py3-none-any.whl", hash = "sha256:753de13948cbf42e32cc87fb533167c88907066eb984251fdbb006c0aab8da00"}, - {file = "graphene-3.2.2.tar.gz", hash = "sha256:5b03e72770dc901f40be55784058d6bb1d952a49eb819a4a085962d5e1cf5fcf"}, + {file = "graphene-3.3-py2.py3-none-any.whl", hash = "sha256:bb3810be33b54cb3e6969506671eb72319e8d7ba0d5ca9c8066472f75bf35a38"}, + {file = "graphene-3.3.tar.gz", hash = "sha256:529bf40c2a698954217d3713c6041d69d3f719ad0080857d7ee31327112446b0"}, ] [package.dependencies] @@ -933,26 +933,27 @@ test = ["coveralls (>=3.3,<4)", "iso8601 (>=1,<2)", "mock (>=4,<5)", "pytest (>= [[package]] name = "graphene-django" -version = "3.0.0b7" +version = "3.2.0" description = "Graphene Django integration" optional = false python-versions = "*" files = [ - {file = "graphene-django-3.0.0b7.tar.gz", hash = "sha256:b1a4ce1a2227b1e77f67f0bc2fadd59c1d05016cb9aced45ab65f8612fba2c87"}, - {file = "graphene_django-3.0.0b7-py2.py3-none-any.whl", hash = "sha256:0f226ec7db744a54dbc5d6db2aa52d945701ae800e1055046dec2b76a539550a"}, + {file = "graphene-django-3.2.0.tar.gz", hash = "sha256:9aca4a862f12912c2e611624bdbcf6b0f9bc7a41d240110a41bf95575a7bacab"}, + {file = "graphene_django-3.2.0-py2.py3-none-any.whl", hash = "sha256:b553ecdc1cd7fd5b2d71de1a729c03ae117321763a90ed48a7fb4fdbf7f0d43f"}, ] [package.dependencies] -Django = ">=2.2" -graphene = ">=3.0.0b5,<4" +Django = ">=3.2" +graphene = ">=3.0,<4" graphql-core = ">=3.1.0,<4" +graphql-relay = ">=3.1.1,<4" promise = ">=2.1" text-unidecode = "*" [package.extras] -dev = ["black (==19.10b0)", "coveralls", "django-filter (>=2)", "djangorestframework (>=3.6.3)", "flake8 (==3.7.9)", "flake8-black (==0.1.1)", "flake8-bugbear (==20.1.4)", "mock", "pytest (>=3.6.3)", "pytest-cov", "pytest-django (>=3.3.2)", "pytest-random-order", "pytz"] +dev = ["coveralls", "django-filter (>=22.1)", "djangorestframework (>=3.6.3)", "mock", "pre-commit", "pytest (>=7.3.1)", "pytest-cov", "pytest-django (>=4.5.2)", "pytest-random-order", "pytz", "ruff (==0.1.2)"] rest-framework = ["djangorestframework (>=3.6.3)"] -test = ["coveralls", "django-filter (>=2)", "djangorestframework (>=3.6.3)", "mock", "pytest (>=3.6.3)", "pytest-cov", "pytest-django (>=3.3.2)", "pytest-random-order", "pytz"] +test = ["coveralls", "django-filter (>=22.1)", "djangorestframework (>=3.6.3)", "mock", "pytest (>=7.3.1)", "pytest-cov", "pytest-django (>=4.5.2)", "pytest-random-order", "pytz"] [[package]] name = "graphql-core" @@ -2438,4 +2439,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.8.1" -content-hash = "9f3c9155b69454d60e711bcb83292b2bb2267fffebb16aec111b7717073d8e14" +content-hash = "b4b8575ec1881250db8e6a775e2eb1084ea262cfc54a54ddcb2ed07dc14f3eb6" diff --git a/pyproject.toml b/pyproject.toml index 8c0f5e923..bae24f7f2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ django-postgres-extra = "^2.0.4" django-watchman = "^1.2.0" djangorestframework = "^3.13.1" django-simple-history = "^3.0.0" -graphene-django = "3.0.0b7" +graphene-django = "3.2.0" graphql-relay = "^3.1.5" idna = "^3.3" minio = "^7.1.4" @@ -101,6 +101,7 @@ filterwarnings = [ "error::DeprecationWarning", "error::PendingDeprecationWarning", "ignore:The 'arrayconnection' module is deprecated:DeprecationWarning", # deprecation in graphene + "ignore:'cgi' is deprecated:DeprecationWarning", "ignore:distutils Version classes are deprecated:DeprecationWarning", # deprecation in pytest-freezegun "ignore:'django_extensions' defines default_app_config:PendingDeprecationWarning", # deprecation in django_extensions "ignore::requests.packages.urllib3.exceptions.InsecureRequestWarning", # MinIO tests do "insecure" requests - that's ok