mirror of
				https://github.com/graphql-python/graphene-django.git
				synced 2025-11-04 01:47:57 +03:00 
			
		
		
		
	Adding support for disabling enum creation on SerializerMutation (#851)
This commit is contained in:
		
							parent
							
								
									6b8c5bdefc
								
							
						
					
					
						commit
						83bc32bc9d
					
				| 
						 | 
					@ -19,7 +19,13 @@ class SerializerMutationOptions(MutationOptions):
 | 
				
			||||||
    serializer_class = None
 | 
					    serializer_class = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def fields_for_serializer(serializer, only_fields, exclude_fields, is_input=False):
 | 
					def fields_for_serializer(
 | 
				
			||||||
 | 
					    serializer,
 | 
				
			||||||
 | 
					    only_fields,
 | 
				
			||||||
 | 
					    exclude_fields,
 | 
				
			||||||
 | 
					    is_input=False,
 | 
				
			||||||
 | 
					    convert_choices_to_enum=True,
 | 
				
			||||||
 | 
					):
 | 
				
			||||||
    fields = OrderedDict()
 | 
					    fields = OrderedDict()
 | 
				
			||||||
    for name, field in serializer.fields.items():
 | 
					    for name, field in serializer.fields.items():
 | 
				
			||||||
        is_not_in_only = only_fields and name not in only_fields
 | 
					        is_not_in_only = only_fields and name not in only_fields
 | 
				
			||||||
| 
						 | 
					@ -34,7 +40,9 @@ def fields_for_serializer(serializer, only_fields, exclude_fields, is_input=Fals
 | 
				
			||||||
        if is_not_in_only or is_excluded:
 | 
					        if is_not_in_only or is_excluded:
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        fields[name] = convert_serializer_field(field, is_input=is_input)
 | 
					        fields[name] = convert_serializer_field(
 | 
				
			||||||
 | 
					            field, is_input=is_input, convert_choices_to_enum=convert_choices_to_enum
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
    return fields
 | 
					    return fields
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -55,6 +63,7 @@ class SerializerMutation(ClientIDMutation):
 | 
				
			||||||
        model_operations=("create", "update"),
 | 
					        model_operations=("create", "update"),
 | 
				
			||||||
        only_fields=(),
 | 
					        only_fields=(),
 | 
				
			||||||
        exclude_fields=(),
 | 
					        exclude_fields=(),
 | 
				
			||||||
 | 
					        convert_choices_to_enum=True,
 | 
				
			||||||
        **options
 | 
					        **options
 | 
				
			||||||
    ):
 | 
					    ):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -74,10 +83,18 @@ class SerializerMutation(ClientIDMutation):
 | 
				
			||||||
            lookup_field = model_class._meta.pk.name
 | 
					            lookup_field = model_class._meta.pk.name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        input_fields = fields_for_serializer(
 | 
					        input_fields = fields_for_serializer(
 | 
				
			||||||
            serializer, only_fields, exclude_fields, is_input=True
 | 
					            serializer,
 | 
				
			||||||
 | 
					            only_fields,
 | 
				
			||||||
 | 
					            exclude_fields,
 | 
				
			||||||
 | 
					            is_input=True,
 | 
				
			||||||
 | 
					            convert_choices_to_enum=convert_choices_to_enum,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        output_fields = fields_for_serializer(
 | 
					        output_fields = fields_for_serializer(
 | 
				
			||||||
            serializer, only_fields, exclude_fields, is_input=False
 | 
					            serializer,
 | 
				
			||||||
 | 
					            only_fields,
 | 
				
			||||||
 | 
					            exclude_fields,
 | 
				
			||||||
 | 
					            is_input=False,
 | 
				
			||||||
 | 
					            convert_choices_to_enum=convert_choices_to_enum,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _meta = SerializerMutationOptions(cls)
 | 
					        _meta = SerializerMutationOptions(cls)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,13 +19,16 @@ def get_graphene_type_from_serializer_field(field):
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def convert_serializer_field(field, is_input=True):
 | 
					def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Converts a django rest frameworks field to a graphql field
 | 
					    Converts a django rest frameworks field to a graphql field
 | 
				
			||||||
    and marks the field as required if we are creating an input type
 | 
					    and marks the field as required if we are creating an input type
 | 
				
			||||||
    and the field itself is required
 | 
					    and the field itself is required
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if isinstance(field, serializers.ChoiceField) and not convert_choices_to_enum:
 | 
				
			||||||
 | 
					        graphql_type = graphene.String
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
        graphql_type = get_graphene_type_from_serializer_field(field)
 | 
					        graphql_type = get_graphene_type_from_serializer_field(field)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    args = []
 | 
					    args = []
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,7 +10,9 @@ from ..serializer_converter import convert_serializer_field
 | 
				
			||||||
from ..types import DictType
 | 
					from ..types import DictType
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _get_type(rest_framework_field, is_input=True, **kwargs):
 | 
					def _get_type(
 | 
				
			||||||
 | 
					    rest_framework_field, is_input=True, convert_choices_to_enum=True, **kwargs
 | 
				
			||||||
 | 
					):
 | 
				
			||||||
    # prevents the following error:
 | 
					    # prevents the following error:
 | 
				
			||||||
    # AssertionError: The `source` argument is not meaningful when applied to a `child=` field.
 | 
					    # AssertionError: The `source` argument is not meaningful when applied to a `child=` field.
 | 
				
			||||||
    # Remove `source=` from the field declaration.
 | 
					    # Remove `source=` from the field declaration.
 | 
				
			||||||
| 
						 | 
					@ -21,7 +23,9 @@ def _get_type(rest_framework_field, is_input=True, **kwargs):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    field = rest_framework_field(**kwargs)
 | 
					    field = rest_framework_field(**kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return convert_serializer_field(field, is_input=is_input)
 | 
					    return convert_serializer_field(
 | 
				
			||||||
 | 
					        field, is_input=is_input, convert_choices_to_enum=convert_choices_to_enum
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def assert_conversion(rest_framework_field, graphene_field, **kwargs):
 | 
					def assert_conversion(rest_framework_field, graphene_field, **kwargs):
 | 
				
			||||||
| 
						 | 
					@ -73,6 +77,16 @@ def test_should_choice_convert_enum():
 | 
				
			||||||
    assert field._meta.enum.__members__["W"].description == "World"
 | 
					    assert field._meta.enum.__members__["W"].description == "World"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_should_choice_convert_string_if_enum_disabled():
 | 
				
			||||||
 | 
					    assert_conversion(
 | 
				
			||||||
 | 
					        serializers.ChoiceField,
 | 
				
			||||||
 | 
					        graphene.String,
 | 
				
			||||||
 | 
					        choices=[("h", "Hello"), ("w", "World")],
 | 
				
			||||||
 | 
					        source="word",
 | 
				
			||||||
 | 
					        convert_choices_to_enum=False,
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_should_base_field_convert_string():
 | 
					def test_should_base_field_convert_string():
 | 
				
			||||||
    assert_conversion(serializers.Field, graphene.String)
 | 
					    assert_conversion(serializers.Field, graphene.String)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user