Skip to content

Commit

Permalink
feat: improvements to adhoc: raw result and typed arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 25, 2023
1 parent ff0b085 commit 3108fd0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/dinghy/adhoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,42 @@
logger = click_log.basic_config("dinghy")


TYPES = {
"int": int,
"str": str,
}


@click.command()
@click_log.simple_verbosity_option(logger)
@click.option(
"--nodes",
is_flag=True,
help="Get paginated list of nodes instead of raw result",
)
@click.argument("query_file", type=click.File("r"))
@click.argument("var", metavar="[VAR=VAL]...", nargs=-1)
def adhoc(query_file, var):
@click.argument("var", metavar="[VAR[:type]=VAL]...", nargs=-1)
def adhoc(nodes, query_file, var):
"""
Run an ad-hoc GraphQL query.
"""
query = query_file.read()
variables = dict(v.split("=", 1) for v in var)
variables = {}
for v in var:
name, val = v.split("=", 1)
if ":" in name:
name, type_name = name.split(":")
val = TYPES[type_name](val)
variables[name] = val

token = os.environ.get("GITHUB_TOKEN", "")
gql = GraphqlHelper("https://api.github.com/graphql", token)
data, _ = main_run(gql.nodes(query=query, variables=variables, clear_nodes=False))
if nodes:
data, _ = main_run(
gql.nodes(query=query, variables=variables, clear_nodes=False)
)
else:
data = main_run(gql.execute(query=query, variables=variables))
json.dump(data, sys.stdout, indent=2)


Expand Down

0 comments on commit 3108fd0

Please sign in to comment.