def test_list():
    args = {
        "integerList": [1, 2],
        "stringList": ["abc", "cde"],
    }

    arguments = [
        StrawberryArgument(
            graphql_name="integerList",
            python_name="integer_list",
            type_=int,
            is_list=True,
            child=StrawberryArgument(graphql_name=None,
                                     python_name=None,
                                     type_=int),
        ),
        StrawberryArgument(
            graphql_name="stringList",
            python_name="string_list",
            type_=str,
            is_list=True,
            child=StrawberryArgument(graphql_name=None,
                                     python_name=None,
                                     type_=str),
        ),
    ]

    assert convert_arguments(args, arguments) == {
        "integer_list": [1, 2],
        "string_list": ["abc", "cde"],
    }
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)}
Esempio n. 3
0
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,
    }
Esempio n. 4
0
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_input_types():
    @strawberry.input
    class MyInput:
        abc: str
        say_hello_to: str
        fun: str
        was: int = strawberry.field(name="having")

    args = {
        "input": {
            "abc": "example",
            "sayHelloTo": "Patrick",
            "having": 10,
            "fun": "yes"
        }
    }

    arguments = [
        StrawberryArgument(graphql_name=None,
                           python_name="input",
                           type_=MyInput)
    ]

    assert convert_arguments(args, arguments) == {
        "input": MyInput(abc="example",
                         say_hello_to="Patrick",
                         was=10,
                         fun="yes")
    }
def test_input_types():
    @strawberry.input
    class MyInput:
        abc: str
        say_hello_to: str
        fun: str
        was: int = strawberry.field(name="having")

    args = {
        "input": {
            "abc": "example",
            "sayHelloTo": "Patrick",
            "having": 10,
            "fun": "yes"
        }
    }

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(MyInput),
        ),
    ]

    assert (convert_arguments(
        args,
        arguments,
        scalar_registry=DEFAULT_SCALAR_REGISTRY,
        config=StrawberryConfig(),
    ) == {
        "input":
        MyInput(abc="example", say_hello_to="Patrick", was=10, fun="yes")
    })
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)
    })
Esempio n. 8
0
    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
def test_nested_list_of_complex_types():
    @strawberry.input
    class Number:
        value: int

    @strawberry.input
    class Input:
        numbers: List[Number]

    args = {"input": {"numbers": [{"value": 1}, {"value": 2}]}}

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(Input),
        ),
    ]

    assert (convert_arguments(
        args,
        arguments,
        scalar_registry=DEFAULT_SCALAR_REGISTRY,
        config=StrawberryConfig(),
    ) == {
        "input": Input(numbers=[Number(1), Number(2)])
    })
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_optional_list_of_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"inputList": [{"abc": "example"}]}

    arguments = [
        StrawberryArgument(
            graphql_name="inputList",
            python_name="input_list",
            is_optional=True,
            type_=None,
            child=StrawberryArgument(graphql_name=None,
                                     python_name=None,
                                     type_=MyInput),
            is_list=True,
        )
    ]
    assert convert_arguments(args, arguments) == {
        "input_list": [MyInput(abc="example")]
    }
def test_simple_types():
    args = {"integer": 1, "string": "abc", "float": 1.2, "bool": True}

    arguments = [
        StrawberryArgument(graphql_name="integer",
                           type_=int,
                           python_name="integer"),
        StrawberryArgument(graphql_name="string",
                           type_=str,
                           python_name="string"),
        StrawberryArgument(graphql_name="float",
                           type_=float,
                           python_name="float"),
        StrawberryArgument(graphql_name="bool", type_=bool,
                           python_name="bool"),
    ]

    assert convert_arguments(args, arguments) == {
        "integer": 1,
        "string": "abc",
        "float": 1.2,
        "bool": True,
    }
Esempio n. 13
0
def test_optional_list_of_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"inputList": [{"abc": "example"}]}

    arguments = [
        StrawberryArgument(
            graphql_name="inputList",
            python_name="input_list",
            type_annotation=StrawberryAnnotation(Optional[List[MyInput]]),
        ),
    ]
    assert convert_arguments(
        args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY
    ) == {"input_list": [MyInput(abc="example")]}
def test_optional_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"input": {"abc": "example"}}

    arguments = [
        StrawberryArgument(graphql_name=None,
                           python_name="input",
                           type_=MyInput,
                           is_optional=True)
    ]

    assert convert_arguments(args, arguments) == {
        "input": MyInput(abc="example")
    }
