Skip to content

Commit

Permalink
Handle UNSET values in the strawberry schema.
Browse files Browse the repository at this point in the history
The codegen currently chokes on `strawberry.UNSET` default values.  This PR updates the codegen to stop raising an exception and instead pass a `GraphQLNullValue(value=UNSET)` to the plugins.  The default python plugin writes a `None` as the default value because there really isn't a well understood concept of UNSET in the python eco
system.  User defined plugins have all the information that they need to handle this differently if they choose to do so.
  • Loading branch information
Matt Gilson committed Aug 23, 2023
1 parent 579a1d0 commit a2904fe
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions strawberry/codegen/plugins/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
GraphQLEnum,
GraphQLEnumValue,
GraphQLList,
GraphQLNullValue,
GraphQLObjectType,
GraphQLOptional,
GraphQLScalar,
Expand Down Expand Up @@ -141,6 +142,8 @@ def _print_argument_value(self, argval: GraphQLArgumentValue) -> str:
"GraphQLEnumValue must have a type for python code gen. {argval}"
)
return f"{argval.enum_type}.{argval.name}"
if isinstance(argval, GraphQLNullValue):
return "None"
if not hasattr(argval, "value"):
raise TypeError(f"Unrecognized values type: {argval}")
return repr(argval.value)
Expand Down
6 changes: 4 additions & 2 deletions strawberry/codegen/query_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
)
from strawberry.types.types import StrawberryObjectDefinition
from strawberry.union import StrawberryUnion
from strawberry.unset import UNSET
from strawberry.utils.str_converters import capitalize_first, to_camel_case

from .exceptions import (
Expand Down Expand Up @@ -178,8 +179,9 @@ def _get_deps(t: GraphQLType) -> Iterable[GraphQLType]:

def _py_to_graphql_value(obj: Any) -> GraphQLArgumentValue:
"""Convert a python object to a GraphQLArgumentValue."""
if obj is None:
return GraphQLNullValue()
if obj is None or obj is UNSET:
return GraphQLNullValue(value=obj)

obj_type = type(obj)
if obj_type in _TYPE_TO_GRAPHQL_TYPE:
return _TYPE_TO_GRAPHQL_TYPE[obj_type](obj)
Expand Down
3 changes: 2 additions & 1 deletion strawberry/codegen/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
if TYPE_CHECKING:
from enum import EnumMeta
from typing_extensions import Literal
from strawberry.unset import UnsetType


@dataclass
Expand Down Expand Up @@ -134,7 +135,7 @@ class GraphQLBoolValue:
class GraphQLNullValue:
"""A class that represents a GraphQLNull value."""

value: None = None
value: None | UnsetType = None


@dataclass
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Image(Node):
@strawberry.input
class PersonInput:
name: str
age: Optional[int] = strawberry.UNSET


@strawberry.input
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/snapshots/python/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class OperationNameResult:

class PersonInput:
name: str
age: Optional[int] = None

class ExampleInput:
id: str
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/snapshots/typescript/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type OperationNameResult = {

type PersonInput = {
name: string
age: number | undefined
}

type ExampleInput = {
Expand Down

0 comments on commit a2904fe

Please sign in to comment.