From 04b58d904d72c8a0dbf88e2962cd58aa174d8d95 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Thu, 28 Jul 2022 20:29:32 +0200 Subject: [PATCH 1/3] Add execute-timeout argument for gql-cli --- gql/cli.py | 11 ++++++++++- tests/test_cli.py | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gql/cli.py b/gql/cli.py index 27a562b2..9a6bc441 100644 --- a/gql/cli.py +++ b/gql/cli.py @@ -103,6 +103,13 @@ def get_parser(with_examples: bool = False) -> ArgumentParser: action="store_true", dest="print_schema", ) + parser.add_argument( + "--execute-timeout", + help="set the execute_timeout argument of the Client (default: 10)", + type=int, + default=10, + dest="execute_timeout", + ) parser.add_argument( "--transport", default="auto", @@ -367,7 +374,9 @@ async def main(args: Namespace) -> int: # Connect to the backend and provide a session async with Client( - transport=transport, fetch_schema_from_transport=args.print_schema + transport=transport, + fetch_schema_from_transport=args.print_schema, + execute_timeout=args.execute_timeout, ) as session: if args.print_schema: diff --git a/tests/test_cli.py b/tests/test_cli.py index 9066544b..a28e0dde 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -73,6 +73,11 @@ def test_cli_parser(parser): ) assert args.operation_name == "my_operation" + # Check execute_timeout + # gql-cli https://your_server.com --execute-timeout 1 + args = parser.parse_args(["https://your_server.com", "--execute-timeout", "1"]) + assert args.execute_timeout == 1 + def test_cli_parse_headers(parser): From 8be966aa42722a38ff924dec5155b7abb95de94d Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Thu, 28 Jul 2022 20:41:24 +0200 Subject: [PATCH 2/3] Set default to None instead of 10 for DRY --- gql/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gql/cli.py b/gql/cli.py index 9a6bc441..da077189 100644 --- a/gql/cli.py +++ b/gql/cli.py @@ -107,7 +107,7 @@ def get_parser(with_examples: bool = False) -> ArgumentParser: "--execute-timeout", help="set the execute_timeout argument of the Client (default: 10)", type=int, - default=10, + default=None, dest="execute_timeout", ) parser.add_argument( From 0fabc21049d2024683426b695907dc6458e0ae4b Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Thu, 28 Jul 2022 21:25:43 +0200 Subject: [PATCH 3/3] Allow to pass None, fix default value, don't allow negative numbers --- gql/cli.py | 23 +++++++++++++++++++++-- tests/test_cli.py | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/gql/cli.py b/gql/cli.py index da077189..2a6ff3f5 100644 --- a/gql/cli.py +++ b/gql/cli.py @@ -46,6 +46,25 @@ """ +def positive_int_or_none(value_str: str) -> Optional[int]: + """Convert a string argument value into either an int or None. + + Raise a ValueError if the argument is negative or a string which is not "none" + """ + try: + value_int = int(value_str) + except ValueError: + if value_str.lower() == "none": + return None + else: + raise + + if value_int < 0: + raise ValueError + + return value_int + + def get_parser(with_examples: bool = False) -> ArgumentParser: """Provides an ArgumentParser for the gql-cli script. @@ -106,8 +125,8 @@ def get_parser(with_examples: bool = False) -> ArgumentParser: parser.add_argument( "--execute-timeout", help="set the execute_timeout argument of the Client (default: 10)", - type=int, - default=None, + type=positive_int_or_none, + default=10, dest="execute_timeout", ) parser.add_argument( diff --git a/tests/test_cli.py b/tests/test_cli.py index a28e0dde..359e94fb 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -78,6 +78,20 @@ def test_cli_parser(parser): args = parser.parse_args(["https://your_server.com", "--execute-timeout", "1"]) assert args.execute_timeout == 1 + # gql-cli https://your_server.com --execute-timeout=none + args = parser.parse_args(["https://your_server.com", "--execute-timeout", "none"]) + assert args.execute_timeout is None + + # gql-cli https://your_server.com --execute-timeout=-1 + with pytest.raises(SystemExit): + args = parser.parse_args(["https://your_server.com", "--execute-timeout", "-1"]) + + # gql-cli https://your_server.com --execute-timeout=invalid + with pytest.raises(SystemExit): + args = parser.parse_args( + ["https://your_server.com", "--execute-timeout", "invalid"] + ) + def test_cli_parse_headers(parser):