diff --git a/.github/stale.yml b/.github/stale.yml index dab9fb3..d066ca6 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -1,7 +1,7 @@ # Number of days of inactivity before an issue becomes stale -daysUntilStale: 90 +daysUntilStale: 120 # Number of days of inactivity before a stale issue is closed -daysUntilClose: 14 +daysUntilClose: 30 # Issues with these labels will never be considered stale exemptLabels: - pinned diff --git a/docs/introspection.rst b/docs/introspection.rst index dea55bd..2097c30 100644 --- a/docs/introspection.rst +++ b/docs/introspection.rst @@ -29,6 +29,20 @@ you're ready to use Relay with Graphene GraphQL implementation. The schema file is sorted to create a reproducible canonical representation. +GraphQL SDL Representation +-------------------------- + +The schema can also be exported as a GraphQL SDL file by changing the file +extension : + +.. code:: bash + + ./manage.py graphql_schema --schema tutorial.quickstart.schema --out schema.graphql + +When exporting the schema as a ``.graphql`` file the ``--indent`` option is +ignored. + + Advanced Usage -------------- diff --git a/docs/settings.rst b/docs/settings.rst index 4776ce0..5a7e4c9 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -140,3 +140,33 @@ Default: ``False`` # 'messages': ['This field is required.'], # } # ] + + +``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` +-------------------------------------- + +Set to ``True`` to use the new naming format for the auto generated Enum types from Django choice fields. The new format looks like this: ``{app_label}{object_name}{field_name}Choices`` + +Default: ``False`` + + +``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME`` +-------------------------------------- + +Define the path of a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type. + +If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored. + +Default: ``None`` + +.. code:: python + + # myapp.utils + def enum_naming(field): + if isinstance(field.model, User): + return f"CustomUserEnum{field.name.title()}" + return f"CustomEnum{field.name.title()}" + + GRAPHENE = { + 'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': "myapp.utils.enum_naming" + } diff --git a/graphene_django/__init__.py b/graphene_django/__init__.py index dbdc201..6574745 100644 --- a/graphene_django/__init__.py +++ b/graphene_django/__init__.py @@ -1,6 +1,6 @@ from .types import DjangoObjectType from .fields import DjangoConnectionField -__version__ = "2.8.2" +__version__ = "2.9.0" __all__ = ["__version__", "DjangoObjectType", "DjangoConnectionField"] diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 8b93d17..bd8f79d 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -1,6 +1,7 @@ from collections import OrderedDict from django.db import models from django.utils.encoding import force_str +from django.utils.module_loading import import_string from graphene import ( ID, @@ -22,6 +23,7 @@ from graphene.types.json import JSONString from graphene.utils.str_converters import to_camel_case, to_const from graphql import assert_valid_name +from .settings import graphene_settings from .compat import ArrayField, HStoreField, JSONField, RangeField from .fields import DjangoListField, DjangoConnectionField from .utils import import_single_dispatch @@ -68,6 +70,31 @@ def convert_choices_to_named_enum_with_descriptions(name, choices): return Enum(name, list(named_choices), type=EnumWithDescriptionsType) +def generate_enum_name(django_model_meta, field): + if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME: + # Try and import custom function + custom_func = import_string( + graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME + ) + name = custom_func(field) + elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True: + name = "{app_label}{object_name}{field_name}Choices".format( + app_label=to_camel_case(django_model_meta.app_label.title()), + object_name=django_model_meta.object_name, + field_name=to_camel_case(field.name.title()), + ) + else: + name = to_camel_case("{}_{}".format(django_model_meta.object_name, field.name)) + return name + + +def convert_choice_field_to_enum(field, name=None): + if name is None: + name = generate_enum_name(field.model._meta, field) + choices = field.choices + return convert_choices_to_named_enum_with_descriptions(name, choices) + + def convert_django_field_with_choices( field, registry=None, convert_choices_to_enum=True ): @@ -77,9 +104,7 @@ def convert_django_field_with_choices( return converted choices = getattr(field, "choices", None) if choices and convert_choices_to_enum: - meta = field.model._meta - name = to_camel_case("{}_{}".format(meta.object_name, field.name)) - enum = convert_choices_to_named_enum_with_descriptions(name, choices) + enum = convert_choice_field_to_enum(field) required = not (field.blank or field.null) converted = enum(description=field.help_text, required=required) else: diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 47b44f6..fb6b98a 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -1,5 +1,6 @@ from functools import partial +import six from django.db.models.query import QuerySet from graphql_relay.connection.arrayconnection import connection_from_list_slice from promise import Promise @@ -19,19 +20,23 @@ class DjangoListField(Field): if isinstance(_type, NonNull): _type = _type.of_type - assert issubclass( - _type, DjangoObjectType - ), "DjangoListField only accepts DjangoObjectType types" - # Django would never return a Set of None vvvvvvv super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs) + assert issubclass( + self._underlying_type, DjangoObjectType + ), "DjangoListField only accepts DjangoObjectType types" + + @property + def _underlying_type(self): + _type = self._type + while hasattr(_type, "of_type"): + _type = _type.of_type + return _type + @property def model(self): - _type = self.type.of_type - if isinstance(_type, NonNull): - _type = _type.of_type - return _type._meta.model + return self._underlying_type._meta.model @staticmethod def list_resolver(django_object_type, resolver, root, info, **args): diff --git a/graphene_django/management/commands/graphql_schema.py b/graphene_django/management/commands/graphql_schema.py index 1e8baf6..751a385 100644 --- a/graphene_django/management/commands/graphql_schema.py +++ b/graphene_django/management/commands/graphql_schema.py @@ -1,3 +1,4 @@ +import os import importlib import json import functools @@ -5,6 +6,7 @@ import functools from django.core.management.base import BaseCommand, CommandError from django.utils import autoreload +from graphql import print_schema from graphene_django.settings import graphene_settings @@ -44,24 +46,40 @@ class CommandArguments(BaseCommand): class Command(CommandArguments): - help = "Dump Graphene schema JSON to file" + help = "Dump Graphene schema as a JSON or GraphQL file" can_import_settings = True - def save_file(self, out, schema_dict, indent): + def save_json_file(self, out, schema_dict, indent): with open(out, "w") as outfile: json.dump(schema_dict, outfile, indent=indent, sort_keys=True) + def save_graphql_file(self, out, schema): + with open(out, "w") as outfile: + outfile.write(print_schema(schema)) + def get_schema(self, schema, out, indent): schema_dict = {"data": schema.introspect()} if out == "-": self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True)) else: - self.save_file(out, schema_dict, indent) + # Determine format + _, file_extension = os.path.splitext(out) + + if file_extension == ".graphql": + self.save_graphql_file(out, schema) + elif file_extension == ".json": + self.save_json_file(out, schema_dict, indent) + else: + raise CommandError( + 'Unrecognised file format "{}"'.format(file_extension) + ) style = getattr(self, "style", None) success = getattr(style, "SUCCESS", lambda x: x) - self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out)) + self.stdout.write( + success("Successfully dumped GraphQL schema to {}".format(out)) + ) def handle(self, *args, **options): options_schema = options.get("schema") diff --git a/graphene_django/settings.py b/graphene_django/settings.py index 05eae80..fe8a9ff 100644 --- a/graphene_django/settings.py +++ b/graphene_django/settings.py @@ -36,7 +36,10 @@ DEFAULTS = { # Max items returned in ConnectionFields / FilterConnectionFields "RELAY_CONNECTION_MAX_LIMIT": 100, "CAMELCASE_ERRORS": False, - "SOURCE": None + "SOURCE": None, + # Set to True to enable v3 naming convention for choice field Enum's + "DJANGO_CHOICE_FIELD_ENUM_V3_NAMING": False, + "DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None, } if settings.DEBUG: diff --git a/graphene_django/templates/graphene/base.html b/graphene_django/templates/graphene/base.html index 5a29b32..baee34d 100755 --- a/graphene_django/templates/graphene/base.html +++ b/graphene_django/templates/graphene/base.html @@ -14,11 +14,11 @@ } - - - - - + + + + +