diff --git a/graphene_pydantic/converters.py b/graphene_pydantic/converters.py index 8e93afe..5fb008a 100644 --- a/graphene_pydantic/converters.py +++ b/graphene_pydantic/converters.py @@ -123,7 +123,7 @@ def convert_pydantic_field( declared_type, field, registry, parent_type=parent_type, model=model ), ) - field_kwargs.setdefault("required", field.required) + field_kwargs.setdefault("required", not field.allow_none) field_kwargs.setdefault("default_value", field.default) # TODO: find a better way to get a field's description. Some ideas include: # - hunt down the description from the field's schema, or the schema diff --git a/tests/test_converters.py b/tests/test_converters.py index 1b0f1d9..1354524 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -43,9 +43,9 @@ def test_default_values(): field = _convert_field_from_spec("s", "hi") assert field is not None assert isinstance(field, graphene.Field) - # there's a default value, so it's not required - assert not isinstance(field.type, graphene.NonNull) - assert field.type == graphene.String + # there's a default value, so it never null + assert isinstance(field.type, graphene.NonNull) + assert field.type.of_type == graphene.String assert field.default_value == "hi" @@ -65,15 +65,15 @@ def test_default_values(): def test_builtin_scalars(input, expected): field = _convert_field_from_spec("attr", input) assert isinstance(field, graphene.Field) - assert field.type == expected + assert field.type.of_type == expected assert field.default_value == input[1] def test_union(): field = _convert_field_from_spec("attr", (T.Union[int, float, str], 5.0)) - assert issubclass(field.type, graphene.Union) + assert issubclass(field.type.of_type, graphene.Union) assert field.default_value == 5.0 - assert field.type.__name__.startswith("UnionOf") + assert field.type.of_type.__name__.startswith("UnionOf") if sys.version_info >= (3, 8): @@ -83,15 +83,15 @@ def test_literal(): field = _convert_field_from_spec( "attr", (T.Literal["literal1", "literal2", 3], 3) ) - assert issubclass(field.type, graphene.Union) + assert issubclass(field.type.of_type, graphene.Union) assert field.default_value == 3 - assert field.type.__name__.startswith("UnionOf") + assert field.type.of_type.__name__.startswith("UnionOf") def test_literal_singleton(): field = _convert_field_from_spec("attr", (T.Literal["literal1"], "literal1")) - assert issubclass(field.type, graphene.String) + assert issubclass(field.type.of_type, graphene.String) assert field.default_value == "literal1" - assert field.type == graphene.String + assert field.type.of_type == graphene.String def test_mapping(): @@ -103,34 +103,34 @@ def test_mapping(): def test_decimal(monkeypatch): monkeypatch.setattr(converters, "DECIMAL_SUPPORTED", True) field = _convert_field_from_spec("attr", (decimal.Decimal, decimal.Decimal(1.25))) - assert field.type.__name__ == "Decimal" + assert field.type.of_type.__name__ == "Decimal" monkeypatch.setattr(converters, "DECIMAL_SUPPORTED", False) field = _convert_field_from_spec("attr", (decimal.Decimal, decimal.Decimal(1.25))) - assert field.type.__name__ == "Float" + assert field.type.of_type.__name__ == "Float" def test_iterables(): field = _convert_field_from_spec("attr", (T.List[int], [1, 2])) - assert isinstance(field.type, graphene.types.List) + assert isinstance(field.type.of_type, graphene.types.List) field = _convert_field_from_spec("attr", (list, [1, 2])) - assert field.type == graphene.types.List + assert field.type.of_type == graphene.types.List field = _convert_field_from_spec("attr", (T.Set[int], {1, 2})) - assert isinstance(field.type, graphene.types.List) + assert isinstance(field.type.of_type, graphene.types.List) field = _convert_field_from_spec("attr", (set, {1, 2})) - assert field.type == graphene.types.List + assert field.type.of_type == graphene.types.List field = _convert_field_from_spec("attr", (T.Tuple[int, float], (1, 2.2))) - assert isinstance(field.type, graphene.types.List) + assert isinstance(field.type.of_type, graphene.types.List) field = _convert_field_from_spec("attr", (T.Tuple[int, ...], (1, 2.2))) - assert isinstance(field.type, graphene.types.List) + assert isinstance(field.type.of_type, graphene.types.List) field = _convert_field_from_spec("attr", (tuple, (1, 2))) - assert field.type == graphene.types.List + assert field.type.of_type == graphene.types.List field = _convert_field_from_spec("attr", (T.Union[None, int], 1)) assert field.type == graphene.types.Int @@ -142,8 +142,8 @@ class Color(enum.Enum): GREEN = 2 field = _convert_field_from_spec("attr", (Color, Color.RED)) - assert field.type.__name__ == "Color" - assert field.type._meta.enum == Color + assert field.type.of_type.__name__ == "Color" + assert field.type.of_type._meta.enum == Color def test_existing_model(): @@ -157,7 +157,7 @@ class Meta: model = Foo field = _convert_field_from_spec("attr", (Foo, Foo(name="bar"))) - assert field.type == GraphFoo + assert field.type.of_type == GraphFoo def test_unresolved_placeholders():