2016-09-20 08:04:23 +03:00
|
|
|
"""
|
|
|
|
Settings for Graphene are all namespaced in the GRAPHENE setting.
|
|
|
|
For example your project's `settings.py` file might look like this:
|
|
|
|
GRAPHENE = {
|
|
|
|
'SCHEMA': 'my_app.schema.schema'
|
|
|
|
'MIDDLEWARE': (
|
|
|
|
'graphene_django.debug.DjangoDebugMiddleware',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
This module provides the `graphene_settings` object, that is used to access
|
|
|
|
Graphene settings, checking for user settings first, then falling
|
|
|
|
back to the defaults.
|
|
|
|
"""
|
|
|
|
|
2021-03-24 09:32:37 +03:00
|
|
|
import importlib # Available in Python 3.1+
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2023-08-06 01:47:00 +03:00
|
|
|
from django.conf import settings
|
|
|
|
from django.test.signals import setting_changed
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
# Copied shamelessly from Django REST Framework
|
|
|
|
|
|
|
|
DEFAULTS = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"SCHEMA": None,
|
|
|
|
"SCHEMA_OUTPUT": "schema.json",
|
2019-03-31 13:30:29 +03:00
|
|
|
"SCHEMA_INDENT": 2,
|
2018-07-20 02:51:33 +03:00
|
|
|
"MIDDLEWARE": (),
|
2017-04-15 12:09:05 +03:00
|
|
|
# Set to True if the connection fields must have
|
|
|
|
# either the first or last argument
|
2018-07-20 02:51:33 +03:00
|
|
|
"RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST": False,
|
2017-04-15 12:09:05 +03:00
|
|
|
# Max items returned in ConnectionFields / FilterConnectionFields
|
2018-07-20 02:51:33 +03:00
|
|
|
"RELAY_CONNECTION_MAX_LIMIT": 100,
|
2020-05-21 18:42:15 +03:00
|
|
|
"CAMELCASE_ERRORS": True,
|
2020-06-10 19:32:07 +03:00
|
|
|
# Set to True to enable v2 naming convention for choice field Enum's
|
|
|
|
"DJANGO_CHOICE_FIELD_ENUM_V2_NAMING": False,
|
2020-03-13 13:04:25 +03:00
|
|
|
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
|
2020-07-12 16:42:31 +03:00
|
|
|
# Use a separate path for handling subscriptions.
|
|
|
|
"SUBSCRIPTION_PATH": None,
|
2020-08-07 12:13:26 +03:00
|
|
|
# By default GraphiQL headers editor tab is enabled, set to False to hide it
|
|
|
|
# This sets headerEditorEnabled GraphiQL option, for details go to
|
|
|
|
# https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
|
|
|
|
"GRAPHIQL_HEADER_EDITOR_ENABLED": True,
|
2022-09-24 17:41:14 +03:00
|
|
|
"GRAPHIQL_SHOULD_PERSIST_HEADERS": False,
|
2020-12-31 02:37:57 +03:00
|
|
|
"ATOMIC_MUTATIONS": False,
|
2022-09-26 01:56:22 +03:00
|
|
|
"TESTING_ENDPOINT": "/graphql",
|
2016-09-20 08:04:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if settings.DEBUG:
|
2018-07-20 02:51:33 +03:00
|
|
|
DEFAULTS["MIDDLEWARE"] += ("graphene_django.debug.DjangoDebugMiddleware",)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
# List of settings that may be in string import notation.
|
2018-07-20 02:51:33 +03:00
|
|
|
IMPORT_STRINGS = ("MIDDLEWARE", "SCHEMA")
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
def perform_import(val, setting_name):
|
|
|
|
"""
|
|
|
|
If the given setting is a string import notation,
|
|
|
|
then perform the necessary import or imports.
|
|
|
|
"""
|
|
|
|
if val is None:
|
|
|
|
return None
|
2020-04-06 15:21:07 +03:00
|
|
|
elif isinstance(val, str):
|
2016-09-20 08:04:23 +03:00
|
|
|
return import_from_string(val, setting_name)
|
|
|
|
elif isinstance(val, (list, tuple)):
|
|
|
|
return [import_from_string(item, setting_name) for item in val]
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
|
|
def import_from_string(val, setting_name):
|
|
|
|
"""
|
|
|
|
Attempt to import a class from a string representation.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Nod to tastypie's use of importlib.
|
2018-07-20 02:51:33 +03:00
|
|
|
parts = val.split(".")
|
|
|
|
module_path, class_name = ".".join(parts[:-1]), parts[-1]
|
2016-09-20 08:04:23 +03:00
|
|
|
module = importlib.import_module(module_path)
|
|
|
|
return getattr(module, class_name)
|
|
|
|
except (ImportError, AttributeError) as e:
|
2022-10-19 17:10:30 +03:00
|
|
|
msg = "Could not import '{}' for Graphene setting '{}'. {}: {}.".format(
|
2018-07-20 02:51:33 +03:00
|
|
|
val,
|
|
|
|
setting_name,
|
|
|
|
e.__class__.__name__,
|
|
|
|
e,
|
|
|
|
)
|
2016-09-20 08:04:23 +03:00
|
|
|
raise ImportError(msg)
|
|
|
|
|
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
class GrapheneSettings:
|
2016-09-20 08:04:23 +03:00
|
|
|
"""
|
|
|
|
A settings object, that allows API settings to be accessed as properties.
|
|
|
|
For example:
|
|
|
|
from graphene_django.settings import settings
|
|
|
|
print(settings.SCHEMA)
|
|
|
|
Any setting with string import paths will be automatically resolved
|
|
|
|
and return the class, rather than the string literal.
|
|
|
|
"""
|
2016-09-20 08:15:10 +03:00
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
def __init__(self, user_settings=None, defaults=None, import_strings=None):
|
|
|
|
if user_settings:
|
|
|
|
self._user_settings = user_settings
|
|
|
|
self.defaults = defaults or DEFAULTS
|
|
|
|
self.import_strings = import_strings or IMPORT_STRINGS
|
|
|
|
|
|
|
|
@property
|
|
|
|
def user_settings(self):
|
2018-07-20 02:51:33 +03:00
|
|
|
if not hasattr(self, "_user_settings"):
|
|
|
|
self._user_settings = getattr(settings, "GRAPHENE", {})
|
2016-09-20 08:04:23 +03:00
|
|
|
return self._user_settings
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
if attr not in self.defaults:
|
|
|
|
raise AttributeError("Invalid Graphene setting: '%s'" % attr)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Check if present in user settings
|
|
|
|
val = self.user_settings[attr]
|
|
|
|
except KeyError:
|
|
|
|
# Fall back to defaults
|
|
|
|
val = self.defaults[attr]
|
|
|
|
|
|
|
|
# Coerce import strings into classes
|
|
|
|
if attr in self.import_strings:
|
|
|
|
val = perform_import(val, attr)
|
|
|
|
|
|
|
|
# Cache the result
|
|
|
|
setattr(self, attr, val)
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
|
|
graphene_settings = GrapheneSettings(None, DEFAULTS, IMPORT_STRINGS)
|
|
|
|
|
|
|
|
|
|
|
|
def reload_graphene_settings(*args, **kwargs):
|
|
|
|
global graphene_settings
|
2018-07-20 02:51:33 +03:00
|
|
|
setting, value = kwargs["setting"], kwargs["value"]
|
|
|
|
if setting == "GRAPHENE":
|
2016-09-20 08:04:23 +03:00
|
|
|
graphene_settings = GrapheneSettings(value, DEFAULTS, IMPORT_STRINGS)
|
|
|
|
|
|
|
|
|
|
|
|
setting_changed.connect(reload_graphene_settings)
|