def test_simple_types(): args = {"integer": 1, "string": "abc", "float": 1.2, "bool": True} arguments = [ StrawberryArgument( graphql_name="integer", type_annotation=StrawberryAnnotation(int), python_name="integer", ), StrawberryArgument( graphql_name="string", type_annotation=StrawberryAnnotation(str), python_name="string", ), StrawberryArgument( graphql_name="float", type_annotation=StrawberryAnnotation(float), python_name="float", ), StrawberryArgument( graphql_name="bool", type_annotation=StrawberryAnnotation(bool), python_name="bool", ), ] assert convert_arguments( args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY ) == { "integer": 1, "string": "abc", "float": 1.2, "bool": True, }
def test_uses_default_for_optional_types_when_nothing_is_passed(): @strawberry.input class Number: value: int @strawberry.input class Input: numbers: Optional[Number] = UNSET numbers_second: Optional[Number] = UNSET # case 1 args = {"input": {}} arguments = [ StrawberryArgument( graphql_name=None, python_name="input", type_annotation=StrawberryAnnotation(Input), ), ] assert convert_arguments(args, arguments) == {"input": Input(UNSET, UNSET)} # case 2 args = {"input": {"numbersSecond": None}} arguments = [ StrawberryArgument( graphql_name=None, python_name="input", type_annotation=StrawberryAnnotation(Input), ), ] assert convert_arguments(args, arguments) == {"input": Input(UNSET, None)}
def test_list(): args = { "integerList": [1, 2], "stringList": ["abc", "cde"], } arguments = [ StrawberryArgument( graphql_name="integerList", python_name="integer_list", type_annotation=StrawberryAnnotation(List[int]), ), StrawberryArgument( graphql_name="stringList", python_name="string_list", type_annotation=StrawberryAnnotation(List[str]), ), ] assert convert_arguments( args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY ) == { "integer_list": [1, 2], "string_list": ["abc", "cde"], }
def test_basic_optional(): annotation = StrawberryAnnotation(Optional[str]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type is str assert resolved == StrawberryOptional(of_type=str) assert resolved == Optional[str]
def test_optional_list(): annotation = StrawberryAnnotation(Optional[List[bool]]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type == List[bool] assert resolved == StrawberryOptional(of_type=List[bool]) assert resolved == Optional[List[bool]]
def test_optional_union_containing_a_real_union_and_unset(): annotation = StrawberryAnnotation(Union[str, int, None, _Unset]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type == Union[str, int] assert resolved == StrawberryOptional(of_type=Union[str, int]) assert resolved == Optional[Union[str, int]]
def test_list_of_optional(): annotation = StrawberryAnnotation(List[Optional[int]]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type == Optional[int] assert resolved == StrawberryList(of_type=Optional[int]) assert resolved == List[Optional[int]]
def test_optional_of_string(): annotation = StrawberryAnnotation(Optional["bool"]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type is bool assert resolved == StrawberryOptional(of_type=bool) assert resolved == Optional[bool]
def test_list_of_string(): annotation = StrawberryAnnotation(List["int"]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type is int assert resolved == StrawberryList(of_type=int) assert resolved == List[int]
def test_list_of_lists(): annotation = StrawberryAnnotation(List[List[float]]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type == List[float] assert resolved == StrawberryList(of_type=List[float]) assert resolved == List[List[float]]
def test_basic_list(): annotation = StrawberryAnnotation(List[str]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type is str assert resolved == StrawberryList(of_type=str) assert resolved == List[str]
def test_string_of_object(): @strawberry.type class StrType: thing: int annotation = StrawberryAnnotation("StrType", namespace=locals()) resolved = annotation.resolve() assert resolved is StrType
def test_optional_with_unset_as_union(): annotation = StrawberryAnnotation(Union[_Unset, None, str]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type is str assert resolved == StrawberryOptional(of_type=str) assert resolved == Optional[str]
def test_string_of_type_var(): T = TypeVar("T") annotation = StrawberryAnnotation("T", namespace=locals()) resolved = annotation.resolve() assert isinstance(resolved, StrawberryTypeVar) assert resolved.type_var is T assert resolved == T
def test_generic_optionals(): T = TypeVar("T") annotation = StrawberryAnnotation(Optional[T]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert isinstance(resolved.of_type, StrawberryTypeVar) assert resolved.is_generic assert resolved == Optional[T]
def test_string_of_optional(): namespace = {**locals(), **globals()} annotation = StrawberryAnnotation("Optional[int]", namespace=namespace) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type is int assert resolved == StrawberryOptional(of_type=int) assert resolved == Optional[int]
def test_basic_generic(): T = TypeVar("T") annotation = StrawberryAnnotation(T) resolved = annotation.resolve() assert isinstance(resolved, StrawberryTypeVar) assert resolved.is_generic assert resolved.type_var is T assert resolved == T
def test_generic_lists(): T = TypeVar("T") annotation = StrawberryAnnotation(List[T]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert isinstance(resolved.of_type, StrawberryTypeVar) assert resolved.is_generic assert resolved == List[T]
def test_optional_optional(): """Optional[Optional[...]] is squashed by Python to just Optional[...]""" annotation = StrawberryAnnotation(Optional[Optional[bool]]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryOptional) assert resolved.of_type is bool assert resolved == StrawberryOptional(of_type=bool) assert resolved == Optional[Optional[bool]] assert resolved == Optional[bool]
def test_string_of_list(): namespace = {**locals(), **globals()} annotation = StrawberryAnnotation("List[float]", namespace=namespace) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type is float assert resolved == StrawberryList(of_type=float) assert resolved == List[float]
def test_generic_unions(): S = TypeVar("S") T = TypeVar("T") annotation = StrawberryAnnotation(Union[S, T]) resolved = annotation.resolve() assert isinstance(resolved, StrawberryUnion) assert resolved.types == (S, T) assert resolved.is_generic assert resolved == Union[S, T]
def test_list_of_string_of_type(): @strawberry.type class NameGoesHere: foo: bool annotation = StrawberryAnnotation(List["NameGoesHere"], namespace=locals()) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type is NameGoesHere assert resolved == StrawberryList(of_type=NameGoesHere) assert resolved == List[NameGoesHere]
def test_basic(): @strawberry.enum class NumaNuma(Enum): MA = "ma" I = "i" # noqa: E741 A = "a" HI = "hi" annotation = StrawberryAnnotation(NumaNuma) resolved = annotation.resolve() # TODO: Remove reference to .enum_definition with StrawberryEnum assert resolved is NumaNuma._enum_definition
def test_lazy_type(): # Module path is short and relative because of the way pytest runs the file LazierType = LazyType["LaziestType", "test_lazy_types"] annotation = StrawberryAnnotation(LazierType) resolved = annotation.resolve() # Currently StrawberryAnnotation(LazyType).resolve() returns the unresolved # LazyType. We may want to find a way to directly return the referenced object # without a second resolving step. assert isinstance(resolved, LazyType) assert resolved is LazierType assert resolved.resolve_type() is LaziestType
def test_forward_reference(): global ForwardClass annotation = StrawberryAnnotation("ForwardClass", namespace=globals()) @strawberry.type class ForwardClass: backward: bool resolved = annotation.resolve() assert resolved is ForwardClass del ForwardClass
def test_string_of_list_of_type(): @strawberry.type class BlahBlah: foo: bool namespace = {**locals(), **globals()} annotation = StrawberryAnnotation("List[BlahBlah]", namespace=namespace) resolved = annotation.resolve() assert isinstance(resolved, StrawberryList) assert resolved.of_type is BlahBlah assert resolved == StrawberryList(of_type=BlahBlah) assert resolved == List[BlahBlah]
def test_type_var(): T = TypeVar("T") annotation = StrawberryAnnotation(T) field = StrawberryField(type_annotation=annotation) assert field.type == T
def test_when_optional(): @strawberry.input class Number: value: int @strawberry.input class Input: numbers: Optional[Number] = UNSET numbers_second: Optional[Number] = UNSET args = {} arguments = [ StrawberryArgument( graphql_name=None, python_name="input", type_annotation=StrawberryAnnotation(Optional[Input]), ) ] assert (convert_arguments( args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY, config=StrawberryConfig(), ) == {})
def test_lazy(): LazierType = LazyType["LaziestType", __name__] args = { "lazyArg": { "something": True }, } arguments = [ StrawberryArgument( graphql_name="lazyArg", python_name="lazy_arg", type_annotation=StrawberryAnnotation(LazierType), ), ] assert (convert_arguments( args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY, config=StrawberryConfig(), ) == { "lazy_arg": LaziestType(something=True) })
def arguments(self) -> List[StrawberryArgument]: parameters = inspect.signature(self._unbound_wrapped_func).parameters function_arguments = set(parameters) - self._SPECIAL_ARGS arguments = self.annotations.copy() arguments.pop("return", None) # Discard return annotation to get just arguments arguments_missing_annotations = function_arguments - set(arguments) if any(arguments_missing_annotations): raise MissingArgumentsAnnotationsError( field_name=self.name, arguments=arguments_missing_annotations, ) module = sys.modules[self._module] annotation_namespace = module.__dict__ strawberry_arguments = [] for arg_name, annotation in arguments.items(): parameter = parameters[arg_name] argument = StrawberryArgument( python_name=arg_name, graphql_name=None, type_annotation=StrawberryAnnotation( annotation=annotation, namespace=annotation_namespace ), default=parameter.default, ) strawberry_arguments.append(argument) return strawberry_arguments