Skip to content

Commit

Permalink
Add execute-timeout argument for gql-cli (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
leszekhanusz authored Jul 28, 2022
1 parent 5713ac7 commit a7f7649
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
30 changes: 29 additions & 1 deletion gql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -103,6 +122,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=positive_int_or_none,
default=10,
dest="execute_timeout",
)
parser.add_argument(
"--transport",
default="auto",
Expand Down Expand Up @@ -367,7 +393,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:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ 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

# 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):

Expand Down

0 comments on commit a7f7649

Please sign in to comment.