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

Strawberry doesn't call serialize on a custom scalar #3087

Open
nkartashov opened this issue Sep 11, 2023 · 1 comment
Open

Strawberry doesn't call serialize on a custom scalar #3087

nkartashov opened this issue Sep 11, 2023 · 1 comment
Labels
bug Something isn't working info-needed Needs more info from OP

Comments

@nkartashov
Copy link

nkartashov commented Sep 11, 2023

Describe the Bug

I have the following code:

import pydantic
import strawberry
from typing import Optional

class PydanticNullableType(pydantic.BaseModel):
    data: Optional[str] = None

@strawberry.scalar
class NullableString:
    @staticmethod
    def serialize(value: Optional[str]) -> str:
        return "" if value is None else value

    @staticmethod
    def parse_value(value: str) -> Optional[str]:
        return None if value == "" else value

@strawberry.experimental.pydantic.type(model=PydanticNullableType)
class StrawberryType:
    data: NullableString

def make_mock_query(pydantic_model, return_type):
    @strawberry.type
    class Query:
        @strawberry.field
        def serialized_data(self) -> return_type:
            return return_type.from_pydantic(pydantic_model)

    return Query


def nullable_field_serializes():
    pydantic_model = PydanticNullableType()
    assert (
        strawberry.Schema(
            query=make_mock_query(pydantic_model, StrawberryType),
            types=[StrawberryType],
        )
        .execute_sync("query { serializedData { data } }")
        .errors
        == []
    )

nullable_field_serializes()

This code throws an assertion error because running execute_sync results in an error like the following:

E       AssertionError: assert [GraphQLError('Cannot return null for non-nullable field data.', locations=[SourceLocation(line=1, column=46)], path=['serializedData', 'data'])] == []

This happens because NullableString.serialize is not called and therefore doesn't handle the None value.

Why is the NullableString.serialize not called and how can this code be fixed to handle nullable fields?

System Information

  • Operating system:
    ProductName: macOS
    ProductVersion: 13.5.2
    BuildVersion: 22G91

  • Strawberry version (if applicable): 0.192.0

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
@nkartashov nkartashov added the bug Something isn't working label Sep 11, 2023
@DoctorJohn
Copy link
Member

From a quick look over your code I noticed that your custom scalar is not defined as in the docs. I'm pretty sure that's the reason Strawberry does not pick up your scalar and never calls your serialize method.

@DoctorJohn DoctorJohn added the info-needed Needs more info from OP label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working info-needed Needs more info from OP
Projects
None yet
Development

No branches or pull requests

2 participants