Repository: strawberry-graphql/strawberry

Issue:
Lists being marked as Optional
When defining a list the resulting schema marks the list as optional (or nullable in GraphQL terms) even if it wasn't wrapped in `typing.Optional`, we should fix that :)


Discussion:

Resolving patch:
diff --git a/strawberry/type_converter.py b/strawberry/type_converter.py
--- a/strawberry/type_converter.py
+++ b/strawberry/type_converter.py
@@ -44,7 +44,9 @@ def get_graphql_type_for_annotation(
                 annotation.__args__[0], field_name
             )
 
-            return GraphQLList(list_of_type)
+            list_type = GraphQLList(list_of_type)
+
+            return list_type if is_field_optional else GraphQLNonNull(list_type)
 
         annotation_origin = getattr(annotation, "__origin__", None)
 


Test patch:
diff --git a/tests/test_type_converter.py b/tests/test_type_converter.py
--- a/tests/test_type_converter.py
+++ b/tests/test_type_converter.py
@@ -63,34 +63,64 @@ class A:
 
 
 def test_list_of_scalar():
-    field = get_graphql_type_for_annotation(List[str], "Example")
+    field = get_graphql_type_for_annotation(Optional[List[str]], "Example")
 
     assert type(field) == GraphQLList
     assert type(field.of_type) == GraphQLNonNull
     assert type(field.of_type.of_type) == GraphQLScalarType
     assert field.of_type.of_type.name == "String"
 
-    field = get_graphql_type_for_annotation(List[Optional[str]], "Example")
+    field = get_graphql_type_for_annotation(List[str], "Example")
+
+    assert type(field) == GraphQLNonNull
+    assert type(field.of_type) == GraphQLList
+    assert type(field.of_type.of_type) == GraphQLNonNull
+    assert type(field.of_type.of_type.of_type) == GraphQLScalarType
+    assert field.of_type.of_type.of_type.name == "String"
+
+    field = get_graphql_type_for_annotation(Optional[List[Optional[str]]], "Example")
 
     assert type(field) == GraphQLList
     assert type(field.of_type) == GraphQLScalarType
     assert field.of_type.name == "String"
 
+    field = get_graphql_type_for_annotation(List[Optional[str]], "Example")
+
+    assert type(field) == GraphQLNonNull
+    assert type(field.of_type) == GraphQLList
+    assert type(field.of_type.of_type) == GraphQLScalarType
+    assert field.of_type.of_type.name == "String"
+
 
 def test_list_of_object_types():
     @strawberry.type
     class A:
         x: int
 
-    field = get_graphql_type_for_annotation(List[A], "Example")
+    field = get_graphql_type_for_annotation(Optional[List[A]], "Example")
 
     assert type(field) == GraphQLList
     assert type(field.of_type) == GraphQLNonNull
     assert type(field.of_type.of_type) == GraphQLObjectType
     assert field.of_type.of_type.name == "A"
 
-    field = get_graphql_type_for_annotation(List[Optional[A]], "Example")
+    field = get_graphql_type_for_annotation(List[A], "Example")
+
+    assert type(field) == GraphQLNonNull
+    assert type(field.of_type) == GraphQLList
+    assert type(field.of_type.of_type) == GraphQLNonNull
+    assert type(field.of_type.of_type.of_type) == GraphQLObjectType
+    assert field.of_type.of_type.of_type.name == "A"
+
+    field = get_graphql_type_for_annotation(Optional[List[Optional[A]]], "Example")
 
     assert type(field) == GraphQLList
     assert type(field.of_type) == GraphQLObjectType
     assert field.of_type.name == "A"
+
+    field = get_graphql_type_for_annotation(List[Optional[A]], "Example")
+
+    assert type(field) == GraphQLNonNull
+    assert type(field.of_type) == GraphQLList
+    assert type(field.of_type.of_type) == GraphQLObjectType
+    assert field.of_type.of_type.name == "A"