def test_nested_list_of_complex_types():
    @strawberry.input
    class Number:
        value: int

    @strawberry.input
    class Input:
        numbers: List[Number]

    args = {"input": {"numbers": [{"value": 1}, {"value": 2}]}}

    arguments = [
        StrawberryArgument(graphql_name=None, python_name="input", type_=Input)
    ]

    assert convert_arguments(args, arguments) == {
        "input": Input(numbers=[Number(1), Number(2)])
    }
def test_list_of_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"inputList": [{"abc": "example"}]}

    arguments = [
        StrawberryArgument(
            graphql_name="inputList",
            python_name="input_list",
            type_annotation=StrawberryAnnotation(List[MyInput]),
        ),
    ]

    assert convert_arguments(args, arguments) == {
        "input_list": [MyInput(abc="example")]
    }
def test_optional_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"input": {"abc": "example"}}

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(Optional[MyInput]),
        ),
    ]

    assert convert_arguments(args, arguments) == {
        "input": MyInput(abc="example")
    }
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_=Input,
                           is_optional=True)
    ]

    assert convert_arguments(args, arguments) == {}
Esempio n. 19
0
    def arguments(self) -> List[StrawberryArgument]:
        # TODO: Move to StrawberryArgument? StrawberryResolver ClassVar?
        SPECIAL_ARGS = {"root", "self", "info"}

        annotations = self.wrapped_func.__annotations__
        parameters = inspect.signature(self.wrapped_func).parameters
        function_arguments = set(parameters) - SPECIAL_ARGS

        annotations = {
            name: annotation
            for name, annotation in annotations.items()
            if name not in (SPECIAL_ARGS | {"return"})
        }

        annotated_arguments = set(annotations)
        arguments_missing_annotations = function_arguments - annotated_arguments

        if any(arguments_missing_annotations):
            raise MissingArgumentsAnnotationsError(
                field_name=self.wrapped_func.__name__,
                arguments=arguments_missing_annotations,
            )

        module = sys.modules[self.wrapped_func.__module__]
        annotation_namespace = module.__dict__
        arguments = []
        for arg_name, annotation in annotations.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,
            )

            arguments.append(argument)

        return arguments
def test_optional_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"input": {"abc": "example"}}

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(Optional[MyInput]),
        ),
    ]

    assert (convert_arguments(
        args,
        arguments,
        scalar_registry=DEFAULT_SCALAR_REGISTRY,
        config=StrawberryConfig(),
    ) == {
        "input": MyInput(abc="example")
    })
Esempio n. 21
0
    def arguments(self) -> List[StrawberryArgument]:
        annotations = self.resolver.__annotations__
        annotations = dict(islice(annotations.items(), 1, None))
        annotations.pop("return", None)

        parameters = inspect.signature(self.resolver).parameters

        module = sys.modules[self.resolver.__module__]
        annotation_namespace = module.__dict__
        arguments = []
        for arg_name, annotation in annotations.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,
            )

            arguments.append(argument)

        return arguments
Esempio n. 22
0
def test_nested_input_types():
    @strawberry.enum
    class ChangeType(Enum):
        MAJOR = "major"
        MINOR = "minor"
        PATCH = "patch"

    @strawberry.input
    class ReleaseInfo:
        change_type: ChangeType
        changelog: str

    @strawberry.enum
    class ReleaseFileStatus(Enum):
        MISSING = "missing"
        INVALID = "invalid"
        OK = "ok"

    @strawberry.input
    class AddReleaseFileCommentInput:
        pr_number: int
        status: ReleaseFileStatus
        release_info: Optional[ReleaseInfo]

    args = {
        "input": {
            "prNumber": 12,
            "status": ReleaseFileStatus.OK.value,
            "releaseInfo": {
                "changeType": ChangeType.MAJOR.value,
                "changelog": "example",
            },
        }
    }

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(AddReleaseFileCommentInput),
        ),
    ]

    assert convert_arguments(
        args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY
    ) == {
        "input": AddReleaseFileCommentInput(
            pr_number=12,
            status=ReleaseFileStatus.OK,
            release_info=ReleaseInfo(change_type=ChangeType.MAJOR, changelog="example"),
        )
    }

    args = {
        "input": {
            "prNumber": 12,
            "status": ReleaseFileStatus.OK.value,
            "releaseInfo": None,
        }
    }

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(AddReleaseFileCommentInput),
        ),
    ]

    assert convert_arguments(
        args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY
    ) == {
        "input": AddReleaseFileCommentInput(
            pr_number=12, status=ReleaseFileStatus.OK, release_info=None
        )
    }