Skip to content

Commit

Permalink
Fix nullable lists of non-scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmoshipco committed Sep 30, 2024
1 parent 36bd99e commit 80d6bd1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
12 changes: 7 additions & 5 deletions strawberry/codegen/query_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,17 +648,19 @@ def _unwrap_type(
wrapper = 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/optional_list_of_people.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query OptionalListOfPeople {
optionalListOfPeople {
name
age
}
}
8 changes: 8 additions & 0 deletions tests/codegen/snapshots/python/optional_list_of_people.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import List, Optional

class OptionalListOfPeopleResultOptionalListOfPeople:
name: str
age: int

class OptionalListOfPeopleResult:
optional_list_of_people: Optional[List[OptionalListOfPeopleResultOptionalListOfPeople]]
8 changes: 8 additions & 0 deletions tests/codegen/snapshots/typescript/optional_list_of_people.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type OptionalListOfPeopleResultOptionalListOfPeople = {
name: string
age: number
}

type OptionalListOfPeopleResult = {
optional_list_of_people: OptionalListOfPeopleResultOptionalListOfPeople[] | undefined
}

0 comments on commit 80d6bd1

Please sign in to comment.