buggy generic interfaces
Closed this issue · 3 comments
Speedy1991 commented
import strawberry
from typing import Optional, Generic, TypeVar, List
from strawberry.scalars import JSON
GenericType = TypeVar("GenericType")
@strawberry.interface
class BlockInterface:
id: strawberry.ID
disclaimer: Optional[str] = strawberry.field(default=None, description='Richtext')
@strawberry.type
class JsonBlock(BlockInterface):
data: JSON
@strawberry.type
class BlockRowType(BlockInterface, Generic[GenericType]):
total: int
items: List[GenericType]
@strawberry.type
class Query:
@strawberry.field
def blocks(self) -> List[BlockInterface]:
return [
BlockRowType(id="3", total=3, items=["a", "b", "c"]),
BlockRowType(id="1", total=1, items=[1, 2, 3, 4]),
JsonBlock(id="2", data={"a": 1}),
]
schema = strawberry.Schema(query=Query, types=[BlockRowType[int], JsonBlock, BlockRowType[str]])
throws: Int cannot represent non-integer value: 'a'
See this gists for more examples:
https://play.strawberry.rocks/?gist=e236f23fee40493f1952ad88cb05df80
https://play.strawberry.rocks/?gist=f56308e2f925eb05c37d82b4ad70efd8
https://play.strawberry.rocks/?gist=3c48b2f9e848042e87a9f9c9da143994
Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
Speedy1991 commented
Speedy1991 commented
Could I assist in making progress on this issue? :)
patrick91 commented
I've done some digging in this, and we have the same issue with unions:
def test_using_generics_with_union():
@strawberry.type
class JsonBlock:
data: JSON
@strawberry.type
class BlockRowType(Generic[GenericType]):
total: int
items: List[GenericType]
@strawberry.type
class Query:
@strawberry.field
def blocks(
self,
) -> List[Union[BlockRowType[int], BlockRowType[str], JsonBlock]]:
return [
BlockRowType(total=3, items=["a", "b", "c"]),
BlockRowType(total=1, items=[1, 2, 3, 4]),
JsonBlock(data=JSON({"a": 1})),
]
schema = strawberry.Schema(query=Query)
result = schema.execute_sync("""query {
blocks {
__typename
... on IntBlockRowType {
a: items
}
... on StrBlockRowType {
b: items
}
... on JsonBlock {
data
}
}
}""")
assert not result.errors
assert result.data == {
"blocks": [
{"id": "3", "__typename": "StrBlockRowType", "items": ["a", "b", "c"]},
{"id": "1", "__typename": "IntBlockRowType", "items": [1, 2, 3, 4]},
{"id": "2", "__typename": "JsonBlock", "data": {"a": 1}},
]
}
fix this in unions, means touching this codebase:
strawberry/strawberry/union.py
Lines 170 to 224 in 29b7dd8
which is separated from the interface part, so I need to think how we could combine both