Skip to content

Commit

Permalink
Fix codegen crash on nullable lists of non-scalars (#3653)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacob Allen <[email protected]>
  • Loading branch information
enoua5 and jacobmoshipco authored Oct 6, 2024
1 parent ffdd85b commit 5c0f0b0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: patch

Fixes an issue where the codegen tool would crash when working with a nullable list of types.
14 changes: 8 additions & 6 deletions strawberry/codegen/query_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,20 +645,22 @@ def _unwrap_type(
) -> Tuple[
Union[type, StrawberryType], Optional[Callable[[GraphQLType], GraphQLType]]
]:
wrapper = None
wrapper: Optional[Callable[[GraphQLType], GraphQLType]] = None

if isinstance(type_, StrawberryOptional):
type_, wrapper = self._unwrap_type(type_.of_type)
type_, previous_wrapper = self._unwrap_type(type_.of_type)
wrapper = (
GraphQLOptional
if wrapper is None
else lambda t: GraphQLOptional(wrapper(t)) # type: ignore[misc]
if previous_wrapper is None
else lambda t: GraphQLOptional(previous_wrapper(t)) # type: ignore[misc]
)

elif isinstance(type_, StrawberryList):
type_, wrapper = self._unwrap_type(type_.of_type)
type_, previous_wrapper = self._unwrap_type(type_.of_type)
wrapper = (
GraphQLList if wrapper is None else lambda t: GraphQLList(wrapper(t))
GraphQLList
if previous_wrapper is None
else lambda t: GraphQLList(previous_wrapper(t))
)

elif isinstance(type_, LazyType):
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Query:
person: Person
optional_person: Optional[Person]
list_of_people: List[Person]
optional_list_of_people: Optional[List[Person]]
enum: Color
json: JSON
union: PersonOrAnimal
Expand Down
6 changes: 6 additions & 0 deletions tests/codegen/queries/nullable_list_of_non_scalars.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query OperationName {
optionalListOfPeople {
name
age
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import List, Optional

class OperationNameResultOptionalListOfPeople:
name: str
age: int

class OperationNameResult:
optional_list_of_people: Optional[List[OperationNameResultOptionalListOfPeople]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type OperationNameResultOptionalListOfPeople = {
name: string
age: number
}

type OperationNameResult = {
optional_list_of_people: OperationNameResultOptionalListOfPeople[] | undefined
}

0 comments on commit 5c0f0b0

Please sign in to comment.