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

@oneOf inputs do not support nullability #3785

Open
DevJake opened this issue Feb 17, 2025 · 0 comments
Open

@oneOf inputs do not support nullability #3785

DevJake opened this issue Feb 17, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@DevJake
Copy link

DevJake commented Feb 17, 2025

When an input class is marked as @oneOf and has a nullable field, it is not possible to actually provide a null value in the query, and the query completely fails.

Describe the Bug

This bug relates to the usage of input classes that implement the @oneOf directive using one_of=True:

@strawberry.input(one_of=True)
class FilterInput:
    name: str | None

As shown, it should be possible to pass a null value in the query for the name field. However, when executing this query:

query MyQuery {
  test(filterInput: {name: null})
}

You get this response:

{
  "data": null,
  "errors": [
    {
      "message": "Field 'FilterInput.name' must be non-null.",
      "locations": [
        {
          "line": 2,
          "column": 21
        }
      ]
    }
  ]
}

I would assume that the one_of parameter directs Strawberry to evaluate if any of the given fields are None and fail if so, even though this is a legitimate input value.


I have provided this minimum working example, which runs using FastAPI. I have also included Poetry's pyproject.toml file so this issue can be replicated with ease:

[tool.poetry]
name = "strawberry-testing"
version = "0.1.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = ">=3.11,<3.12"
strawberry-graphql = "0.260.2"
fastapi = "0.115.8"
uvicorn = "0.34.0"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
import strawberry


@strawberry.input(one_of=True)
class FilterInput:
    name: str | None


@strawberry.input
class Query:
    @strawberry.field
    async def test(self, filter_input: FilterInput) -> str | None:
        return filter_input.name


app = FastAPI()

schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema=schema)

app.include_router(graphql_app, prefix="/graphql")
@DevJake DevJake added the bug Something isn't working label Feb 17, 2025
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