Skip to content

Commit 6b53422

Browse files
authored
Feature gql cli print schema (#258)
1 parent 5df465b commit 6b53422

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

docs/gql-cli/intro.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _gql_cli:
2+
13
gql-cli
24
=======
35

@@ -69,3 +71,10 @@ Then execute query from the file:
6971
7072
$ cat query.gql | gql-cli wss://countries.trevorblades.com/graphql
7173
{"continent": {"name": "Africa"}}
74+
75+
Print the GraphQL schema in a file
76+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77+
78+
.. code-block:: shell
79+
80+
$ gql-cli https://countries.trevorblades.com/graphql --print-schema > schema.graphql

docs/usage/validation.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ The schema can be provided as a String (which is usually stored in a .graphql fi
2121
2222
client = Client(schema=schema_str)
2323
24+
.. note::
25+
You can download a schema from a server by using :ref:`gql-cli <gql_cli>`
26+
27+
:code:`$ gql-cli https://SERVER_URL/graphql --print-schema > schema.graphql`
28+
2429
OR can be created using python classes:
2530

2631
.. code-block:: python

gql/cli.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
55
from typing import Any, Dict
66

7-
from graphql import GraphQLError
7+
from graphql import GraphQLError, print_schema
88
from yarl import URL
99

1010
from gql import Client, __version__, gql
@@ -38,6 +38,9 @@
3838
# Execute query saved in a file
3939
cat query.gql | gql-cli wss://countries.trevorblades.com/graphql
4040
41+
# Print the schema of the backend
42+
gql-cli https://countries.trevorblades.com/graphql --print-schema
43+
4144
"""
4245

4346

@@ -92,6 +95,12 @@ def get_parser(with_examples: bool = False) -> ArgumentParser:
9295
help="set the operation_name value",
9396
dest="operation_name",
9497
)
98+
parser.add_argument(
99+
"--print-schema",
100+
help="get the schema from instrospection and print it",
101+
action="store_true",
102+
dest="print_schema",
103+
)
95104

96105
return parser
97106

@@ -241,7 +250,15 @@ async def main(args: Namespace) -> int:
241250
exit_code = 0
242251

243252
# Connect to the backend and provide a session
244-
async with Client(transport=transport) as session:
253+
async with Client(
254+
transport=transport, fetch_schema_from_transport=args.print_schema
255+
) as session:
256+
257+
if args.print_schema:
258+
schema_str = print_schema(session.client.schema)
259+
print(schema_str)
260+
261+
return exit_code
245262

246263
while True:
247264

tests/custom_scalars/test_money.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,15 @@ async def handler(request):
410410
data = await request.json()
411411
source = data["query"]
412412

413-
print(f"data keys = {data.keys()}")
414413
try:
415414
variables = data["variables"]
416-
print(f"variables = {variables!r}")
417415
except KeyError:
418416
variables = None
419417

420418
result = graphql_sync(
421419
schema, source, variable_values=variables, root_value=root_value
422420
)
423421

424-
print(f"backend result = {result!r}")
425-
426422
return web.json_response(
427423
{
428424
"data": result.data,
@@ -742,3 +738,34 @@ def test_serialize_value_with_nullable_type():
742738
nullable_int = GraphQLInt
743739

744740
assert serialize_value(nullable_int, None) is None
741+
742+
743+
@pytest.mark.asyncio
744+
async def test_gql_cli_print_schema(event_loop, aiohttp_server, capsys):
745+
746+
from gql.cli import get_parser, main
747+
748+
server = await make_money_backend(aiohttp_server)
749+
750+
url = str(server.make_url("/"))
751+
752+
parser = get_parser(with_examples=True)
753+
args = parser.parse_args([url, "--print-schema"])
754+
755+
exit_code = await main(args)
756+
757+
assert exit_code == 0
758+
759+
# Check that the result has been printed on stdout
760+
captured = capsys.readouterr()
761+
captured_out = str(captured.out).strip()
762+
763+
print(captured_out)
764+
assert (
765+
"""
766+
type Subscription {
767+
spend(money: Money): Money
768+
}
769+
""".strip()
770+
in captured_out
771+
)

0 commit comments

Comments
 (0)