Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From_pydantic fails on union type #3641

Open
tobiasBora opened this issue Sep 23, 2024 · 0 comments
Open

From_pydantic fails on union type #3641

tobiasBora opened this issue Sep 23, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@tobiasBora
Copy link

tobiasBora commented Sep 23, 2024

Describe the Bug

If I do:

QuestionType = Annotated[Union[QuestionMultipleChoiceType, QuestionTextType, QuestionNumberType],
                         strawberry.union("Question")]

Then I'm not able to call:

QuestionType.from_pydantic(question)

as from_pydantic does not exist on union types.

MWE:

import strawberry
import operator
from pydantic import BaseModel, TypeAdapter
from typing import Union, Literal, Annotated
from strawberry.schema.config import StrawberryConfig

# Pedantic classes
class QuestionMultipleChoice(BaseModel):
    # This helps pydantic to distinguish the types
    kind: str = "QuestionMultipleChoice"
    nbChoices: int

class QuestionText(BaseModel):
    kind: str = "QuestionText"
    
class QuestionNumber(BaseModel):
    kind: str = "QuestionNumber"

Question = Union[
    QuestionMultipleChoice,
    QuestionText,
    QuestionNumber
]

# GraphQL types

@strawberry.experimental.pydantic.type(model=QuestionMultipleChoice, all_fields=True)
class QuestionMultipleChoiceType:
    pass

@strawberry.experimental.pydantic.type(model=QuestionText, all_fields=True)
class QuestionTextType:
    pass

@strawberry.experimental.pydantic.type(model=QuestionNumber, all_fields=True)
class QuestionNumberType:
    pass

# Redefining all types in the union seems very verbose and error-prone. Is this really the good approach?
QuestionType = Annotated[Union[QuestionMultipleChoiceType, QuestionTextType, QuestionNumberType],
                         strawberry.union("Question")]

# Fake a redis database

data = {
    "questionA": QuestionMultipleChoice(nbChoices=4).model_dump_json(),
    "questionB": QuestionText().model_dump_json(),
    "questionC": QuestionNumber().model_dump_json(),
}
print(data)

@strawberry.type
class Query:
    @strawberry.field
    def get_ident(self, questionID: str) -> QuestionType:
        question = TypeAdapter(Question).validate_json(data[questionID])
        print("question:", question)
        return QuestionType.from_pydantic(question)

config = StrawberryConfig(
    default_resolver=operator.getitem
)

schema = strawberry.Schema(query=Query, config=config)

System Information

  • Operating system: NixOs
  • Strawberry version (if applicable): 0.237.3

Additional Context

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.
Fund with Polar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant