Warn when DjangoObjectType has neither fields nor exclude option

This commit is contained in:
radoslaw.kowalski 2020-06-07 12:08:41 +02:00
parent d9c187ffc2
commit 30306e8108
2 changed files with 41 additions and 0 deletions

View File

@ -476,6 +476,37 @@ def test_django_objecttype_exclude_fields_exist_on_model():
assert len(record) == 0 assert len(record) == 0
@with_local_registry
def test_django_objecttype_neither_fields_nor_exclude():
with pytest.warns(
PendingDeprecationWarning,
match=r"Creating a DjangoObjectType without either the `fields` "
"or the `exclude` option is deprecated.",
):
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
with pytest.warns(None) as record:
class Reporter2(DjangoObjectType):
class Meta:
model = ReporterModel
fields = ["email"]
assert len(record) == 0
with pytest.warns(None) as record:
class Reporter3(DjangoObjectType):
class Meta:
model = ReporterModel
exclude = ["email"]
assert len(record) == 0
def custom_enum_name(field): def custom_enum_name(field):
return "CustomEnum{}".format(field.name.title()) return "CustomEnum{}".format(field.name.title())

View File

@ -220,6 +220,16 @@ class DjangoObjectType(ObjectType):
% type(exclude).__name__ % type(exclude).__name__
) )
if fields is None and exclude is None:
warnings.warn(
"Creating a DjangoObjectType without either the `fields` "
"or the `exclude` option is deprecated. Add an explicit `fields "
"= '__all__'` option on DjangoObjectType {class_name} to use all "
"fields".format(class_name=cls.__name__,),
PendingDeprecationWarning,
stacklevel=2,
)
django_fields = yank_fields_from_attrs( django_fields = yank_fields_from_attrs(
construct_fields(model, registry, fields, exclude, convert_choices_to_enum), construct_fields(model, registry, fields, exclude, convert_choices_to_enum),
_as=Field, _as=Field,