Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add execute-timeout argument for gql-cli #349

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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